【问题标题】:Discord.py ctx.message.delete() raises error in cogDiscord.py ctx.message.delete() 在 cog 中引发错误
【发布时间】:2020-02-12 18:43:18
【问题描述】:

我最近将我的机器人移动到了 cogs 中,但随之而来的是一些麻烦。我有一个命令,当您调用该命令时,它会让机器人重复一条消息。我希望机器人删除调用消息的命令,然后发送您键入的消息。我有这个(注意:当它不在齿轮中时它有效):

@commands.command()
    async def say(self, ctx, *, arg=None):
        if arg is None:
            await ctx.send(f'{ctx.message.author.mention} Tell me what to say!')
        if len(arg) > 50:
            await ctx.send(f"{ctx.message.author.mention} That's too long!")
        else:
            await ctx.message.delete()
            await ctx.send(f'''{arg}
    - {ctx.message.author.mention}''')

代码运行并删除调用命令并发送消息的消息,但仍然抛出错误:

discord.errors.NotFound: 404 NOT FOUND (error code: 10008): Unknown Message

有什么办法可以解决这个问题吗?该命令再次起作用,但仍然抛出错误。

提前致谢。

编辑:经过一些测试,我想我已经找到了问题所在。我的代码的另一个问题是导致此问题,此代码本身不会导致错误。

如果其他人在迁移到 cog 时遇到类似问题,我的代码在 cog 中有一个 on_message 侦听器。我误加了await self.client.process_commands(message)

你不需要这个齿轮。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    试试这个代码。

    @commands.command()
        async def say(self, ctx, *, arg=""):
            if arg == "":
                await ctx.send(f'{ctx.message.author.mention} Tell me what to say!')
            elif len(arg) > 50:
                await ctx.send(f"{ctx.message.author.mention} That's too long!")
            else:
                await ctx.message.delete()
                await ctx.send(f"```{arg}
        - {ctx.message.author.mention}```")
    

    【讨论】:

    • 感谢您的回答。您的代码也可以正常工作,但它仍然会从上面抛出未知消息错误。我认为问题在于上下文,但我不确定到底是什么。
    • @ZippoTown 你不应该传递上下文吗??
    【解决方案2】:

    该错误是因为您试图获取刚刚删除的邮件的作者。将 ctx.message.author.mention 更改为 ctx.author.mention 应该可以修复此错误。

    @commands.command()
    async def say(self , ctx , * , arg=""):
        if arg == "":
            await ctx.send(f'{ctx.author.mention} Tell me what to say!')
        elif len(arg) > 50:
            await ctx.send(f"{ctx.author.mention} That's too long!")
        else:
            await ctx.message.delete()
            await ctx.send(f"```{arg}
        - {ctx.author.mention}```")
    

    祝你好运! :D

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2021-06-29
      相关资源
      最近更新 更多