Search code examples
pythondiscord.pypython-3.9

How to change ID to Ping (PyCharm, Python 3.9)


I have made this snipe command (From a Stackoverflow question) but when the bot answers, the title says the ID of the author of the deleted message, not the ping or name. How do I change it?

Code:

snipe_message_content = None
snipe_message_author = None
snipe_message_id = None

@bot.event
async def on_message_delete(message):

    global snipe_message_content
    global snipe_message_author
    global snipe_message_id

    snipe_message_content = message.content
    snipe_message_author = message.author.id
    snipe_message_id = message.id
    await asyncio.sleep(60)

    if message.id == snipe_message_id:
        snipe_message_author = None
        snipe_message_content = None
        snipe_message_id = None

@bot.command()
async def snipe(message):
    if snipe_message_content is None:
        await message.channel.send("Theres nothing to snipe.")
    else:
        embed = discord.Embed(description=f"{snipe_message_content}")
        embed.set_footer(text=f"Asked by {message.author.name}#{message.author.discriminator}", icon_url=message.author.avatar_url)
        embed.set_author(name=f"<@{snipe_message_author}>")
        await message.channel.send(embed=embed)
        return

Solution

  • Unfortunately, you can't ping a user in the author part of an embed. However, you can say their name, so that is what we'll do.

    Author Name

    Getting the author name is as simple as changing the snipe_message_author = message.author.id (which gets the ID), to snipe_message_author = message.author.name.

    Displaying Author Name

    To display the author name with a nice format, remove the "<@ >" from where you display the name.

    Code

    @client.event
    async def on_message_delete(message):
    
        global snipe_message_content
        global snipe_message_author
        global snipe_message_id
    
        snipe_message_content = message.content
        snipe_message_author = message.author.name
        snipe_message_id = message.id
        await asyncio.sleep(60)
    
        if message.id == snipe_message_id:
            snipe_message_author = None
            snipe_message_content = None
            snipe_message_id = None
    
    @client.command()
    async def snipe(message):
        if snipe_message_content is None:
            await message.channel.send("Theres nothing to snipe.")
        else:
            embed = discord.Embed(description=f"{snipe_message_content}")
            embed.set_footer(text=f"Asked by {message.author.name}#{message.author.discriminator}", icon_url=message.author.avatar_url)
            embed.set_author(name=f"{snipe_message_author}")
            await message.channel.send(embed=embed)
            return
    

    Output

    enter image description here

    Edit

    Accidentally leaked my discord name and discriminator, changed discriminator to random int. Code still works though.