Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

python - How would I make on_command_error toggleable

I have a on_command_error() event in my discord bot, but sometimes when I'm writing new commands it's just not enough information, how would I make it toggleable? So that if its off it shows the python error and if it's on it would just show a small user friendly error. Here's what I have right now:

@bot.listen()
async def on_command_error(icy, error):
    try:
        await icy.message.delete()
    except:
        pass

    if __ERRORS__ == 0:
        embed = discord.Embed(title='**Error**', description=f'{error}', color=0xff2b2b)
        embed.set_thumbnail(url='https://media.tenor.com/images/fe70a29bb71a79150401c59c8991c4b8/tenor.gif')
        embed.set_footer(text=__footer__)
        await icy.send(embed=embed, delete_after=__deletetime__)

    if __ERRORS__ == 1:
        print(COLOR['RED'], f'| ERROR |',COLOR['YELLOW'],f'{error}', COLOR["DEFAULT"])
question from:https://stackoverflow.com/questions/66065536/how-would-i-make-on-command-error-toggleable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's pretty simple, you can make a command(s) that switches a variable using the global keyword

__ERRORS__ = False

@bot.command()
async def disable(ctx):
    global __ERRORS__
    __ERRORS__ = False

@bot.command()
async def enable(ctx):
    global __ERRORS__
    __ERRORS__ = True

@bot.event()
async def on_command_error(ctx, error):
    if __ERRORS__:
        raise error

    else:
        # Send the embed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...