How to Make A Discord Bot - Part 7
FishballNooodle (131)

Today we will be doing something very interesting, requested by @polarbear2015mw (0), I will be teaching you all over how to assign roles and also how to autorole. Lastly, we will be going over how to create join messages.

Assigning, Creating & Removing Roles

Creating Roles

Firstly, we need to know that we can actually create a role ourselves, so this is pretty useless...but I'll still go through it, it's really easy.
We will be using the add_roles of the guild object which we can get by using ctx.
guild.add_roles(name, reason,colour,hoist,mentionable,reason)
So this is how the code will look like:

@client.command()
async def create_role(ctx,name,*,reason=None):
  role = await ctx.guild.create_role(name=name,colour=discord.Colour.orange(),hoist=True,mentionable=True,reason=reason)
  await ctx.send(f'{ctx.guild.owner.mention}, {ctx.author} has made the role {role.mention}'

As you can see, there was alot of arguments being passed over there.
I will explain each of them:

  • name - name of the role
  • colour - colour of the role
  • hoist - This is a boolean, if True it will be shown seperately from other members
  • mentionable - if True, the role will light up blue when you do @rolename
  • reason - reason for making role(Shows in Audit Log), as you can see in the def command, it is by default None, so it's optional. Also note that it has the * which make reason a keyword argument

    Note that the name in the input cannot have spaces because reason is already taken as the keyword argument for this function.
    If you don't want this, you can remove the reason argument totally(It's not really useful) and replace it with the name as the keyword.

Removing Roles to a member

First, we need to get the member object, so similar as last tutorial, we will be accepting member as an member object, like this. member:discord.Member and the role object similarly role:discord.Role.We will also need the reason of the removal.
The code should look like this:

@client.command()
async def remove_role(ctx,role:discord.Role,member:discord.Member,*,reason=None):
  await member.remove_roles(reason=reason)
  await ctx.send(f'{ctx.author.mention}, has removed the role {role.name} from {member.mention}'

The command should be typed like this:

By the way, if I didn't explain what .mention does, it pings the relevant user or role. And . name prints the string form so it doesn't ping the other people in the role unnecessarily.

Adding Roles From a Member

This is almost exactly the same as the one above so i'm not gonna explain.
Here's the code:

@client.command()
async def **add**_role(ctx,role:discord.Role,member:discord.Member,*,reason=None):
  await member.**add**_roles(reason=reason)
  await ctx.send(f'{ctx.author.mention}, has **added** the role {role.name} from {member.mention}'

Auto Role

So now what, you don't want to add role manually, you want them to add automatically, probably by a command. I will be using the command |gamer to give the person the role called 'gamer'.
How we will do this is we will not actually be taking in any other arguments other than ctx and the reason.
We will get the role using id, see my discord tutorial on Developer Mode to learn how to get the role id.
Next, we will use member.add_roles method where it will take in the role and reason.
Lastly, we will be doing a quick check to see if he has the role before we add the role to him. If he already has the role it will return an error message.

Note that the role has already been created.

Here's How the Code looks like:
@client.command()
async def gamer(ctx,*,reason):
member = ctx.author
role = client.get_role(2476234827364)
#put your own role id here
if role in member.roles:
await member.add_roles(role,reason=reason)
await ctx.send(f'You have been given the role {role.name}")
else:
await ctx.send("You already have that role")

Join & Leave Messages (Upon Request)

We will be using @client.event this time since no command will needed to activate it and also a built in client method is which discord.on_member_join and discord.on_member_remove (kick or ban). It needs 1 argument which is member, which you can take it fro
So there's really not much explanation for this one.

Note: I would prefer if you put this function just after on_ready()

@client.event
async def on_member_join(member):
  #This can be set to a specific channel, for example a 'Welcome'
  channel = client.get_channel(8263541721827)
  #This should be your own channel id
  await channel.send(f"Welcome {member.mention} to our server, hope you will like it here.")
#Leave function is same
@client.event
async def on_member_remove(member):
  channel = client.get_channel(8263541721827)
  #This should be your own channel id
  await channel.send(f"Bye {member.mention} !!!")

By now, you would have noticed a pattern.
ctx, keyword arguments(*),client.event, methods, classes and you would probably know understand the asycnc await type by now. You can now call yourself a pro at discord.py. For that, i would like to give you something very valuable....
The Legendary Discord.PY Handbook
It's really just the module documentation, it consist of almost all data you want to access and how to do so. With that let me do my outro.

This ends my 7-day discord.py series. Hope you all enjoyed it, thanks for the constant support over the past few tutorials. You can still post your questions under this post if you have any in the future.
Stay Safe and Happy Coding!!!
:)

You are viewing a single comment. View All