为discord bot中的某个命令生成错误处理程序时,我收到一个文本错误“必需的位置参数:'coro'

2024-05-07 06:51:59 发布

您现在位置:Python中文网/ 问答频道 /正文

import discord
from discord.ext import commands

class Text(commands.Cog):
  def __init__(self, client):
    self.client = client
  
  @commands.command()
  @commands.has_permissions(administrator=True)
  async def text(self, ctx, *, message):
    await ctx.message.delete()
    await ctx.send(message)
    
  @text.error()
  async def text_error(self, error, ctx):
    if isinstance(error, commands.MissingPermissions):
      await ctx.send(f"{ctx.auhtor.mention} You don't Have permission to use this command")

def setup(client):
  client.add_cog(Text(client))

整个错误是:

File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 609, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.text' raised an error: TypeError: error() missing 1 required positional argument: 'coro'

1条回答
网友
1楼 · 发布于 2024-05-07 06:51:59

这是因为您的代码中有一些错误:

  1. @text.error()应替换为@text.error
  2. 您在text_error函数中交换了ctxerror
  3. 您在{ctx.auhtor.mention}中输入了一个错误

最终代码:

import discord
from discord.ext import commands


class Text(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    @commands.has_permissions(administrator=True)
    async def text(self, ctx, *, message):
        await ctx.message.delete()
        await ctx.send(message)

    @text.error
    async def text_error(self, ctx, error):
        if isinstance(error, commands.MissingPermissions):
            await ctx.send(
                f"{ctx.author.mention} You don't Have permission to use this command"
            )


def setup(client):
    client.add_cog(Text(client))

相关问题 更多 >