pythondiscord.pyreply

How can I read the message that someone replied to in discord.py?


I am new to making discord bots with Discord.py Is there any way to get the message characters of a message that someone replied to? As in, read the message that someone replied to; not the actual message of the reply.

Read what's in the red box after replying


Solution

  • Yes you can. For this, you'll have to use Message.reference to get the discord.Message object of the message you replied to.

    In this case, you would have to use ctx.message.reference which returns a discord.MessageReference object.

    using the message_id attribute you can get the id from the message you replied to.

    You would have to use await ctx.channel.fetch_message(ctx.message.reference.message_id) to get a full discord.Message object, which has attribute content

    Full code would look like this:

    @bot.command()
    async def foo(ctx):
        message = await ctx.channel.fetch_message(ctx.message.reference.message_id)
        await ctx.send(message.content)
    

    Here's the result:
    enter image description here