Sebastian Illi
Sebastian Illi

Reputation: 13

Discord py Error in on_delete_message -> Unknown Message

I'm making a bot for my discord channel and I got an error in the on_delete_message function.

The weird thing is I got basically the same thing in a different function: on_reaction_clear, and it seems to be fine.

The problem was thrown in the async for user in reaction.users(): line and only in the on_reaction_delete function not in the one above

@client.event
async def on_reaction_clear(message, reactions):
    global con

    cur = con.cursor()

    for reaction in message.reactions:
        async for user in reaction.users():
            cur.execute('SELECT count FROM reactions WHERE emoji = "{0}" AND name = "{1}" AND channel = "{2}"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
            
            count = cur.fetchone()
            if (count[0] > 0):
                cur.execute('UPDATE reactions SET count = count - 1 WHERE emoji = "{0}" AND name = "{1}" AND channel = "{2}"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
            else:
                cur.execute('DELETE FROM reactions WHERE emoji = "{0}" AND name = "{1}" AND channel = "{2}"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))


@client.event
async def on_message_delete(message):
    global con

    cur = con.cursor()

    for reaction in message.reactions:
        async for user in reaction.users():
            cur.execute('SELECT count FROM reactions WHERE emoji = "{0}" AND name = "{1}" AND channel = "{2}"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
            
            count = cur.fetchone()
            if (count[0] > 0):
                cur.execute('UPDATE reactions SET count = count - 1 WHERE emoji = "{0}" AND name = "{1}" AND channel = "{2}"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
            else:
                cur.execute('DELETE FROM reactions WHERE emoji = "{0}" AND name = "{1}" AND channel = "{2}"'.format(str(reaction.emoji), str(user.id), str(reaction.channel_id)))
    
    print("done")

the error message:

Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "asd" is not found
Ignoring exception in on_message_delete
Traceback (most recent call last):
  File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\seb82\source\reaction-counter-bot\reaction-counter-bot.py", line 145, in on_message_delete
    async for user in reaction.users():
  File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\iterators.py", line 86, in __anext__
    msg = await self.next()
  File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\iterators.py", line 139, in next
    await self.fill_users()
  File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\iterators.py", line 154, in fill_users
    data = await self.getter(self.channel_id, self.message.id, self.emoji, retrieve, after=after)
  File "C:\Users\seb82\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 243, in request
    raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message

Hope you guys can help me out :)

Upvotes: 1

Views: 664

Answers (1)

chris
chris

Reputation: 884

tl;dr: Solution: Use on_reaction_add and save all reactions in your database, use that database when a message is deleted to do the stuff you tried to do before.


Hey as Lukasz explained before, you can't fetch the reactions form a deleted message.

The async and await keywords indicate that your bot tries to get new information from the discord severs.
Like "Hey discord, please show me al reactions to this message" and discord answers: "This does not exist, sorry bro, here is a 404 in exchange" ;)

This works for reactions because the deleted reaction is sent to you when the function is called
-> You don't need to request this separately.

My suggestion is the following:
Since you've already got sql involved use the on_reaction_add listener to save each reaction (with the information you need) in a table 'mapped' to its message id.
If a message gets deleted (on_message_delete) simply search for all entries in your table that are related to this message and do the things you tried to do before.

P.S. please be careful with 'raw string magic' sql statements in your code.
sql injections are a thing, you know :)

Upvotes: 1

Related Questions