编辑已在 Discord.py 中编辑过的消息

Editing a message that has already been edited in Discord.py

我有一个用 Python 编码的 Discord 机器人,我有一个设置命令,它在服务器中创建不同的颜色角色。我希望它在每次创建角色时都编辑当前消息,而不是每次创建角色时都发送一条消息(这会填满聊天)。这是我到目前为止的损坏代码:

@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!`")

有没有办法让它编辑已经编辑过的消息?我尝试了上面的方法并返回了错误:

'NoneType' object has no attribute 'edit'

您可以删除邮件并编写新邮件,而不是编辑邮件。你可以这样做:

@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!`")

感谢对我的问题的第一条评论,它成功了! 代码是:

@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!`")

不必每次都创建一个新的消息对象,您只需使用同一个消息对象。