2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-30 14:08:31 +00:00

use setters and getters for some db functions, more event.prefix

This commit is contained in:
Nick80835
2021-03-24 16:16:13 -04:00
parent 42f2fb1c75
commit ea02b42c2c
5 changed files with 27 additions and 21 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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|) <board name>")
await event.reply(f"Syntax: {event.prefix}4c(f|) <board name>")
return
board = event.args.lower()

View File

@@ -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) <subreddit name>")
await event.reply(f"Syntax: {event.prefix}red(i|t|b) <subreddit name>")
return
if fetch_type == "i":

View File

@@ -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!")