How can I modify a Discord bot's last sent message using a user command?

I’m working on a Discord bot using discord.py and I need help with message editing functionality. My bot posts a list of available countries for a strategy game, and when users type commands like !claim (Country Name), I want the bot to update its original message.

The issue is that I can’t use fetch_message(ID) because the message gets deleted and recreated for each new game session, so the ID keeps changing. I need the command to automatically find the most recent list message and edit it.

Here’s what I have so far:

import discord
from discord import app_commands
from discord.ext import commands
from discord.utils import get

intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
tree = app_commands.CommandTree

@bot.event
async def on_ready():
    print("Bot is online")
    try:
        synced = await tree.sync(guild=discord.Object(id=1210361385112961024))
        print(f"Synced {len(synced)} commands")
    except Exception as e:
        print(e)

@bot.command()
async def create_list(ctx):
    msg = await ctx.send("Available Nations: :flag_us: United States")

@bot.command()
async def claim(ctx):
    target_channel = bot.get_channel(CHANNEL_ID)
    # Need to find the bot's previous message without using specific ID
    # since message ID changes when recreated
    await target_msg.edit(content="Available Nations: :flag_us: United States :flag_jp: Japan")

How can I make the !claim command locate and modify the bot’s most recent message automatically?

The easiest way to handle this is to store the message object in a variable when you first create it, and then refer to that variable in your claim command. You can modify your code to keep track of the last message your bot sent.

In your create_list function, store the message globally or use a dictionary if you’re supporting multiple servers. For example, you could declare global last_list_message and then set last_list_message = await ctx.send(...). In your claim command, use await last_list_message.edit(content=new_content).

If you need this to persist through bot restarts, consider iterating through the channel’s message history with async for message in channel.history(limit=50) and checking if message.author == bot.user and if the content matches your specific format. Although this method is less efficient, it works well when you don’t have the message object stored. I’ve implemented both approaches, and the first is certainly more reliable for real-time updates.

Just use channel.history() to grab your bot’s last message. Try async for msg in target_channel.history(limit=10): if msg.author == bot.user: await msg.edit(content="new stuff"); break - works great and beats storing global variables.

You could try channel.last_message if it exists, but that only works if your bot’s message is actually the most recent one. Better approach: check message content patterns instead of just the author. Loop through recent messages and match against a specific string that identifies your country list messages. Something like async for message in target_channel.history(limit=20): if message.author == bot.user and "Available Nations:" in message.content: await message.edit(content="your updated content"); break. This works great when your bot sends different types of messages to the same channel. Content matching makes sure you’re editing the right message type instead of accidentally changing some unrelated bot message. I’ve used this method across different Discord servers and it’s been solid regardless of how busy the channels are.