[go: up one dir, main page]

DEV Community

Swislok-Dev
Swislok-Dev

Posted on

Discord Role Commands

Bot Controls the Roles!

Time to give the bot the ability to give (and remove) roles from other members of the Discord server. This will function just as any other cog as a collection of functions and create commands out of those functions.

First, we'll build a command to first have members use to check to see what permissions they have. This will return one of two things. The first will be to check to see if a user has administrator permissions. While this may not seem useful at first this will provide the following command which is an error catch for the command to provide the list of permissions.

# './cogs/Roles.py

import nextcord
from nextcord.ext.commands.core import has_permissions, CheckFailure
from nextcord.utils import get
from nextcord.ext import commands

class Roles(commands.Cog):

  def __init__(self, client):
    self.client = client

  # Check if user has admin role
  @commands.command()
  @has_permissions(administrator=True)
  async def permCheck(self, ctx):
    await ctx.send("You're an Admin!")

  # Catch error to provide listed permissions for the user
  @permCheck.error  
  async def permCheckError(self, ctx, error):
    authorPerm = ctx.author.guild_permissions
    if isinstance(error, CheckFailure):
      await ctx.send(
        f'You have access to there permissions below:' +
        f"Manage Messages: {authorPerm.manage_messages}\n" +
        f"Manage Roles: {authorPerm.manage_roles}\n" +
        f"Mute Members: {authorPerm.mute_members}\n" +
        f"Deafen Members: {authorPerm.deafen_members}\n" +
        f"Move Members: {authorPerm.move_members}\n" +
        f"Ban Members: {authorPerm.ban_members}\n" 
      )

def setup(client):
  client.add_cog(Roles(client))
Enter fullscreen mode Exit fullscreen mode

We will then add in a couple more functions. One will tell a user (or a mentioned user) what their top role is, the other will be for those with the manage_roles permission to give/remove a role.

# Top Role Command
@commands.command(brief = "Shows a user's top role")
async def topRole(self, ctx, user: nextcord.Member=None):
  if user is None:
    user = ctx.author
  await ctx.send(f"{user.name} has {user.top_role} as the top role")

# Role Command
@commands.command()
@has_permissions(manage_roles = True)
async def role(self, ctx, user: nextcord.Member, role: nextcord.Role):
  try:
    if role not in user.roles:
        await user.add_roles(role)
        await ctx.send(f'{user.mention} now has the {role} role')
      else:
        await user.remove_roles(role)
        await ctx.send(f'{user.mention} no longer has the {role} role')

    except:
      await ctx.send(f'Tried to find {role}.\n Please make sure you can mention the role from the right channel {ctx.author}')
    finally:
      print('EOL for Role command\n')
Enter fullscreen mode Exit fullscreen mode

The last command is for the stealth server mods who want to gain access to having admin permissions.

@commands.command(hidden = True)
@has_permissions(manage_roles = True)
async def admin(self, ctx):
  user = ctx.author
  adminID = <admin-id-here>
  admin = get(ctx.guild.roles, id = adminID)

  if admin not in user.roles:
    await user.add_roles(admin)
  else: 
    await user.remove_roles(admin)
Enter fullscreen mode Exit fullscreen mode

This trick can be used also to give other special roles without the need for calling !role @some-user @some-role.

Top comments (0)