Skip to content
Advertisement

discord.py bot sends message each and finishes only when sending each words

I’m new in this, it’s really annoying that the bot would send each bad word one by one until it finishes. Here’s the code.

@client.event
async def on_message(message):
  if message.author == client.user:
      return
  if message.author.bot: 
      return

  with open('badwords.txt') as file:
    file = file.read().strip()

  for badwords in file:
    if badwords in message.content.lower():
       msg = await message.channel.send(f'{message.author.mention}Please avoid using malicious and insulting words in the future! words you used: {badwords}',delete_after=10)
       await msg.add_reaction('<a:cRight:819401530964836382>')
       await msg.add_reaction('<a:Heart:819401449095692309>')
       await msg.add_reaction('<a:cLeft:819403565440827423>')
       await message.channel.send('messages delete after 10 seconds',delete_after=5)
       guild = message.guild
       role = discord.utils.get(guild.roles, name="Muted")
       await message.author.add_roles(role)
       embed = discord.Embed(title="Muted!", description=f"{message.author.mention} Muted for 1 minute", colour=discord.Colour.blue())
       embed.add_field(name="reason",value=f"Badwords moderation, word that was used: {badwords}",inline=False)
       await message.channel.send(embed=embed,delete_after=300)
       await asyncio.sleep(60)
       await message.author.remove_roles(role)
       embed = discord.Embed(title="Unmuted", description=f"Unmuted - {message.author.mention} ", colour=discord.Colour.blue())
       await message.channel.send(embed=embed,delete_after=300)

  await client.process_commands(message)

I’m trying to make the bot send all the bad words in one message and as in the code, the bot would send a message and mute, but it would repeat based on how many bad words there are in the message and would also repeat muting the user who used bad words in her/his message. The code has no problems it works perfectly fine, I ask thee on changes to only make the bot send once no matter how many bad words in the message and to also list the bad words in the message that the bot sends. the picture of the problem

In the image it would repeat until it finishes sending all words, and I am completely clueless on what should I add or change to make the bot compile each bad word and would send the embed and mute the user who used the bad word

(ignore this it seems like stack overflow keeps telling me to add details….. I’m filling it up) I used the method open file in this code pls help and ps I’m new don’t say I’m bad and I’m still 14 learning on coding :) I would be really glad to someone who will help me :)

Advertisement

Answer

If you loop through your badwords file and store all the used words in a list, you can then afterwards send a message with all the collected bad words.

Something like this:

@client.event
async def on_message(message):
  if message.author == client.user:
      return
  if message.author.bot: 
      return

  with open('badwords.txt') as file:
    file = file.read().strip()

  found_words = list()

  for badword in file:
    if badword in message.content.lower():
      found_words.append(badword)

  msg_str = f'{message.author.mention}Please avoid using malicious and insulting words in the future! words you used: {", ".join(found_words)}'
  msg = await message.channel.send(msg_str,delete_after=10)
  await msg.add_reaction('<a:cRight:819401530964836382>')
  await msg.add_reaction('<a:Heart:819401449095692309>')
  await msg.add_reaction('<a:cLeft:819403565440827423>')
  await message.channel.send('messages delete after 10 seconds',delete_after=5)
  guild = message.guild
  role = discord.utils.get(guild.roles, name="Muted")
  await message.author.add_roles(role)
  embed = discord.Embed(title="Muted!", description=f"{message.author.mention} Muted for 1 minute", colour=discord.Colour.blue())
  embed.add_field(name="reason",value=f"Badwords moderation, word that was used: {", ".join(found_words)}",inline=False)
  await message.channel.send(embed=embed,delete_after=300)
  await asyncio.sleep(60)
  await message.author.remove_roles(role)
  embed = discord.Embed(title="Unmuted", description=f"Unmuted - {message.author.mention} ", colour=discord.Colour.blue())
  await message.channel.send(embed=embed,delete_after=300)

  await client.process_commands(message)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement