From ea02b42c2c43fe38162cee3bb65969d1c3fdc25d Mon Sep 17 00:00:00 2001 From: Nick80835 Date: Wed, 24 Mar 2021 16:16:13 -0400 Subject: [PATCH] use setters and getters for some db functions, more event.prefix --- ubot/command_handler.py | 8 ++++---- ubot/database.py | 14 ++++++++++---- ubot/modules/fourchan.py | 2 +- ubot/modules/reddit.py | 2 +- ubot/modules/system.py | 22 +++++++++++----------- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/ubot/command_handler.py b/ubot/command_handler.py index a85897c..e893da6 100644 --- a/ubot/command_handler.py +++ b/ubot/command_handler.py @@ -37,7 +37,7 @@ class CommandHandler(): async def handle_incoming(self, event): chat_db = ChatWrapper(self.db.get_chat(event.chat.id)) - chat_prefix = chat_db.get_prefix() + chat_prefix = chat_db.prefix for command in self.incoming_commands: if command.simple_pattern: @@ -57,7 +57,7 @@ class CommandHandler(): return if command.pass_nsfw: - event.nsfw_disabled = not chat_db.nsfw_enabled() + event.nsfw_disabled = not chat_db.nsfw_enabled if command.raw_pattern: event.command = command.pattern @@ -283,12 +283,12 @@ class CommandHandler(): print(f"Attempted admin command ({event.raw_text}) from ID {event.sender_id}") return False - if event.chat and command.nsfw and not chat_db.nsfw_enabled(): + if event.chat and command.nsfw and not chat_db.nsfw_enabled: await event.reply(command.nsfw_warning or "NSFW commands are disabled in this chat!") print(f"Attempted NSFW command ({event.raw_text}) in blacklisted chat ({event.chat.id}) from ID {event.sender_id}") return False - if event.chat and command.fun and not chat_db.fun_enabled(): + if event.chat and command.fun and not chat_db.fun_enabled: print(f"Attempted fun command ({event.raw_text}) in blacklisted chat ({event.chat.id}) from ID {event.sender_id}") return False diff --git a/ubot/database.py b/ubot/database.py index f045679..7c5c9be 100644 --- a/ubot/database.py +++ b/ubot/database.py @@ -42,26 +42,32 @@ class ChatWrapper(): self.chat = chat # custom prefix functions - def get_prefix(self) -> str: + @property + def prefix(self) -> str: return self.chat.custom_prefix - def set_prefix(self, prefix: str): + @prefix.setter + def prefix(self, prefix: str): self.chat.custom_prefix = prefix self.chat.save() # fun command functions + @property def fun_enabled(self) -> bool: return self.chat.fun_enabled - def set_fun(self, enabled: bool): + @fun_enabled.setter + def fun_enabled(self, enabled: bool): self.chat.fun_enabled = enabled self.chat.save() # nsfw command functions + @property def nsfw_enabled(self) -> bool: return self.chat.nsfw_enabled - def set_nsfw(self, enabled: bool): + @nsfw_enabled.setter + def nsfw_enabled(self, enabled: bool): self.chat.nsfw_enabled = enabled self.chat.save() diff --git a/ubot/modules/fourchan.py b/ubot/modules/fourchan.py index 8005bda..ce8e932 100644 --- a/ubot/modules/fourchan.py +++ b/ubot/modules/fourchan.py @@ -12,7 +12,7 @@ NSFW_BOARDS = ['aco', 'b', 'bant', 'd', 'e', 'f', 'gif', 'h', 'hc', 'hm', 'hr', @ldr.add("4c", pattern_extra="(f|)", userlocking=True, pass_nsfw=True, help="Fetches images from 4chan, requires a board name as an argument.") async def fourchan(event): if not event.args: - await event.reply(f"Syntax: {ldr.prefix()}4c(f|) ") + await event.reply(f"Syntax: {event.prefix}4c(f|) ") return board = event.args.lower() diff --git a/ubot/modules/reddit.py b/ubot/modules/reddit.py index 167d666..69b391a 100644 --- a/ubot/modules/reddit.py +++ b/ubot/modules/reddit.py @@ -120,7 +120,7 @@ async def redimg(event): fetch_type = event.command[-1] if not sub: - await event.reply(f"Syntax: {ldr.prefix()}red(i|t|b) ") + await event.reply(f"Syntax: {event.prefix}red(i|t|b) ") return if fetch_type == "i": diff --git a/ubot/modules/system.py b/ubot/modules/system.py index b29ff6e..30e973d 100644 --- a/ubot/modules/system.py +++ b/ubot/modules/system.py @@ -45,7 +45,7 @@ async def help_cmd(event): help_string = "\n".join([f"**{module}**: {', '.join(pattern_list)}" for module, pattern_list in help_dict.items()]) - prefix_help = f"**Bot prefix:** {ldr.prefix()}\n**Group prefix:** {event.chat_db.get_prefix()}\n\n" + prefix_help = f"**Bot prefix:** {ldr.prefix()}\n**Group prefix:** {event.chat_db.prefix}\n\n" await event.reply(f"{prefix_help}**Available commands:**\n\n{help_string}") @@ -53,14 +53,14 @@ async def help_cmd(event): @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 `{event.chat_db.get_prefix()}` and this bot will always respond to `{ldr.prefix()}`") + await event.reply(f"With this command you can set a custom prefix to replace `/`, the current prefix is `{event.chat_db.prefix}` and this bot will always respond to `{ldr.prefix()}`") return if len(event.args) > 3: await event.reply("Custom prefixes must be at most 3 characters long!") return - event.chat_db.set_prefix(event.args) + event.chat_db.prefix = event.args await event.reply(f"Successfully set this groups prefix to `{event.args}`!") @@ -150,36 +150,36 @@ async def show_disabled(event): @ldr.add("nsfw", admin=True, help="Enables or disables NSFW commands for a chat, requires admin.") async def nsfw_toggle(event): if event.args.lower() not in ("on", "off"): - if event.chat_db.nsfw_enabled(): + if event.chat_db.nsfw_enabled: current_config = 'On' else: current_config = 'Off' - await event.reply(f"Syntax: {ldr.prefix()}nsfw (on|off)\nCurrent config for this chat: {current_config}") + await event.reply(f"Syntax: {event.prefix}nsfw (on|off)\nCurrent config for this chat: {current_config}") return if event.args == "on": - event.chat_db.set_nsfw(True) + event.chat_db.nsfw_enabled = True await event.reply("NSFW commands enabled for this chat!") elif event.args == "off": - event.chat_db.set_nsfw(False) + event.chat_db.nsfw_enabled = False await event.reply("NSFW commands disabled for this chat!") @ldr.add("fun", admin=True, help="Enables or disables fun commands for a chat, requires admin.") async def fun_toggle(event): if event.args.lower() not in ("on", "off"): - if event.chat_db.fun_enabled(): + if event.chat_db.fun_enabled: current_config = 'On' else: current_config = 'Off' - await event.reply(f"Syntax: {ldr.prefix()}fun (on|off)\nCurrent config for this chat: {current_config}") + await event.reply(f"Syntax: {event.prefix}fun (on|off)\nCurrent config for this chat: {current_config}") return if event.args.lower() == "on": - event.chat_db.set_fun(True) + event.chat_db.fun_enabled = True await event.reply("Fun commands enabled for this chat!") elif event.args.lower() == "off": - event.chat_db.set_fun(False) + event.chat_db.fun_enabled = False await event.reply("Fun commands disabled for this chat!")