'Editing a message that has already been edited in Discord.py
I have a Discord bot coded in Python, and I have a setup command for it where it creates different colour roles in the server. Instead of it sending a message every time it creates a role (which would fill up the chat), I want it to edit the current message every time it's created a role. Here's the broken code I have so far:
@client.command()
async def setup(ctx):
guild = ctx.guild
message = await ctx.reply("**Sonata is currently setting up!**\n"
"`[..........] 0% complete - Creating colour roles: Red`")
await guild.create_role(name="Red", colour=discord.Colour(0xe94d4d))
message2 = await message.edit(content="**Sonata is currently setting up!**\n"
"`[/.........] 16.6666667% complete - Creating colour roles: Orange`")
await guild.create_role(name="Orange", colour=discord.Colour(0xce7816))
message3 = await message2.edit(content="**Sonata is currently setting up!**\n"
"`[///.......] 33.3333334% complete - Creating colour roles: Yellow`")
await guild.create_role(name="Yellow", colour=discord.Colour(0xceb716))
message4 = await message3.edit(content="**Sonata is currently setting up!**\n"
"`[/////.....] 50.0000001% complete - Creating colour roles: Green`")
await guild.create_role(name="Green", colour=discord.Colour(0x4ace16))
message5 = await message4.edit(content="**Sonata is currently setting up!**\n"
"`[///////...] 66.6666668% complete - Creating colour roles: Blue`")
await guild.create_role(name="Blue", colour=discord.Colour(0x1674ce))
message6 = await message5.edit(content="**Sonata is currently setting up!**\n"
"`[/////////.] 83.3333335% complete - Creating colour roles: Purple`")
await guild.create_role(name="Purple", colour=discord.Colour(0xb316ce))
await message6.edit(content="**Sonata is currently setting up!**\n"
"`[//////////] 100% complete!`")
Is there a way to make it edit an already edited message? I tried the above and it returned the error:
'NoneType' object has no attribute 'edit'
Solution 1:[1]
Thanks to the first comment on my question, it worked! The code is:
@client.command()
async def setup(ctx):
guild = ctx.guild
message = await ctx.reply("**Sonata is currently setting up!**\n"
"`[..........] 0% complete - Creating colour roles: Red`")
await guild.create_role(name="Red", colour=discord.Colour(0xe94d4d))
await message.edit(content="**Sonata is currently setting up!**\n"
"`[/.........] 16.6666667% complete - Creating colour roles: Orange`")
await guild.create_role(name="Orange", colour=discord.Colour(0xce7816))
await message.edit(content="**Sonata is currently setting up!**\n"
"`[///.......] 33.3333334% complete - Creating colour roles: Yellow`")
await guild.create_role(name="Yellow", colour=discord.Colour(0xceb716))
await message.edit(content="**Sonata is currently setting up!**\n"
"`[/////.....] 50.0000001% complete - Creating colour roles: Green`")
await guild.create_role(name="Green", colour=discord.Colour(0x4ace16))
await message.edit(content="**Sonata is currently setting up!**\n"
"`[///////...] 66.6666668% complete - Creating colour roles: Blue`")
await guild.create_role(name="Blue", colour=discord.Colour(0x1674ce))
await message.edit(content="**Sonata is currently setting up!**\n"
"`[/////////.] 83.3333335% complete - Creating colour roles: Purple`")
await guild.create_role(name="Purple", colour=discord.Colour(0xb316ce))
await message.edit(content="**Sonata is currently setting up!**\n"
"`[//////////] 100% complete!`")
Instead of creating a new message object every time, you just use the same message object.
Solution 2:[2]
Instead of editing the message, you can delete it and write the new one. You can do something like this:
@client.command()
async def setup(ctx):
guild = ctx.guild
message = await ctx.reply("**Sonata is currently setting up!**\n"
"`[..........] 0% complete - Creating colour roles: Red`")
await guild.create_role(name="Red", colour=discord.Colour(0xe94d4d))
await client.delete_message(message)
message = await ctx.reply("**Sonata is currently setting up!**\n"
"`[/.........] 16.6666667% complete - Creating colour roles: Orange`")
await guild.create_role(name="Orange", colour=discord.Colour(0xce7816))
await client.delete_message(message)
message = await ctx.reply("**Sonata is currently setting up!**\n"
"`[///.......] 33.3333334% complete - Creating colour roles: Yellow`")
await guild.create_role(name="Yellow", colour=discord.Colour(0xceb716))
await client.delete_message(message)
message = await ctx.reply("**Sonata is currently setting up!**\n"
"`[/////.....] 50.0000001% complete - Creating colour roles: Green`")
await guild.create_role(name="Green", colour=discord.Colour(0x4ace16))
await client.delete_message(message)
message = await ctx.reply("**Sonata is currently setting up!**\n"
"`[///////...] 66.6666668% complete - Creating colour roles: Blue`")
await guild.create_role(name="Blue", colour=discord.Colour(0x1674ce))
await client.delete_message(message)
message = await ctx.reply("**Sonata is currently setting up!**\n"
"`[/////////.] 83.3333335% complete - Creating colour roles: Purple`")
await guild.create_role(name="Purple", colour=discord.Colour(0xb316ce))
await client.delete_message(message)
message = await ctx.reply("**Sonata is currently setting up!**\n"
"`[//////////] 100% complete!`")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | A person |
Solution 2 | Peter Mortensen |