Skip to content
Advertisement

Bot will not log an automod event if user has blocked the bot

If someone says a bad word that triggers the automod, the bot will delete the message, DM the user, and log it in a log channel.

The problem is if someone blocks the bot and says a bad word, the bot can’t DM the user which doesn’t allow the bot to log the event in a log channel.

I tried multiple ways to fix this by adding if and else and except but those do not help. Below is the current code that I have already, so how can I make the bot still log the event if the offender has blocked the bot?

@commands.Cog.listener()
async def on_message(self, message):
 
  curseWord = ['bad words here']
  msg_content = message.content.lower()  

  if any(word in msg_content for word in curseWord):
    if message.channel.type == discord.ChannelType.private:
      return
    if message.author.id == 330988962162147329: #johnny
      return
    if message.author.id == 467715040087244800: #me
      return
    if message.author.id == 261578097978114050: #devvy
      return
    if message.author.id == 835307493558321172: #examplebot
      return

    await message.delete()
    embed=discord.Embed(title="No No Word", description=f"{message.author.mention}, Hey! Those words arent allowed here!", color=0x00FFFF)
    embed.timestamp = datetime.datetime.utcnow()
    author = message.author
    pfp = author.avatar_url
    embed.set_author(name=f"{author.name}", icon_url=pfp)
    await message.channel.send(embed=embed)
    dmembed=discord.Embed(title="AutoMod", description="You were caught saying a bad word!", color=0x00FFFF)
    dmembed.add_field(name="**Message:**", value=f"{msg_content}", inline=False)
    pfp = author.avatar_url
    dmembed.add_field(name="**Server:**", value=f"{message.guild.name}", inline=False)
    dmembed.set_author(name=f"{author.name}", icon_url=pfp)
    dmembed.timestamp = datetime.datetime.utcnow()
    with open('logchannel.json', 'r', encoding='utf-8') as fp:
      log_channel = json.load(fp)

    try:
      await message.author.send(embed=dmembed)
      if log_channel:
        log_channel = message.guild.get_channel(log_channel[str(message.guild.id)])
        logembed=discord.Embed(title="Bot Log", description="Bad Word Said", color=0x00FFFF)
        logembed.add_field(name="**Message:**", value=f"{msg_content}", inline=False)
        logembed.add_field(name="**Member:**", value=f"{message.author.name}", inline=False)
        author = message.author
        pfp = author.avatar_url
        logembed.set_author(name=f"{author}", icon_url=pfp)
        logembed.timestamp = datetime.datetime.utcnow()
        await log_channel.send(embed=logembed)
      else:
        await log_channel.send(embed=logembed)
    except (AttributeError, KeyError):
      await log_channel.send(embed=logembed)

Advertisement

Answer

Try/except

When you have a try/except in your code, Python will try to execute the code, and if there is and error he will execute the code in the except section. So a code in the try section may be not executed.

What in your code needs a try/except ?

In your code, the things which could raise an error are :

await message.author.send(embed=dmembed) #DM closed
log_channel = ...(log_channel[str(message.guild.id)]) #key error
await log_channel.send(embed=logembed) #NoneType has no attribute send (if the channel doesn't exist)

Which means that if there is an error on the first possibility, all the following code in the try will not be executed.

Solution

I advise you to have two try/except, the first for the DM, and the second for the log. You code with my solution is now :

    try:
      await message.author.send(embed=dmembed)
    except:
        pass #ignore error if DM are closed
    try:
      if log_channel:
        log_channel = message.guild.get_channel(log_channel[str(message.guild.id)])
        logembed=discord.Embed(title="Bot Log", description="Bad Word Said", color=0x00FFFF)
        logembed.add_field(name="**Message:**", value=f"{msg_content}", inline=False)
        logembed.add_field(name="**Member:**", value=f"{message.author.name}", inline=False)
        author = message.author
        pfp = author.avatar_url
        logembed.set_author(name=f"{author}", icon_url=pfp)
        logembed.timestamp = datetime.datetime.utcnow()
        await log_channel.send(embed=logembed)
      else: #You can remove that, because if `log_channel` is not, you can't send an embed
        await log_channel.send(embed=logembed) #So you can also remove that
    except (AttributeError, KeyError):
      await log_channel.send(embed=logembed) #Same here, if the above code raises an error, this code can be executed without raising another error.
      pass
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement