0
votes

I have a discord bot using the discord.py rewrite. One of my commands fetches the first Youtube video result for a given query. If a user deletes their message with the command, the bot deletes his response. That part works fine, but here it is for reference:

@bot.command()
async def yt(ctx):
    ytquery = urllib.parse.urlencode({"search_query" : ctx.message.content[4:]})
    html_cont = urllib.request.urlopen("http://youtube.com/results?"+ytquery)
    ytresult = re.findall(r'href=\"\/watch\?v=(.{11})', html_cont.read().decode())
    delcmd = await ctx.send("http://youtube.com/watch?v=" + ytresult[0])
    deletelog[ctx.message] = delcmd

deletelog={}

@bot.event
async def on_message_delete(message):
    if message in deletelog:
        dellog = deletelog[message]
        await dellog.delete()
        del deletelog[message]

But I've seen other bots that can also EDIT the message if the command message is edited. My friends in my server have demanded I figure out how to make my bot do this. I figure it should be pretty simple, just piggyback off the "deletelog" I made, and if the youtube command in that log is edited, we edit the response.

This is my first time using the edit command, and I read the docs but I can't get it to work and am not sure what I'm screwing up:

@bot.event
async def on_message_edit(before, after):
    print("test")
    if before in deletelog:
        print("test2")
        ytquery = urllib.parse.urlencode({"search_query": after.message.content[4:]})
        html_cont = urllib.request.urlopen("http://youtube.com/results?" + ytquery)
        ytresult = re.findall(r'href=\"\/watch\?v=(.{11})', html_cont.read().decode())
        delcmd = await before.edit(content=("http://youtube.com/watch?v=" + ytresult[0]))
        deletelog[after] = delcmd

That second test print, "test2" is never firing. So my bot isn't even detecting the "before" message in the deletelog, even though it should, right? Sorry if this is a stupid question, I just am not sure where I'm messing up.

1

1 Answers

0
votes

Use the id attribute of the messages instead of the Message object itself.

@bot.command()
async def yt(ctx):
    ytquery = urllib.parse.urlencode({"search_query" : ctx.message.content[4:]})
    html_cont = urllib.request.urlopen("http://youtube.com/results?"+ytquery)
    ytresult = re.findall(r'href=\"\/watch\?v=(.{11})', html_cont.read().decode())
    delcmd = await ctx.send("http://youtube.com/watch?v=" + ytresult[0])
    deletelog[ctx.message.id] = delcmd

deletelog={}

@bot.event
async def on_message_delete(message):
    if message.id in deletelog:
        dellog = deletelog[message.id]
        await dellog.delete()
        del deletelog[message.id]

@bot.event
async def on_message_edit(before, after):
    print("test")
    if before.id in deletelog:
        print("test2")
        ytquery = urllib.parse.urlencode({"search_query": after.message.content[4:]})
        html_cont = urllib.request.urlopen("http://youtube.com/results?" + ytquery)
        ytresult = re.findall(r'href=\"\/watch\?v=(.{11})', html_cont.read().decode())
        delcmd = await before.edit(content=("http://youtube.com/watch?v=" + ytresult[0]))
        deletelog[after.id] = delcmd