Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to make the discord bot to not react to a certain user when they edit a message, but it doesn't work

I am trying to make it so a discord bot won’t react to a certain user when they make say an edit on a message, however it doesn’t quite work. My current code is

from discord.ext import commands

id = id

bot = commands.Bot(command_prefix='!')




@bot.event
async def on_message(message):
    
    if message.author.id == id:
        pass

    else:

        @bot.event
        async def on_message_edit( before, after):
              await before.channel.send(
                    f'Before: {before.content}\n' f'After: {after.content}'


                

bot.run('token')

I have to do this because I just put if message.author.id == id in the on_message_edit it would return as false. I also tried to also use variables like if message.author.id == id then something = True, but it just won’t work. But this doesn’t quite work either, any suggestions as to how I could fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First of all, avoid naming strings like id since it’s used by discord.py already and can be confusing for python. I’ve changed it for banned_id just for this example.

Secondly, message.author.id is an integer value, so make sure you compare it to another integer value. If you make something like:

banned_id = 142345353455345

python treats it just like a string of characters, not a number. You can make it a number using int().

For me this code works just fine:

    @bot.event
    async def on_message_edit(before, after):
        banned_id = int(YOUR_ID_HERE)

        if before.author.id == banned_id:
            await before.channel.send("you wish")
        else:
            await before.channel.send(f'Before: {before.content}\n' f'After: {after.content}')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading