I’m working on a Discord bot that needs to update its own messages by adding new text content. The bot is designed for managing player reservations in a strategy game.
Here’s how it should work: The bot posts a list of available countries from a text file. When someone types !claim Germany, the bot should modify that original message to show the player’s name next to Germany.
Original bot message:
Country Selection
Germany
France
Russia
...
After user runs !claim Germany:
Country Selection
Germany - @username
France
Russia
...
I can retrieve the message ID successfully, but I’m stuck on how to properly edit the message content. My current approach with message.edit() isn’t working correctly.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@bot.command()
async def setup_game(ctx):
with open('countries.txt', 'r') as file:
game_msg = await ctx.send(file.read())
with open('msg_id.txt', 'w') as storage:
storage.write(str(game_msg.id))
@bot.command()
async def claim_germany(ctx):
target_channel = bot.get_channel(CHANNEL_ID)
with open("msg_id.txt", "r") as storage:
msg_id = int(storage.read())
original_msg = await target_channel.fetch_message(msg_id)
await original_msg.edit(content="NEW_CONTENT") # Need help here
bot.run(TOKEN)
The countries.txt contains:
Available Countries
Germany
France
Russia
Italy
Japan
Britain
USA
How do I properly modify the original message content to add the user’s name next to their chosen country?
You’re on the right track, but your string manipulation needs work. I hit this same issue building a tournament bracket bot. The problem is you’re not preserving the original message structure when editing. Don’t hardcode the country name - make your command dynamic so it works with any country. Here’s what worked for me:
@bot.command()
async def claim(ctx, *, country_name):
target_channel = bot.get_channel(CHANNEL_ID)
with open("msg_id.txt", "r") as storage:
msg_id = int(storage.read())
original_msg = await target_channel.fetch_message(msg_id)
content = original_msg.content
# Check if country exists and isn't already claimed
if f"\n{country_name}\n" in content or content.endswith(country_name):
content = content.replace(country_name, f"{country_name} - {ctx.author.mention}")
await original_msg.edit(content=content)
else:
await ctx.send("Country not available or already claimed")
Now users can type !claim Germany or !claim France and it’ll work for any country in your list.
Just use replace() on the original content instead of splitting lines. Try updated_content = original_msg.content.replace('Germany', f'Germany - {ctx.author.mention}') then edit with that. Way simpler than splitting lines.
I’ve had the same issue with my Discord bots. You’re replacing the whole message instead of just updating the part you need. Parse the existing content, find the country line, and add the username to it.
Here’s what worked for me:
@bot.command()
async def claim_germany(ctx):
target_channel = bot.get_channel(CHANNEL_ID)
with open("msg_id.txt", "r") as storage:
msg_id = int(storage.read())
original_msg = await target_channel.fetch_message(msg_id)
current_content = original_msg.content
lines = current_content.split('\n')
for i, line in enumerate(lines):
if line.strip() == "Germany":
lines[i] = f"Germany - {ctx.author.mention}"
break
updated_content = '\n'.join(lines)
await original_msg.edit(content=updated_content)
This splits the message into lines, finds your target country, updates that line, then puts it back together. Don’t forget to handle cases where the country’s already claimed or doesn’t exist.