I've encountered this problem with discord.py that happens at random for some reason

Viewed 69

I have a function where if you edit something the bot would type out before and after the edited sentence. I also have a command where it google searches question and return the first result from the google search in discord. But when I use these two function/commands together (they are on different files and I was running two files on PyCharm) it would return the result from google but then the function says it's edited when it clearly isn't. The weird thing is it only some times print out the "edited message" and sometime it just doesn't for some unknown reason which is the most confusing part. Also the "edited version" is exactly the same as the "non edited version".

Code for the reacting to edit function


@client.event
async def on_message_edit(before, after):

        await before.channel.send(
            f'Before: {before.content}\n' f'After: {after.content}'
    )

Code to the command function

@bot.command()
async def question(ctx,*, query):
        author = ctx.author.mention
        await ctx.channel.send(f"Here are the links related to your question {author} !")
        async with ctx.typing():
                for j in search(query, tld="co.in", num=1, stop=1, pause=1):
                        await ctx.send(f"\n {j}")

I tried to use the author function, but it just said un unresolved reference.


 import discord


from discord.ext import commands

id = id

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




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

    else:

        @bot.event
        async def on_message_edit( before, after):

                embed = discord.Embed(
                timestamp=after.created_at,
                description = f"<@!{before.author.id}>**'s message was edited in** <#{before.channel.id}>.",
                colour = discord.Colour(0x00FF00)
                )
                embed.set_author(name=f'{before.author.name}#{before.author.discriminator}', icon_url=before.author.avatar_url)
                embed.set_footer(text=f"Author ID:{before.author.id} • Message ID: {before.id}")
                embed.add_field(name='Before:', value=before.content + "\u200b", inline=False)
                embed.add_field(name="After:", value=after.content + "\u200b", inline=False)
                channel = bot.get_channel(channel)
                await channel.send(embed=embed)

bot.run('token')

I tried to use this method where if your id is a specific id then it won't return. It still doesn't work, any suggestions as to why?

1 Answers

From the docs:

The following non-exhaustive cases trigger this event:

  • A message has been pinned or unpinned.
  • The message content has been changed.
  • The message has received an embed.

Since this there are cases besides content change that you don't want you'll need to filter them. I'd suggest something like this:

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

Disclaimer: I didn't test this code and have never used this library. Good luck :)

Update: You question looks similar to this question

Also, if you'd rather filter on some other attribute, the discord.Message class has various attributes to use. The author attribute looks like one you may wish to use based on your comments.

Related