diff --git a/config.py b/config.py index c0f6854..783d36f 100644 --- a/config.py +++ b/config.py @@ -26,41 +26,35 @@ # # ######################################### - -class LanguageConfigEntry(ConfigEntry): - """docstring for LanguageConfigEntry""" - def __init__(self, bot, cfg_chan_id): - super().__init__() - self.bot = bot - self.cfg_chan_id = cfg_chan_id - - async def run(self, ctx): - try: - - except Exception as e: - raise e - - class MendatoryConfigEntries(ConfigEntry): """docstring for ClearanceConfigEntry""" def __init__(self, bot, cfg_chan_id): super().__init__(bot, cfg_chan_id) + def is_valid(self, lang): + if lang in ALLOWED_LANGS: + return True + else: + return False + + async def run(self, ctx): try: #LANGUAGE CONFIG + print("conf") good = False while not good: lang = await self.get_answer(ctx, f"I'm an international robot and tend to opperate in many places. This also means that I speak many language! The list of supported languages can be found on my website {WEBSITE}. So which do you want to speak with?") - if not self.is_valid(lang): + if not self.is_valid(lang.content): continue good = True - await ctx.send(f"You have selected {lang}. Glad you could find a language that suits you! If you think the translation is incomplete or could be improved, feel free to improve it. The translations are open to everyone on our {WEBSITE}.") + await ctx.send(f"You have selected {lang.content}. Glad you could find a language that suits you! If you think the translation is incomplete or could be improved, feel free to improve it. The translations are open to everyone on our {WEBSITE}.") with ConfigFile(ctx.guild.id) as conf: - conf["lang"] = lang + conf["lang"] = lang.content #ROLE CONFIG + await self.config_channel.send("Role setup is **mendatory** for the bot to work correctly. Otherwise no one will be able to use administration commands.") await self.config_channel.send("**\nStarting role configuration**\nThis bot uses two level of clearance for its commands.\nThe first one is the **manager** level of clearance. Everyone with a role with this clearance can use commands related to server management. This includes but is not limited to message management and issuing warnings.\nThe second level of clearance is **admin**. Anyone who has a role with this level of clearance can use all commands but the ones related to the bot configuration. This is reserved to the server owner. All roles with this level of clearance inherit **manager** clearance as well.") new_roles = [] @@ -154,9 +148,6 @@ async def init(self, ctx): await self.config_channels[ctx.guild.id].send("**Starting full bot configuration...**") try: - await self.config_channels[ctx.guild.id].send("Role setup is **mendatory** for the bot to work correctly. Otherwise no one will be able to use administration commands.") - #await ClearanceConfigEntry(self.bot, self.config_channel).run(ctx) - for cog in self.bot.cogs: if self.bot.cogs[cog].config_entry: await self.bot.cogs[cog].config_entry(self.bot, self.config_channel).run(ctx) diff --git a/exts/essentials.py b/exts/essentials.py index bb0f3fa..7eb8269 100644 --- a/exts/essentials.py +++ b/exts/essentials.py @@ -1,5 +1,6 @@ """Essential features all bot built with this template should have. Do note that disabling it will cause issues to the config extension.""" +import datetime import logging import discord from settings import * @@ -147,12 +148,20 @@ async def shutdown(self, ctx): async def clear(self, ctx, nbr: int): '''deletes specified number of messages in the current channel''' to_del = [] + now = datetime.datetime.now() async for msg in ctx.channel.history(limit=nbr+1): local_logger.info("Deleting {}".format(msg)) - to_del.append(msg) + if (msg.created_at - now).days <= -14: + await msg.delete() + else: + to_del.append(msg) try: await ctx.channel.delete_messages(to_del) + + except discord.HTTPException as e: + raise e + except Exception as e: local_logger.exception("Couldn't delete at least on of{}".format(to_del)) raise e diff --git a/exts/poll.py b/exts/poll.py index 97b135b..7f33e42 100644 --- a/exts/poll.py +++ b/exts/poll.py @@ -87,8 +87,8 @@ async def on_raw_reaction_add(self, payload): #getting poll_allowed_chans #@is_init - poll_allowed_chans = ConfigFile(payload.guild_id) - + with ConfigFile(payload.guild_id) as conf: + poll_allowed_chans = conf["poll_channels"] #checking that user isn't the bot if (payload.user_id != self.bot.user.id) and (payload.channel_id in poll_allowed_chans): @@ -134,7 +134,8 @@ async def on_raw_reaction_add(self, payload): async def on_raw_reaction_remove(self, payload): #getting poll_allowed_chans - poll_allowed_chans = ConfigFile(payload.guild_id) + with ConfigFile(payload.guild_id) as conf: + poll_allowed_chans = conf["poll_channels"] #fetching concerned message and the user who added the reaction message = await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id) @@ -144,26 +145,6 @@ async def on_raw_reaction_remove(self, payload): #changing color of the embed await self.balance_poll_color(message, message.reactions[0].count, message.reactions[2].count) - async def balance_poll_color(self, msg, for_count, against_count): - r = g = 128 - diff = for_count - against_count - votes = for_count + against_count - r -= (diff/votes)*64 - g += (diff/votes)*64 - - #checking whether the number is over 255 - r = int(min(255, r)) - g = int(min(255, g)) - - color = int((r*65536) + (g*256)) - #getting messages's embed (there should only be one) - embed = msg.embeds[0].copy() - embed.color = color - await msg.edit(embed=embed) - - return msg - - @commands.Cog.listener() async def on_message(self, message): if message.author==self.bot.user: return @@ -173,7 +154,9 @@ async def on_message(self, message): return #getting poll_allowed_chans - poll_allowed_chans = ConfigFile(message.guild.id) + #poll_allowed_chans = ConfigFile(message.guild.id)["poll_channels"] + with ConfigFile(message.guild.id) as conf: + poll_allowed_chans = conf["poll_channels"] if message.channel.id in poll_allowed_chans and message.content.startswith(PREFIX)!=True: @@ -188,7 +171,7 @@ async def on_message(self, message): #making embed embed_poll = discord.Embed( title = message.author.name, - description = message.content, + description = discord.utils.escape_mentions(message.content), colour = discord.Color(16776960), url = None ) @@ -207,6 +190,25 @@ async def on_message(self, message): except Exception as e: local_logger.exception("Couldn't send and delete all reaction") + async def balance_poll_color(self, msg, for_count, against_count): + r = g = 128 + diff = for_count - against_count + votes = for_count + against_count + r -= (diff/votes)*64 + g += (diff/votes)*64 + + #checking whether the number is over 255 + r = int(min(255, r)) + g = int(min(255, g)) + + color = int((r*65536) + (g*256)) + #getting messages's embed (there should only be one) + embed = msg.embeds[0].copy() + embed.color = color + await msg.edit(embed=embed) + + return msg + @commands.group() async def poll(self, ctx): diff --git a/settings.py b/settings.py index 2a9a03e..ef89a21 100644 --- a/settings.py +++ b/settings.py @@ -48,6 +48,7 @@ } HELP_TAB = " " +ALLOWED_LANGS = ["en"] #########################################