2020-05-16 17:52:29 -04:00
|
|
|
import asyncio
|
2022-08-02 16:25:02 -04:00
|
|
|
from inspect import isawaitable
|
2020-06-29 15:29:20 -04:00
|
|
|
from random import randint
|
2021-02-16 19:07:18 -05:00
|
|
|
from re import DOTALL, IGNORECASE, escape, search
|
2020-10-04 13:43:57 -04:00
|
|
|
from traceback import format_exc, print_exc
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-10-03 11:27:11 -04:00
|
|
|
from telethon import events
|
2023-05-07 17:08:49 -04:00
|
|
|
from telethon.errors.rpcerrorlist import (ChatAdminRequiredError,
|
|
|
|
ChatWriteForbiddenError)
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2023-10-21 19:30:14 -04:00
|
|
|
from ubot.command import CallbackQueryCommand, Command
|
|
|
|
from ubot.custom import (ExtendedCallbackQuery, ExtendedInlineQuery,
|
|
|
|
ExtendedNewMessage)
|
|
|
|
from ubot.database import ChatWrapper
|
|
|
|
|
2020-06-15 13:04:35 -04:00
|
|
|
from .fixes import inline_photos
|
|
|
|
|
2023-10-21 20:57:02 -04:00
|
|
|
PATTERN_TEMPLATE = "^{0}({1})(?: |\n|$|_|@{2}(?: |\n|$|_))(.*)"
|
|
|
|
SIMPLE_PATTERN_TEMPLATE = "^({0})(?: |\n|$)(.*)"
|
|
|
|
RAW_PATTERN_TEMPLATE = "{0}"
|
|
|
|
MODERATION_COMMAND_COOLDOWN_SEC = 3
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-07-18 15:11:09 -04:00
|
|
|
|
2023-10-21 20:57:02 -04:00
|
|
|
class CommandHandler():
|
2020-07-18 15:11:09 -04:00
|
|
|
incoming_commands = []
|
|
|
|
inline_photo_commands = []
|
|
|
|
inline_article_commands = []
|
|
|
|
callback_queries = []
|
|
|
|
|
2023-10-16 14:32:57 -04:00
|
|
|
def __init__(self, loader):
|
2020-06-23 15:28:09 -04:00
|
|
|
self.loader = loader
|
2023-10-16 14:32:57 -04:00
|
|
|
self.micro_bot = loader.micro_bot
|
|
|
|
self.settings = self.micro_bot.settings
|
|
|
|
self.logger = loader.logger
|
2021-03-23 19:16:21 -04:00
|
|
|
self.db = loader.db
|
2021-03-23 20:01:27 -04:00
|
|
|
self.hard_prefix = self.settings.get_list("hard_cmd_prefix") or ["/"]
|
2023-10-16 14:32:57 -04:00
|
|
|
self.micro_bot.client.add_event_handler(self.report_incoming_excepts, events.NewMessage(incoming=True, forwards=False, func=lambda e: e.raw_text))
|
|
|
|
self.micro_bot.client.add_event_handler(self.handle_inline, events.InlineQuery())
|
|
|
|
self.micro_bot.client.add_event_handler(self.handle_callback_query, events.CallbackQuery())
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-10-03 12:18:34 -04:00
|
|
|
async def report_incoming_excepts(self, event):
|
|
|
|
try:
|
|
|
|
await self.handle_incoming(event)
|
2023-05-07 15:18:00 -04:00
|
|
|
except Exception as exception:
|
|
|
|
if not isinstance(exception, (ChatAdminRequiredError, ChatWriteForbiddenError)):
|
|
|
|
await event.client.send_message(int(self.settings.get_list("owner_id")[0]), str(format_exc()))
|
2020-10-03 12:18:34 -04:00
|
|
|
|
2023-10-21 19:30:14 -04:00
|
|
|
async def handle_incoming(self, event: ExtendedNewMessage):
|
2023-10-12 18:05:28 -04:00
|
|
|
chat_db = self.db.get_chat((await event.get_chat()).id)
|
2021-03-24 16:16:13 -04:00
|
|
|
chat_prefix = chat_db.prefix
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
for command in self.incoming_commands:
|
|
|
|
if command.simple_pattern:
|
2023-10-21 20:57:02 -04:00
|
|
|
pattern_match = search(SIMPLE_PATTERN_TEMPLATE.format(command.pattern + command.pattern_extra), event.raw_text, IGNORECASE|DOTALL)
|
2020-09-15 20:02:44 -04:00
|
|
|
elif command.raw_pattern:
|
2023-10-21 20:57:02 -04:00
|
|
|
pattern_match = search(RAW_PATTERN_TEMPLATE.format(command.pattern + command.pattern_extra), event.raw_text, IGNORECASE|DOTALL)
|
2020-06-26 19:17:50 -04:00
|
|
|
else:
|
2021-03-24 10:32:33 -04:00
|
|
|
if command.not_disableable:
|
|
|
|
prefix_list = self.hard_prefix + [chat_prefix] + ["/"]
|
|
|
|
else:
|
|
|
|
prefix_list = self.hard_prefix + [chat_prefix]
|
|
|
|
|
2023-10-21 20:57:02 -04:00
|
|
|
pattern_match = search(PATTERN_TEMPLATE.format(f"({'|'.join([escape(i) for i in prefix_list])})", command.pattern + command.pattern_extra, self.micro_bot.me.username), event.raw_text, IGNORECASE|DOTALL)
|
2020-04-16 16:26:05 -04:00
|
|
|
|
2020-04-17 16:47:19 -04:00
|
|
|
if pattern_match:
|
2023-10-21 19:58:41 -04:00
|
|
|
command.uses += 1
|
|
|
|
|
2023-10-16 18:13:41 -04:00
|
|
|
if command.moderation and not chat_db.modmode_enabled:
|
|
|
|
continue
|
|
|
|
|
2023-10-12 21:32:21 -04:00
|
|
|
if not (priv_resp := await self.check_privs(event, command, chat_db))[0]:
|
|
|
|
if priv_resp[1]:
|
|
|
|
await event.reply(priv_resp[1])
|
|
|
|
|
2022-08-02 16:25:02 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
if command.filter:
|
|
|
|
if isawaitable(command.filter):
|
|
|
|
if not await command.filter(event):
|
|
|
|
continue
|
|
|
|
elif not command.filter(event):
|
|
|
|
continue
|
2020-07-18 15:11:09 -04:00
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
if command.pass_nsfw:
|
2021-03-24 16:16:13 -04:00
|
|
|
event.nsfw_disabled = not chat_db.nsfw_enabled
|
2020-06-09 21:16:18 -04:00
|
|
|
|
2020-10-03 15:34:26 -04:00
|
|
|
if command.raw_pattern:
|
|
|
|
event.command = command.pattern
|
|
|
|
else:
|
|
|
|
event.command = pattern_match.groups()[1]
|
2020-08-24 12:07:23 -04:00
|
|
|
|
2023-10-12 19:22:06 -04:00
|
|
|
if event.chat and not command.not_disableable and event.command in chat_db.disabled_commands:
|
2022-08-02 16:25:02 -04:00
|
|
|
continue
|
2020-08-24 12:07:23 -04:00
|
|
|
|
2020-10-03 15:34:26 -04:00
|
|
|
if not command.raw_pattern:
|
|
|
|
event.args = pattern_match.groups()[-1].strip()
|
2021-01-25 17:21:18 -05:00
|
|
|
|
|
|
|
if command.simple_pattern:
|
|
|
|
event.other_args = pattern_match.groups()[1:-1]
|
|
|
|
else:
|
|
|
|
event.other_args = pattern_match.groups()[2:-1]
|
2021-03-23 20:07:59 -04:00
|
|
|
event.prefix = pattern_match.groups()[0]
|
2020-10-03 15:34:26 -04:00
|
|
|
|
2020-04-17 16:47:19 -04:00
|
|
|
event.pattern_match = pattern_match
|
2020-09-15 20:02:44 -04:00
|
|
|
event.extra = command.extra
|
2020-09-18 20:39:45 -04:00
|
|
|
event.object = command
|
2021-03-23 20:55:54 -04:00
|
|
|
event.chat_db = chat_db
|
2023-10-16 09:46:31 -04:00
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
await self.execute_command(event, command)
|
2020-05-16 17:52:29 -04:00
|
|
|
|
2023-10-21 19:30:14 -04:00
|
|
|
async def handle_inline(self, event: ExtendedInlineQuery):
|
2020-09-15 20:02:44 -04:00
|
|
|
for command in self.inline_photo_commands:
|
2023-10-21 20:57:02 -04:00
|
|
|
pattern_match = search(SIMPLE_PATTERN_TEMPLATE.format(command.pattern + command.pattern_extra), event.text, IGNORECASE|DOTALL)
|
2020-05-16 17:52:29 -04:00
|
|
|
|
2020-05-25 14:04:56 -04:00
|
|
|
if pattern_match:
|
2023-10-22 19:43:46 -04:00
|
|
|
if self.is_blacklisted(event) and not self.is_owner(event) and not self.is_sudo(event):
|
2020-06-12 14:20:18 -04:00
|
|
|
return
|
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
await self.handle_inline_photo(event, pattern_match, command)
|
2020-05-25 14:04:56 -04:00
|
|
|
return
|
2020-05-21 13:53:11 -04:00
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
for command in self.inline_article_commands:
|
2023-10-21 20:57:02 -04:00
|
|
|
pattern_match = search(SIMPLE_PATTERN_TEMPLATE.format(command.pattern + command.pattern_extra), event.text, IGNORECASE|DOTALL)
|
2020-05-21 13:53:11 -04:00
|
|
|
|
|
|
|
if pattern_match:
|
2023-10-22 19:43:46 -04:00
|
|
|
if self.is_blacklisted(event) and not self.is_owner(event) and not self.is_sudo(event):
|
2020-06-12 14:20:18 -04:00
|
|
|
return
|
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
await self.handle_inline_article(event, pattern_match, command)
|
2020-05-25 14:04:56 -04:00
|
|
|
return
|
2020-05-16 17:52:29 -04:00
|
|
|
|
2020-05-25 14:04:56 -04:00
|
|
|
await self.fallback_inline(event)
|
2020-05-21 13:53:11 -04:00
|
|
|
|
2023-10-21 19:30:14 -04:00
|
|
|
async def handle_inline_photo(self, event: ExtendedInlineQuery, pattern_match, command):
|
2020-05-25 14:04:56 -04:00
|
|
|
builder = event.builder
|
|
|
|
event.pattern_match = pattern_match
|
|
|
|
event.args = pattern_match.groups()[-1]
|
2020-07-16 13:41:42 -04:00
|
|
|
event.other_args = pattern_match.groups()[1:-1]
|
|
|
|
event.command = pattern_match.groups()[0]
|
2020-10-03 17:31:15 -04:00
|
|
|
event.extra = command.extra
|
2020-09-18 20:39:45 -04:00
|
|
|
event.object = command
|
2020-12-08 21:04:48 -05:00
|
|
|
event.parse_mode = command.parse_mode
|
2020-05-21 13:53:11 -04:00
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
photo_list = await command.function(event)
|
2020-05-21 13:53:11 -04:00
|
|
|
|
2020-06-15 13:04:35 -04:00
|
|
|
if not photo_list:
|
2020-05-25 14:04:56 -04:00
|
|
|
return
|
2020-05-21 13:53:11 -04:00
|
|
|
|
2020-05-25 14:04:56 -04:00
|
|
|
photo_coros = []
|
2020-05-16 17:52:29 -04:00
|
|
|
|
2020-06-15 13:04:35 -04:00
|
|
|
for photo in photo_list:
|
2020-05-25 14:04:56 -04:00
|
|
|
try:
|
2020-06-15 13:04:35 -04:00
|
|
|
if isinstance(photo, list):
|
2020-12-08 21:04:48 -05:00
|
|
|
photo_coros += [
|
|
|
|
self.try_coro(inline_photos.photo(
|
|
|
|
event.client,
|
|
|
|
photo[0],
|
|
|
|
text=photo[1],
|
|
|
|
parse_mode=event.parse_mode
|
|
|
|
))
|
|
|
|
]
|
2020-06-15 13:04:35 -04:00
|
|
|
else:
|
|
|
|
photo_coros += [self.try_coro(builder.photo(photo))]
|
2020-05-25 14:04:56 -04:00
|
|
|
except:
|
2020-10-04 13:43:57 -04:00
|
|
|
print_exc()
|
2020-05-25 14:04:56 -04:00
|
|
|
|
|
|
|
if photo_coros:
|
|
|
|
photos = await asyncio.gather(*photo_coros)
|
|
|
|
else:
|
|
|
|
return
|
2020-05-16 17:52:29 -04:00
|
|
|
|
2020-05-25 14:04:56 -04:00
|
|
|
try:
|
2020-05-25 14:38:53 -04:00
|
|
|
await event.answer([i for i in photos if i], gallery=True)
|
2020-05-25 14:04:56 -04:00
|
|
|
except:
|
2020-10-04 13:43:57 -04:00
|
|
|
print_exc()
|
2020-05-25 14:04:56 -04:00
|
|
|
|
2023-10-21 19:30:14 -04:00
|
|
|
async def handle_inline_article(self, event: ExtendedInlineQuery, pattern_match, command):
|
2020-05-25 14:04:56 -04:00
|
|
|
builder = event.builder
|
|
|
|
event.pattern_match = pattern_match
|
|
|
|
event.args = pattern_match.groups()[-1]
|
2020-09-23 20:15:21 -04:00
|
|
|
event.other_args = pattern_match.groups()[1:-1]
|
|
|
|
event.command = pattern_match.groups()[0]
|
2020-10-03 17:31:15 -04:00
|
|
|
event.extra = command.extra
|
2020-09-18 20:39:45 -04:00
|
|
|
event.object = command
|
2020-11-05 14:05:49 -05:00
|
|
|
event.link_preview = command.link_preview
|
2020-12-08 21:04:48 -05:00
|
|
|
event.parse_mode = command.parse_mode
|
2020-05-25 14:04:56 -04:00
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
result_list = await command.function(event)
|
2020-05-25 14:04:56 -04:00
|
|
|
|
|
|
|
if not result_list:
|
|
|
|
return
|
|
|
|
|
|
|
|
articles = []
|
|
|
|
|
|
|
|
for result in result_list:
|
2020-05-24 12:10:54 -04:00
|
|
|
try:
|
2020-12-08 21:04:48 -05:00
|
|
|
articles += [
|
|
|
|
await builder.article(
|
|
|
|
title=result["title"],
|
|
|
|
description=result["description"],
|
|
|
|
text=result["text"],
|
|
|
|
link_preview=event.link_preview,
|
2023-02-09 12:28:06 -05:00
|
|
|
parse_mode=event.parse_mode,
|
|
|
|
buttons=result.get("buttons", None)
|
2020-12-08 21:04:48 -05:00
|
|
|
)
|
|
|
|
]
|
2020-05-24 12:10:54 -04:00
|
|
|
except:
|
2020-10-04 13:43:57 -04:00
|
|
|
print_exc()
|
2020-05-16 17:52:29 -04:00
|
|
|
|
2020-05-25 14:04:56 -04:00
|
|
|
try:
|
|
|
|
await event.answer([i for i in articles if i])
|
|
|
|
except:
|
2020-10-04 13:43:57 -04:00
|
|
|
print_exc()
|
2020-05-25 14:04:56 -04:00
|
|
|
|
2023-10-21 19:30:14 -04:00
|
|
|
async def handle_callback_query(self, event: ExtendedCallbackQuery):
|
2020-06-15 14:23:24 -04:00
|
|
|
data_str = event.data.decode("utf-8")
|
|
|
|
data_id = data_str.split("*")[0]
|
2023-10-12 18:06:32 -04:00
|
|
|
data_data = data_str.removeprefix(data_id + "*")
|
2020-06-15 14:23:24 -04:00
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
for command in self.callback_queries:
|
|
|
|
if command.data_id == data_id:
|
2020-10-03 17:56:46 -04:00
|
|
|
event.command = data_id
|
2020-06-15 14:23:24 -04:00
|
|
|
event.args = data_data
|
2020-09-15 20:02:44 -04:00
|
|
|
event.extra = command.extra
|
2020-09-18 20:39:45 -04:00
|
|
|
event.object = command
|
2023-10-12 22:01:43 -04:00
|
|
|
event.chat_db = None
|
2023-02-09 12:28:06 -05:00
|
|
|
|
|
|
|
if not event.via_inline:
|
2023-10-12 22:01:43 -04:00
|
|
|
event.chat_db = self.db.get_chat((await event.get_chat()).id)
|
2023-10-12 21:32:21 -04:00
|
|
|
|
2023-10-22 19:43:46 -04:00
|
|
|
if not (priv_resp := await self.check_privs(event, command, event.chat_db))[0]:
|
2023-10-12 21:32:21 -04:00
|
|
|
await event.answer(priv_resp[1])
|
|
|
|
continue
|
|
|
|
|
2020-06-15 14:23:24 -04:00
|
|
|
try:
|
2020-09-15 20:02:44 -04:00
|
|
|
await command.function(event)
|
2020-06-15 14:23:24 -04:00
|
|
|
except Exception as exception:
|
2020-09-15 20:02:44 -04:00
|
|
|
await event.reply(f"An error occurred in **{command.function.__name__}**: `{exception}`")
|
2020-10-04 13:43:57 -04:00
|
|
|
print_exc()
|
2020-06-15 14:23:24 -04:00
|
|
|
|
2020-05-25 14:04:56 -04:00
|
|
|
async def fallback_inline(self, event):
|
2020-06-16 17:00:09 -04:00
|
|
|
defaults_list = self.inline_photo_commands + self.inline_article_commands
|
2020-05-25 14:04:56 -04:00
|
|
|
|
|
|
|
try:
|
2020-09-15 20:02:44 -04:00
|
|
|
await event.answer([await event.builder.article(title=command.pattern, text=f"{self.loader.prefix()}{command.default}") for command in defaults_list if command.default])
|
2020-05-25 14:04:56 -04:00
|
|
|
except:
|
2020-10-04 13:43:57 -04:00
|
|
|
print_exc()
|
2020-05-25 14:04:56 -04:00
|
|
|
|
2020-05-21 13:53:11 -04:00
|
|
|
async def try_coro(self, coro):
|
2020-05-16 17:52:29 -04:00
|
|
|
try:
|
2020-05-21 13:53:11 -04:00
|
|
|
return await coro
|
2020-05-16 17:52:29 -04:00
|
|
|
except:
|
2020-05-21 13:53:11 -04:00
|
|
|
return
|
2020-06-09 21:05:53 -04:00
|
|
|
|
2023-10-22 19:43:46 -04:00
|
|
|
async def execute_command(self, event: ExtendedNewMessage, command: Command):
|
2020-07-18 15:11:09 -04:00
|
|
|
try:
|
2020-09-15 20:02:44 -04:00
|
|
|
if command.locking:
|
|
|
|
if command.lock_reason:
|
|
|
|
await event.reply(f"That command is currently locked: {command.lock_reason}")
|
2020-07-18 15:11:09 -04:00
|
|
|
return
|
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
if command.chance and randint(0, 100) <= command.chance or not command.chance:
|
2020-10-03 11:27:11 -04:00
|
|
|
command.lock_reason = f"In use by **{event.sender_id}** (`{event.raw_text}`)"
|
2020-09-15 20:02:44 -04:00
|
|
|
await command.function(event)
|
|
|
|
command.lock_reason = None
|
|
|
|
elif command.user_locking:
|
2020-10-03 11:27:11 -04:00
|
|
|
if event.sender_id in command.locked_users:
|
2021-02-11 10:47:08 -05:00
|
|
|
await event.reply("Please don't spam that command.")
|
2020-07-18 15:11:09 -04:00
|
|
|
return
|
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
if command.chance and randint(0, 100) <= command.chance or not command.chance:
|
2020-10-03 11:27:11 -04:00
|
|
|
command.locked_users.append(event.sender_id)
|
2020-09-15 20:02:44 -04:00
|
|
|
await command.function(event)
|
2020-10-03 11:27:11 -04:00
|
|
|
command.locked_users.remove(event.sender_id)
|
2023-10-21 20:57:02 -04:00
|
|
|
elif command.moderation:
|
|
|
|
if not event.chat.id in command.mod_cooldown_chats:
|
|
|
|
command.mod_cooldown_chats.append(event.chat.id)
|
2020-09-15 20:02:44 -04:00
|
|
|
await command.function(event)
|
2023-10-21 20:57:02 -04:00
|
|
|
await asyncio.sleep(MODERATION_COMMAND_COOLDOWN_SEC)
|
|
|
|
|
|
|
|
try:
|
|
|
|
command.mod_cooldown_chats.remove(event.chat.id)
|
|
|
|
except:
|
|
|
|
pass
|
2023-10-22 19:04:15 -04:00
|
|
|
elif command.chance:
|
|
|
|
if randint(0, 100) <= command.chance:
|
|
|
|
await command.function(event)
|
2023-10-21 20:57:02 -04:00
|
|
|
else:
|
|
|
|
await command.function(event)
|
2020-07-18 15:11:09 -04:00
|
|
|
except Exception as exception:
|
2020-09-15 20:02:44 -04:00
|
|
|
command.lock_reason = None
|
2020-07-18 15:11:09 -04:00
|
|
|
|
2020-10-03 11:27:11 -04:00
|
|
|
if event.sender_id in command.locked_users:
|
|
|
|
command.locked_users.remove(event.sender_id)
|
2020-07-18 15:11:09 -04:00
|
|
|
|
2021-06-22 08:49:37 -04:00
|
|
|
if not command.silent_bail:
|
|
|
|
try:
|
|
|
|
await event.reply(f"An error occurred in **{command.function.__name__}**: `{exception}`")
|
|
|
|
except:
|
|
|
|
pass
|
2020-10-04 13:11:27 -04:00
|
|
|
|
2020-10-04 13:43:57 -04:00
|
|
|
print_exc()
|
2020-07-18 15:11:09 -04:00
|
|
|
|
2023-10-12 21:32:21 -04:00
|
|
|
# returns True if the command can be used, False if not, and an optional error string together in a tuple
|
|
|
|
# for normal commands, this will be passed to event.reply; for callback queries this will call event.answer
|
2023-10-22 19:43:46 -04:00
|
|
|
async def check_privs(self, event, command: Command|CallbackQueryCommand, chat_db: ChatWrapper|None = None) -> tuple[bool, str|None]:
|
2020-08-30 19:09:57 -04:00
|
|
|
if self.is_blacklisted(event) and not self.is_owner(event) and not self.is_sudo(event):
|
2023-10-12 21:32:21 -04:00
|
|
|
return (False, None)
|
2020-08-30 19:09:57 -04:00
|
|
|
|
2023-10-22 19:43:46 -04:00
|
|
|
if isinstance(command, Command):
|
|
|
|
if event.chat and chat_db:
|
|
|
|
if command.nsfw and not chat_db.nsfw_enabled:
|
|
|
|
return (False, None if command.silent_bail else command.nsfw_warning or "NSFW commands are disabled in this chat!")
|
|
|
|
|
|
|
|
if command.fun and not chat_db.fun_enabled:
|
|
|
|
return (False, None)
|
|
|
|
|
|
|
|
if event.is_private and command.no_private:
|
2024-07-03 15:51:22 -04:00
|
|
|
return (False, None if command.silent_bail else "That command can't be used in private!")
|
|
|
|
|
|
|
|
if not event.is_private and command.private_only:
|
|
|
|
return (False, None if command.silent_bail else "That command can only be used in private!")
|
2021-03-23 20:01:27 -04:00
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
if command.owner and not self.is_owner(event):
|
2023-10-12 21:32:21 -04:00
|
|
|
return (False, None if command.silent_bail else "You lack the permissions to use that command!")
|
2020-06-12 14:20:18 -04:00
|
|
|
|
2020-09-15 20:02:44 -04:00
|
|
|
if command.sudo and not self.is_sudo(event) and not self.is_owner(event):
|
2023-10-12 21:32:21 -04:00
|
|
|
return (False, None if command.silent_bail else "You lack the permissions to use that command!")
|
2020-06-12 14:20:18 -04:00
|
|
|
|
2020-11-27 10:20:05 -05:00
|
|
|
if command.admin:
|
2020-11-30 23:38:03 -05:00
|
|
|
if event.chat and event.sender_id:
|
2020-11-27 10:20:05 -05:00
|
|
|
if event.is_private or not (await event.client.get_permissions(event.chat, event.sender_id)).is_admin and not self.is_sudo(event) and not self.is_owner(event):
|
2023-10-12 21:32:21 -04:00
|
|
|
return (False, None if command.silent_bail else "You lack the permissions to use that command!")
|
2021-06-22 16:13:20 -04:00
|
|
|
|
2023-10-12 21:32:21 -04:00
|
|
|
return (True, None)
|
2020-07-18 15:11:09 -04:00
|
|
|
|
2023-10-22 19:43:46 -04:00
|
|
|
def is_owner(self, event: ExtendedNewMessage|ExtendedInlineQuery) -> bool:
|
2021-03-23 19:16:21 -04:00
|
|
|
return str(event.sender_id) in self.settings.get_list("owner_id")
|
2020-07-18 15:11:09 -04:00
|
|
|
|
2023-10-22 19:43:46 -04:00
|
|
|
def is_sudo(self, event: ExtendedNewMessage|ExtendedInlineQuery) -> bool:
|
2023-10-12 21:54:01 -04:00
|
|
|
return event.sender_id in self.db.sudo_users
|
2020-07-18 15:11:09 -04:00
|
|
|
|
2023-10-22 19:43:46 -04:00
|
|
|
def is_blacklisted(self, event: ExtendedNewMessage|ExtendedInlineQuery) -> bool:
|
|
|
|
return event.query.user_id if isinstance(event, ExtendedInlineQuery) else event.sender_id in self.db.blacklisted_users
|