Warm tip: This article is reproduced from serverfault.com, please click

Discord.py send error msg if member don't have role

发布于 2020-12-02 12:19:48

Hey I'm a beginner to discord.py and need some help.

I am making a discord bot and I have a command that only members with the "Staff" role can use but if you don't have the role I want a command that sends an embed that says "Staff role is a requirement to use this command!". But I don't know how to make it, I have tried a little bit of everything but nothing works =/

This is what I have so far

# Moderator Commands List
@commands.has_role('Staff')
@bot.command(name='help_mod')
async def help_mod(context):
    my_embed = discord.Embed(title="Moderator Plugin", color=0xFFC71C)
    my_embed.add_field(name="Ban a member from the server", value="``?ban [member] (optional reason)``", inline=False)
    my_embed.add_field(name="Temporarily ban a member from the server", value="``?tempban [member] [duration] (optional reason)``", inline=False)
    my_embed.add_field(name="Mute a member in the whole server", value="``?mute [member] (optional reason)``", inline=True)
    my_embed.add_field(name="Temporarily mute a member in the server", value="``?tempmute [member] [duration] (optional reason)``", inline=False)
    my_embed.add_field(name="Kick a member from the server", value="``?kick [member] (optional reason)``", inline=False)
    my_embed.add_field(name="Unban a member", value="``?unban [member]``", inline=False)
    my_embed.add_field(name="Unmute a member", value="``?unmute [member]``", inline=False)
    my_embed.set_thumbnail(url='https://cdn.discordapp.com/attachments/765665083082407976/767502481922981928/ModHammer.png')
    my_embed.set_footer(text="Work in progress, these commands are not in function yet.")

Thanks for all the help!

Questioner
Sally
Viewed
0
Łukasz Kwieciński 2020-12-02 20:27:04

You can do it in the error handler:

@bot.command(name='help_mod')
@commands.has_role('Staff')
async def help_mod(ctx):
   # ...


@help_mod.error # <- name of the command + .error
async def help_mod_error(ctx, error):
    if isinstance(error, commands.MissingRole):
        await ctx.send("Staff role is a requirement to use this command!")

Here's an introduction to error handling