简体   繁体   English

如何在 discord.py 中编辑消息?

[英]How Do I edit a message in discord.py?

The code below is for discord.py v1.4.0以下代码适用于 discord.py v1.4.0

I tried multiple things looking up in the documentation but it just doesn't seem to work I don't know why.我尝试了在文档中查找的多种方法,但它似乎不起作用,我不知道为什么。 Can someone please help me with this.有人可以帮我解决这个问题。

@bot.command(pass_context=True)
async def balance(ctx):
  boolean = hypixelSkyblockBank.updateBalance()

  embed = discord.Embed(
    title = "Bank Balance",
    color = discord.Color.orange()
  )

  with open('dataStorage.txt', 'r') as inputt:
    dataStorage = eval(inputt.read())

  if boolean:
    embed.add_field(name='Total Balance', value='{:,.1f}'.format(round(dataStorage['balance'], 1)), inline=False)
    embed.add_field(name='LaterKids:', value='{:,.1f}'.format(round(dataStorage['LaterKids'], 1)), inline=True)
    embed.add_field(name='LaterIdiot:', value='{:,.1f}'.format(round(dataStorage['LaterIdiot'], 1)), inline=True)
  else:
    embed.add_field(name='Error', value="Hypixel Skyblock API is out of reach.", inline=False)
  
  await ctx.send(embed=embed)
  
  repeat = 0
  time.sleep(5)
  while True:
    boolean = hypixelSkyblockBank.updateBalance()
    if boolean:
      embed.set_field_at(0, name='Total Balance', value='{:,.1f}'.format(round(dataStorage['balance'], 1)), inline=False)
      embed.set_field_at(1, name='LaterKids:', value='{:,.1f}'.format(round(dataStorage['LaterKids'], 1)), inline=True)
      embed.set_field_at(2, name='LaterIdiot:', value='{:,.1f}'.format(round(dataStorage['LaterIdiot'], 1)), inline=True)
    else:
      repeat += 1
      if repeat == 1:
        embed.clear_fields()
        embed.add_field(name='Error', value="Hypixel Skyblock API is out of reach.", inline=False)
      else:
        embed.set_field_at(0, name='Error', value="Hypixel Skyblock API is out of reach.", inline=False)
    await ctx.edit(embed=embed)
    time.sleep(10)

When running this it gives me an error:运行它时,它给了我一个错误:

Bot is ready
[]
Total Balance: 13561217.227630626
LaterKids: 84047.0
LaterIdiot: 13477170.227630626
Timestamp: 1595896376010
[]
Total Balance: 13561217.227630626
LaterKids: 84047.0
LaterIdiot: 13477170.227630626
Timestamp: 1595896376010
Ignoring exception in command balance:
Traceback (most recent call last):
  File "c:\Users\Name\Desktop\Python-Projects\hypixel skyblock bank record\env\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/Name/Desktop/Python-Projects/hypixel skyblock bank record/discordBOT.py", line 54, in balance
    await ctx.edit(embed=embed)
AttributeError: 'Context' object has no attribute 'edit'

The above exception was the direct cause of the following exception:上述异常是以下异常的直接原因:

Traceback (most recent call last):
  File "c:\Users\Name\Desktop\Python-Projects\hypixel skyblock bank record\env\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "c:\Users\Name\Desktop\Python-Projects\hypixel skyblock bank record\env\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "c:\Users\Name\Desktop\Python-Projects\hypixel skyblock bank record\env\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'edit'

You are trying to use a edit attribute on a Context object.您正在尝试在Context object 上使用edit属性。 When you send the message, you want to assign it to a variable发送消息时,您希望将其分配给变量

my_custom_message_variable = await ctx.send(embed=embed)

then at the end when you try to edit the message, you want to use the edit attribute on the custom variable which is holding your message object.最后,当您尝试编辑消息时,您希望在保存您的消息 object 的自定义变量上使用edit属性。

await my_custom_message_variable.edit(embed=embed)

As a side note, import asyncio and start using asyncio.sleep(seconds) as time.sleep is blocking.附带说明一下,导入asyncio并开始使用asyncio.sleep(seconds)因为time.sleep是阻塞的。 Which will stop your bot from processing anything else until the time delay is over.这将阻止您的机器人处理任何其他内容,直到时间延迟结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM