trs-k3tchup
trs-k3tchup

Reputation: 299

Bot responding to channel mentions

I've been trying to develop a discord bot in python recently. I made it so that if a message contains a certain number, it will react and send a message. Here is the code in the cog file:

import discord

from discord.ext import commands
    
nice_numbers = ['69', '420']
    
class Auto(commands.Cog):
    def __init__(self, client):
        self.client = client
      
    @commands.Cog.listener()
    async def on_message(self, message):
        
        msg = message.content
    
        if message.author == self.client.user:
             return
    
        if any (word in msg for word in nice_numbers):
             await message.add_reaction('đź‘Ś')
             await message.channel.send(f'lmao nice')
        
    
def setup(client):
    client.add_cog(Auto(client))

The problem is, the bot also responds with the same message and reaction when a user mentions a certain channel (in this case #general, #super-private-testing, and #resources). I can't seem to fix it or figure out why it's happening. I'm still pretty new to python so can someone please tell me what I'm doing wrong?

Upvotes: 3

Views: 126

Answers (2)

ZeroKnight
ZeroKnight

Reputation: 528

Expanding on Shunya's answer, you can use message.clean_content instead of message.content:

A property that returns the content in a “cleaned up” manner. This basically means that mentions are transformed into the way the client shows it. e.g. <#id> will transform into #name.

This will also transform @everyone and @here mentions into non-mentions.

This will prevent you from inadvertently matching against the IDs of channels, users, roles, etc. Of course, if the actual name contains a nice_number, it'll still match.

Upvotes: 2

Shunya
Shunya

Reputation: 2429

Basically what is happening is that mentions have a special syntax within the Discord API where they are basically a bunch of numbers put together.

For example when you are mentioning another user like the following:

Hello @User1234!

The real syntax within the discord message is the following:

Hello <@125342019199458000>!

And in the case of mentioning channels, it works similar, as a channel mentioned like:

#general

Internally would be written as:

<#550012071928922144>

Of course, the problem is that within this big number there could be false positive of finding your nice_numbers. There could be different ways to avoid this, for example you could check if a channel or a user is being mentioned in the message and return in that case.

if message.channel_mentions or message.mentions:
    return

I think a better solution would be to changing the way you are checking if the nice_numbers are within message.content.

Using if word in msg would return true if message.content also includes something like 'My favourite number is 45669'. To overcome this issue it is better to make use of regular expressions.

You can declare a new function like this answers explains, which would return a <match object> if what you pass as parameter is found.

It would be something like this:

import re

def findCoincidences(w):
    return re.compile(r'\b({0})\b'.format(w)).search

findCoincidences('69')('I like 69')    # -> return <match object>
findCoincidences('69')('My favourite number is 45669')                   # -> return None

Upvotes: 3

Related Questions