【问题标题】:How to edit a message in discord.py如何在 discord.py 中编辑消息
【发布时间】:2023-04-02 22:19:01
【问题描述】:

如果我的机器人检测到关键字,我想让它编辑消息,但我不确定如何编辑消息。

我查看了文档,但似乎无法弄清楚。我在 python 3.6 中使用 discord.py。

这是代码:

@bot.event
async def on_message(message):
    if 'test' in message.content:
        await edit(message, "testtest")

这是错误:

  File "testthing.py", line 67, in on_message
    await edit(message, "test")
 NameError: name 'edit' is not defined

如果消息包含单词 test,我希望机器人将消息编辑为“testtest”,但我只是收到一个错误。

【问题讨论】:

  • 您使用的是什么版本的 discord.py? print(discord.__version__)
  • Discord.py 1.0.0

标签: python discord.py


【解决方案1】:

您可以使用Message.edit 协程。参数必须作为关键字参数contentembeddelete_after 传递。您只能编辑已发送的消息。

await message.edit(content="newcontent")

【讨论】:

  • 我试过了,但我只是得到这个错误:raise Forbidden(r, data) discord.errors.Forbidden: FORBIDDEN (status code: 403): Cannot edit a message authored by another user跨度>
  • 虽然我对机器人有管理员权限。
  • @nijwons 实际上,manage_messages 的文档让我相信您无法编辑其他用户的消息。
  • 哦,是这样吗?昵称是否与我得到相同的错误相同。
  • @nijwons 有一个权限:manage_nicknames。确保您的机器人具有该功能,并检查它是否是角色层次结构问题。您的机器人可能无法编辑最高角色高于机器人最高角色的用户。
【解决方案2】:

这是一个对我有用的解决方案。

@client.command()
async def test(ctx):
  message = await ctx.send("hello")
  await asyncio.sleep(1)
  await message.edit(content="newcontent")

【讨论】:

    【解决方案3】:

    请尝试将def 添加到您的代码中,如下所示:

    @bot.event
    async def on_message(message):
        if 'test' in message.content:
            await edit(message, "edited !")
    

    【讨论】:

      【解决方案4】:

      你这样做了吗:

      from discord import edit
      

      或者这个:

      from discord import *
      

      在使用 message.edit 函数之前?

      如果你这样做了,问题可能出在你的 discord.py 版本上。 试试这个:

      print(discord.__version__)
      

      【讨论】:

        【解决方案5】:

        这就是我所做的:

        @bot.event
        async def on_message(message):
          if message.content == 'test':
            await message.channel.send('Hello World!')
            await message.edit(content='testtest')
        

        我不知道这是否适合你,但试试看。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-29
          • 2020-03-17
          • 2021-03-19
          • 1970-01-01
          • 2021-07-31
          • 1970-01-01
          • 2020-08-16
          相关资源
          最近更新 更多