不和谐.py |我的 if 语句有问题

时间:2021-01-19 11:07:11

标签: python python-3.x discord.py discord.py-rewrite

所以我正在尝试制作一个基本的错误处理程序,但似乎每次我 ping 某人或使用表情符号时,都会激活其中一个错误处理程序。这个:commands.CommandNotFound。所以我认为这可能是因为它是一个标签,一个表情符号以 @: 开头,所以机器人可能会混淆并认为这两个是前缀。所以我试着做一个 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))

还有我得到的问题/错误:

Undefined variable 'client'

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

1 个答案:

答案 0 :(得分:1)

您的 client 对象是 self.client,而不是 client,这就是您遇到此未定义变量错误的原因。 此外,commands.Bot 对象没有任何 prefix 属性。要获取机器人的前缀,您可以使用 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 将返回一个 discord.Message 对象,而不是一个字符串,因此您需要改写 ctx.message.content