简体   繁体   中英

How can I make the mute command add a Muted role if there isn't already one? (discord.py)

I recently started making a moderation bot for Discord servers, and I have all the basic commands. I made a mute command, but for most new servers, there isn't a muted role. Does anyone know how to add a role to the role list, and turn off send messages?

Use utils.get to get a role named Muted

If that role is None then create a role using Guild.create_role

@bot.command()
async def mute(ctx, member: discord.Member):
    role = discord.utils.get(ctx.guild.roles, name="Muted")
    if not role:
        role = await ctx.guild.create_role("Muted", permissions=discord.Permissions(send_messages=False))
    await member.add_roles(role)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM