Discord.py |我对 if 语句有疑问

Discord.py | I have a problem with if statement

所以我正在尝试制作一个基本的 错误处理程序 但似乎每次我 ping 某人或使用表情符号时都会激活其中一个错误处理程序。这个:commands.CommandNotFound。所以我认为这可能是因为它是一个标签和一个表情符号以 @: 开头,所以机器人可能会混淆并认为这 2 个是前缀。所以我试着做一个 If 语句,每次它都必须查看消息是否以机器人的前缀开头。但看起来我很笨,而且是 discord.py 的新手,所以我在问题部分遇到了错误。有人可以向我解释如何使用 message.startsWith 或任何有用的 If 语句吗?

我的代码(插入一个齿轮):

import discord
import datetime
from discord.ext import commands

class Errors(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_command_error(self, ctx, error):
        if ctx.message.startsWith(f'{client.prefix}'):
        

            if isinstance(error, commands.MissingRequiredArgument):
                errorrequired = discord.Embed(title='Something went wrong...', description='', color=0xf76300)
                errorrequired.add_field(name='Error: Missing An Argument', value='You are using the command wrong, make sure to add every argument!.\nContact <@434843854030635009> if you think this is a bug!')
                errorrequired.timestamp = datetime.datetime.utcnow()
                errorrequired.set_author(name=f'{ctx.author}', icon_url=ctx.author.avatar_url)

                await ctx.channel.send(embed=errorrequired)
        
            elif isinstance(error, commands.BadArgument):
                errorargument = discord.Embed(title='Something went wrong...', description='', color=0xf76300)
                errorargument.add_field(name='Error: User Not Found', value='Could not find the user you are looking for.\nContact <@434843854030635009> if you think this is a bug!')
                errorargument.timestamp = datetime.datetime.utcnow()
                errorargument.set_author(name=f'{ctx.author}', icon_url=ctx.author.avatar_url)

                await ctx.channel.send(embed=errorargument)
        """
        elif isinstance(error, commands.CommandNotFound):
            errornotfound = discord.Embed(title='Something went wrong...', description='', color=0xf76300)
            errornotfound.add_field(name='Error: Command can not be found', value='This command does not exit.\nContact <@434843854030635009> if you think this is a bug!')
            errornotfound.timestamp = datetime.datetime.utcnow()
            errornotfound.set_author(name=f'{ctx.author}', icon_url=ctx.author.icon_url)

            await ctx.channel.send(embed=errornotfound)
        elif isinstance(error, commands.MissingPermissions):
            errorperms = discord.Embed(title='Something went wrong...', description='', color=0xf76300)
            errorperms.add_field(name='Error: Missing Permissions', value='You are missing a required permission to run this command.\nContact <@434843854030635009> if you think this is a bug!')
            errorperms.timestamp = datetime.datetime.utcnow()
            errorperms.set_author(name=f'{ctx.author}', icon_url=ctx.author.icon_url)

            await ctx.channel.send(embed=errorperms)
        """
def setup(client):
    client.add_cog(Errors(client))

还有 problem/Error 我得到:

Undefined variable 'client'

注意:MissingPermissionsCommandNotFound 位于注释中,只是为了确保机器人不会为发生的每个 ping 开始发送嵌入。

您的 client 对象是 self.client,而不是 client,这就是您出现此未定义变量错误的原因。
另外,commands.Bot objects don't have any prefix attribute. To get your bot's prefixes, you can use Bot.get_prefix(message):

@commands.Cog.listener()
async def on_command_error(self, ctx, error):
    prefixes = await self.client.get_prefix(ctx.message)
    if ctx.message.content[0] in prefixes:
        ...

我还注意到另外两个错误:

  • 您使用“开头为”方法 ID startswith() 而非 startsWith()
  • 的方法
  • ctx.message 将 return 一个 discord.Message 对象,而不是一个字符串,所以你需要写成 ctx.message.content