Discord.py |我有一个if语句的问题



所以我想做一个基本的错误处理程序但似乎每次我呼叫某人或使用表情符号时,其中一个错误处理程序就会被激活。这个:commands.CommandNotFound。所以我认为这可能是因为它是一个标签,一个表情符号以@:开头,所以机器人可能会混淆,认为这两个是前缀。所以我试着做一个If语句,每次每次它都要看消息是否以bot的前缀开头。但我似乎很笨,而且不熟悉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在注释中只是为了确保bot不会开始为每个发生的ping发送嵌入。

您的client对象是self.client,而不是client,这就是为什么您有此未定义的变量错误。
另外,commands.Bot对象没有任何prefix属性。要获取bot的前缀,可以使用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:
...

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

  • 您使用的方法"以"开头方法idstartswith()notstartsWith()
  • ctx.message将返回一个discord.Message对象,而不是一个字符串,所以你需要写ctx.message.content代替。

最新更新