diff --git a/src/bot.py b/src/bot.py index bfa1970..8565d92 100644 --- a/src/bot.py +++ b/src/bot.py @@ -1,12 +1,10 @@ -import discord from discord.ext import commands import os +bot = commands.Bot(command_prefix='!') -bot = commands.Bot(command_prefix='¡') - -TOKEN = open('token.txt', 'r').read() - +with open('token.txt', 'r') as token_file: + TOKEN = token_file.read() for file in os.listdir('./funcionalidades'): if file.endswith('.py'): diff --git a/src/funcionalidades/ban_hammer.py b/src/funcionalidades/ban_hammer.py index 0bcdcb9..9fa937b 100644 --- a/src/funcionalidades/ban_hammer.py +++ b/src/funcionalidades/ban_hammer.py @@ -1,6 +1,18 @@ -import discord from discord.ext import commands +PERMITTED_ROLES_NAMES = ('Junta', 'Admin') + + +def setup(bot): + bot.add_cog(BanHammer(bot)) + + +def have_permitted_rol(autor_roles): + for autor_rol in autor_roles: + if autor_rol.name in PERMITTED_ROLES_NAMES: + return True + return False + class BanHammer(commands.Cog): @@ -20,14 +32,27 @@ async def on_message(self, message): if message.author == self.bot.user: return + message_content = message.content forbidden_words_used = [ - i for i in self.blacklist if message.content.casefold().count(i) > 0] + i for i in self.blacklist if message_content.casefold().count(i) > 0] - if(len(forbidden_words_used) > 0): - await message.delete() # Delete the message - # Send a message on the same channel - await message.channel.send(message.author.mention + ", debes cuidar tu vocabulario, jovencito") - await message.author.send("El mensaje \n" + str(""f"```css\n{message.content}```""") + "no se ajusta a las normas, en los próximos mensajes no uses: " + str(forbidden_words_used).strip('[]')) # Send a private message to the user + if len(forbidden_words_used) > 0: + censor_command = self.bot.get_command('censor') + censor_command_names = censor_command.aliases + censor_command_names.append(censor_command.name) + str_ = message_content.split()[0] + # If one with permitted roles is baning a word, we dont ban his message + if have_permitted_rol(message.author.roles) and str_[0] == self.bot.command_prefix \ + and str_[1:] in censor_command_names: + pass + else: + await message.delete() # Delete the message + # Send a message on the same channel + await message.channel.send(message.author.mention + ", debes cuidar tu vocabulario, jovencito") + # Send a private message to the user + await message.author.send("El mensaje \n" + str(f'```diff\n-"{message_content}"```') + + "no se ajusta a las normas, en los próximos mensajes no uses: " + + str(forbidden_words_used).strip('[]')) @commands.command(name='censor') @commands.has_role('Junta') @@ -36,7 +61,7 @@ async def ban_word(self, ctx): if word in self.blacklist: await ctx.send("La palabra ya estaba baneada") else: - with open("blacklist_insultos.txt", "a+") as file: + with open("blacklist_insultos.txt", "a") as file: file.write("\n" + word) self.blacklist.append(word) await ctx.send('Palabra censurada correctamente :)') @@ -59,7 +84,3 @@ async def unban_word(self, ctx): async def on_command_error(self, ctx, error): if isinstance(error, commands.errors.CheckFailure): await ctx.send('Lo siento, no es nada personal, pero no tienes permiso para hacer eso :)') - - -def setup(bot): - bot.add_cog(BanHammer(bot))