Skip to content

Commit a04dc8d

Browse files
committed
Añadida funcionalidad para que el bot no te haga ban a ti mismo cuando censuras palabras nuevas. Cambiado algunos detalles menores, mirar comentarios de cada cambio
1 parent ac11a5e commit a04dc8d

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/bot.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import discord
21
from discord.ext import commands
32
import os
43

4+
bot = commands.Bot(command_prefix='!')
55

6-
bot = commands.Bot(command_prefix='¡')
7-
8-
TOKEN = open('token.txt', 'r').read()
9-
6+
with open('token.txt', 'r') as token_file:
7+
TOKEN = token_file.read()
108

119
for file in os.listdir('./funcionalidades'):
1210
if file.endswith('.py'):

src/funcionalidades/ban_hammer.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
import discord
21
from discord.ext import commands
32

3+
PERMITTED_ROLES_NAMES = ('Junta', 'Admin')
4+
5+
6+
def setup(bot):
7+
bot.add_cog(BanHammer(bot))
8+
9+
10+
def have_permitted_rol(autor_roles):
11+
for autor_rol in autor_roles:
12+
if autor_rol.name in PERMITTED_ROLES_NAMES:
13+
return True
14+
return False
15+
416

517
class BanHammer(commands.Cog):
618

@@ -20,14 +32,27 @@ async def on_message(self, message):
2032
if message.author == self.bot.user:
2133
return
2234

35+
message_content = message.content
2336
forbidden_words_used = [
24-
i for i in self.blacklist if message.content.casefold().count(i) > 0]
37+
i for i in self.blacklist if message_content.casefold().count(i) > 0]
2538

26-
if(len(forbidden_words_used) > 0):
27-
await message.delete() # Delete the message
28-
# Send a message on the same channel
29-
await message.channel.send(message.author.mention + ", debes cuidar tu vocabulario, jovencito")
30-
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
39+
if len(forbidden_words_used) > 0:
40+
censor_command = self.bot.get_command('censor')
41+
censor_command_names = censor_command.aliases
42+
censor_command_names.append(censor_command.name)
43+
str_ = message_content.split()[0]
44+
# If one with permitted roles is baning a word, we dont ban his message
45+
if have_permitted_rol(message.author.roles) and str_[0] == self.bot.command_prefix \
46+
and str_[1:] in censor_command_names:
47+
pass
48+
else:
49+
await message.delete() # Delete the message
50+
# Send a message on the same channel
51+
await message.channel.send(message.author.mention + ", debes cuidar tu vocabulario, jovencito")
52+
# Send a private message to the user
53+
await message.author.send("El mensaje \n" + str(f'```diff\n-"{message_content}"```') +
54+
"no se ajusta a las normas, en los próximos mensajes no uses: " +
55+
str(forbidden_words_used).strip('[]'))
3156

3257
@commands.command(name='censor')
3358
@commands.has_role('Junta')
@@ -36,7 +61,7 @@ async def ban_word(self, ctx):
3661
if word in self.blacklist:
3762
await ctx.send("La palabra ya estaba baneada")
3863
else:
39-
with open("blacklist_insultos.txt", "a+") as file:
64+
with open("blacklist_insultos.txt", "a") as file:
4065
file.write("\n" + word)
4166
self.blacklist.append(word)
4267
await ctx.send('Palabra censurada correctamente :)')
@@ -59,7 +84,3 @@ async def unban_word(self, ctx):
5984
async def on_command_error(self, ctx, error):
6085
if isinstance(error, commands.errors.CheckFailure):
6186
await ctx.send('Lo siento, no es nada personal, pero no tienes permiso para hacer eso :)')
62-
63-
64-
def setup(bot):
65-
bot.add_cog(BanHammer(bot))

0 commit comments

Comments
 (0)