Skip to content

Commit

Permalink
use Cypher.cyeor function in cy_run to avoid erros as much as pos…
Browse files Browse the repository at this point in the history
…sible
  • Loading branch information
Itz-fork committed Jan 21, 2024
1 parent 2ffbcc8 commit f98401e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# dev environment
.mega/
42 changes: 30 additions & 12 deletions megadl/helpers/cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from asyncio import sleep as xsleep
from cryptography.fernet import Fernet

from pyrogram.types import Message
from pyrogram import Client, errors
from pyrogram.handlers import MessageHandler
from pyrogram.types import Message, CallbackQuery

from .database import CypherDB
from .files import send_as_guessed, fs_cleanup, splitit, listfiles
Expand Down Expand Up @@ -128,15 +128,17 @@ def run_checks(self, func) -> Callable:
Decorator to run middleware
"""

async def cy_run(client: Client, msg: Message):
async def cy_run(client: Client, msg: Message | CallbackQuery):
can_use = False
# shit might just work
# use message of the query if it's a callback query
uid = (
msg.from_user.id
if isinstance(msg, Message)
else msg.message.from_user.id
msg.message.from_user.id
if isinstance(msg, CallbackQuery)
else msg.from_user.id
)

try:
# no need to use "Cypher.cyeor" as `use_logs` are Message handlers
if func.__name__ in self.use_logs:
# return if user has already started a process
if uid in self.mega_running or uid in self.ddl_running:
Expand All @@ -154,8 +156,10 @@ async def cy_run(client: Client, msg: Message):
if self.database:
status = await self.database.add(uid)
if status["banned"]:
return await msg.reply(
f"**You're banned from using this bot 😬** \n\n**Reason:** `{status['reason']}`"
return await self.cyeor(
msg,
f"**You're banned from using this bot 😬** \n\n**Reason:** `{status['reason']}`",
True,
)

if "*" in self.auth_users:
Expand All @@ -164,8 +168,10 @@ async def cy_run(client: Client, msg: Message):
can_use = uid in self.auth_users

if not can_use:
await msg.reply(
"`You're not authorized to use this bot 🙅‍♂️` \n\n**Join @NexaBotsUpdates ❤️**"
await self.cyeor(
msg,
"`You're not authorized to use this bot 🙅‍♂️` \n\n**Join @NexaBotsUpdates ❤️**",
True,
)
return msg.stop_propagation()

Expand All @@ -178,7 +184,9 @@ async def cy_run(client: Client, msg: Message):
except errors.MessageNotModified:
pass
except FileExistsError:
await self.cyeor(msg, "`File already exists in the server 😬`")
await self.cyeor(
msg, "`File already exists in the server. Try again 😬`"
)
await self.full_cleanup(self.dl_loc, uid)
# Other exceptions
except Exception as e:
Expand All @@ -190,7 +198,17 @@ async def cy_run(client: Client, msg: Message):

return cy_run

async def cyeor(self, msg, text: str, reply: bool = False, **kwargs):
async def cyeor(
self, msg: Message | CallbackQuery, text: str, reply: bool = False, **kwargs
):
"""
Edit or Reply to a Message or CallbackQuery
Arguments:
msg (Message | CallbackQuery): Message or CallbackQuery to edit or reply to
text (str): Text to edit or reply with
reply (bool, optional): Whether to reply to the msg or not. Defaults to False.
"""
if isinstance(msg, Message):
if reply:
await msg.reply(text)
Expand Down

0 comments on commit f98401e

Please sign in to comment.