2
0
mirror of https://github.com/Nick80835/microbot synced 2025-09-02 15:35:43 +00:00

add custom prefixes

This commit is contained in:
Nick80835
2021-03-23 20:01:27 -04:00
parent 5986870479
commit b47088fe4b
5 changed files with 43 additions and 5 deletions

View File

@@ -27,6 +27,7 @@ class Command:
self.chance = args.get("chance", None)
self.fun = args.get("fun", False)
self.not_disableable = args.get("no_disable", False) or self.owner or self.sudo or self.admin
self.no_private = args.get("no_private", False)
class InlinePhotoCommand:

View File

@@ -23,6 +23,7 @@ class CommandHandler():
self.settings = settings
self.loader = loader
self.db = loader.db
self.hard_prefix = self.settings.get_list("hard_cmd_prefix") or ["/"]
client.add_event_handler(self.report_incoming_excepts, events.NewMessage(incoming=True, forwards=False, func=lambda e: e.raw_text))
client.add_event_handler(self.handle_inline, events.InlineQuery())
client.add_event_handler(self.handle_callback_query, events.CallbackQuery())
@@ -35,8 +36,16 @@ class CommandHandler():
async def handle_incoming(self, event):
prefix = "|".join([escape(i) for i in (self.settings.get_list("cmd_prefix") or ['.'])])
chat_prefix = self.db.get_prefix(event.chat.id)
for command in self.incoming_commands:
if command.not_disableable:
prefix_list = self.hard_prefix + [chat_prefix] + ["/"]
else:
prefix_list = self.hard_prefix + [chat_prefix]
prefix = "|".join([escape(i) for i in prefix_list])
if command.simple_pattern:
pattern_match = search(self.simple_pattern_template.format(command.pattern + command.pattern_extra), event.raw_text, IGNORECASE|DOTALL)
elif command.raw_pattern:
@@ -252,6 +261,10 @@ class CommandHandler():
print(f"Attempted command ({event.raw_text}) from blacklisted ID {event.sender_id}")
return False
if command.no_private and event.is_private:
await event.reply("That command can't be used in private!")
return False
if command.owner and not self.is_owner(event):
await event.reply("You lack the permissions to use that command!")
print(f"Attempted owner command ({event.raw_text}) from ID {event.sender_id}")

View File

@@ -23,6 +23,7 @@ class Chat(BaseDB):
fun_enabled = BooleanField(default=True)
nsfw_enabled = BooleanField(default=True)
disabled_commands = TextField(default="[]")
custom_prefix = TextField(default="/")
DATABASE.connect()
@@ -46,6 +47,15 @@ class Database():
chat.save()
return chat
# custom prefix functions
def get_prefix(self, chat_id: int) -> str:
return self.get_chat(chat_id).custom_prefix
def set_prefix(self, chat_id: int, prefix: str):
chat = self.get_chat(chat_id)
chat.custom_prefix = prefix
chat.save()
# fun command functions
def fun_enabled(self, chat_id: int) -> bool:
return self.get_chat(chat_id).fun_enabled

View File

@@ -136,7 +136,7 @@ class Loader():
return await self.client.loop.run_in_executor(self.thread_pool, partial(function, *args))
def prefix(self):
return (self.settings.get_list('cmd_prefix') or ['.'])[0]
return ", ".join(self.settings.get_list("hard_cmd_prefix") or ["/"])
def _find_all_modules(self):
module_paths = glob.glob(dirname(__file__) + "/modules/*.py")

View File

@@ -4,7 +4,7 @@ from telethon import Button
from ubot import ldr
@ldr.add("del", help="Deletes messages from this bot, it's a safety feature.")
@ldr.add("del", no_disable=True, help="Deletes messages from this bot, it's a safety feature.")
async def delete_message(event):
message_to_delete = await event.get_reply_message()
@@ -12,16 +12,16 @@ async def delete_message(event):
await message_to_delete.delete()
@ldr.add("start", help="A start command to start the bot so you know what this bot is capable of when you start it, dumbass.")
@ldr.add("start", no_disable=True, help="A start command to start the bot so you know what this bot is capable of when you start it, dumbass.")
async def start_cmd(event):
await event.reply(
f"Hi I'm {ldr.settings.get_config('bot_name') or 'μBot'}, use {ldr.prefix()}help to see what commands I have!",
f"Hi I'm {ldr.settings.get_config('bot_name') or 'μBot'}, use /help to see what commands I have!",
buttons=[Button.url("Creator", "https://t.me/Nick80835"), Button.url("Source", "https://github.com/Nick80835/microbot/tree/bot")],
link_preview=False
)
@ldr.add("help")
@ldr.add("help", no_disable=True)
async def help_cmd(event):
if event.args:
for command in ldr.command_handler.incoming_commands:
@@ -49,6 +49,20 @@ async def help_cmd(event):
await event.reply(f"**Available commands:**\n\n{help_string}")
@ldr.add("prefix", admin=True, no_private=True)
async def set_group_prefix(event):
if not event.args:
await event.reply(f"With this command you can set a custom prefix to replace `/`, the current prefix is `{ldr.db.get_prefix(event.chat.id)}` and this bot will always respond to `{ldr.prefix()}`")
return
if len(event.args) > 2:
await event.reply("Custom prefixes must be 1 or 2 characters long!")
return
ldr.db.set_prefix(event.chat.id, event.args)
await event.reply(f"Successfully set this groups prefix to `{event.args}`!")
@ldr.add("sudohelp", sudo=True)
async def sudohelp(event):
if event.args: