discord.py rewrite | Making errors for my commands

岁酱吖の 提交于 2019-12-11 06:35:38

问题


Now that I finished my moderation commands [mostly], I am trying to add errors in. I already made the "please specify a member" error, but I cannot manage to make the bot say "this member does not exist" when an invalid name is input.

@client.command(name='kick',
            brief='Kicks user',
            aliases=['Kick'],
            pass_context=True)
async def kick(context, member:discord.Member=None):
# Errors
if not member:
    await context.send('Please specify a member.')
    return
# Actual Kicking
if context.author.guild_permissions.kick_members == True:
    await member.kick()
    await context.send(f"{member.mention} was kicked ")
else:
    await context.send(context.message.author.mention + ", you don't have permission")

This is one of my commands, where everything is working. I would like an error which says "User not found" if the member, obviously, doesn't exist. For example, k!kick ijhguiserb would make the bot say, "Member not found," rather than giving me an error in the shell.

Help would be appreciated, thanks!


回答1:


You'll have to define an error handler to handle the ConversionError

from discord.ext.commands import ConversionError

@kick.error
async def kick_error(ctx, error):
    if isinstance(error, ConversionError):
        await ctx.send("Member not found")
    elif isinstance(error, BadArgument):
        await ctx.send('Member not found')
    else:
        raise error


来源:https://stackoverflow.com/questions/54727825/discord-py-rewrite-making-errors-for-my-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!