From 94597b45ad6a89c2bcb28d580c4ab3c249644294 Mon Sep 17 00:00:00 2001 From: Siphalor Date: Tue, 19 Jan 2021 11:34:46 +0100 Subject: [PATCH] Make last och time guild specific --- bot.py | 23 +++++++++++++++-------- lib/config.py | 8 ++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index 83d8f2f..20ee8df 100644 --- a/bot.py +++ b/bot.py @@ -10,7 +10,8 @@ import gtts from discord.ext import commands from dotenv import load_dotenv -from lib.config import config, config_meta, config_load, config_save, config_get, config_set, config_get_descriptions +from lib.config import config, config_meta, config_load, config_save, config_get, config_set, config_get_descriptions, \ + config_set_raw from lib.utils import async_filter load_dotenv() @@ -44,8 +45,6 @@ OWNER_ID = 327126546970312739 OCH_LOEH_SOUND = "assets/och_loeh.mp3" -last_och = 0 - config_load() bot = commands.Bot(command_prefix=config.get('prefix')) @@ -60,6 +59,14 @@ async def _get_loeh(guild: discord.Guild) -> Optional[discord.Member]: return None +def _get_last_och_time(guild: discord.Guild) -> int: + return config_get('last-och-time', guild.id) + + +def _set_last_och_time(guild: discord.Guild): + config_set_raw('last-och-time', time.time(), guild.id) + + @bot.event async def on_ready(): print(f'{bot.user} has connected to Discord!') @@ -67,17 +74,17 @@ async def on_ready(): @bot.event async def on_message(message: discord.Message): - global last_och if message.guild is not None and not message.author.bot: if config_get('loeh-enable', message.guild.id) and LOEH_REGEX.match(message.content): if message.author.id == LOEH_ID: await message.channel.send("https://siphalor.de/img/spidy-is-that-you.jpg") else: t = time.time() - if t - config_get('och-cooldown', message.guild.id) <= last_och: + if t - config_get('och-cooldown', message.guild.id) <= _get_last_och_time(message.guild): await message.channel.send('429: Too many requests') return - last_och = t + _set_last_och_time(message.guild) + loeh = await _get_loeh(message.guild) if loeh is None: await message.channel.send('404: Löh not found!') @@ -107,10 +114,10 @@ async def on_message(message: discord.Message): elif config_get('och-anyone-enable', message.guild.id) and ( match := OCH_ANYONE_REGEX.search(message.content)) is not None: t = time.time() - if t - config_get('och-cooldown', message.guild.id) <= last_och: + if t - config_get('och-cooldown', message.guild.id) <= _get_last_och_time(): await message.channel.send('429: Too many requests') return - last_och = t + _set_last_och_time() guild: discord.Guild = message.guild bot_member: discord.Member = guild.get_member(bot.user.id) diff --git a/lib/config.py b/lib/config.py index 75c4cb1..ec4c841 100644 --- a/lib/config.py +++ b/lib/config.py @@ -78,6 +78,14 @@ def config_set(key: str, value, guild: Optional[int] = None) -> str: return 'This config can only be changed globally by the bot owner!' +def config_set_raw(key: str, value, guild: Optional[int] = None): + if guild is None: + config[key] = value + else: + _get_guild_config(guild)[key] = value + config_save() + + def config_get_descriptions() -> iter: return map(lambda entry: (entry[0], entry[1]), config_meta.items())