How to Make A Discord Bot - Part 7 (Updated 2021)

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 auto role. 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:

As you can see, there was a lot 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 separately 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 to the last tutorial, we will be accepting a member as a member object, like this. member:discord.Member and the role object similarly role:discord.Role.We will also need the reason for the removal.
The code should look like this:

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:

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:
async def gamer(ctx,*,reason):
member =
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 {}")
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 be needed to activate it and also a built-in client method is 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()

By now, you would have noticed a pattern.
ctx, keyword arguments(*), client.event, methods, classes and you would probably know to understand the async await type by now. You can now call yourself a pro at For that, I would like to give you something very valuable...
The Legendary Discord.PY Handbook
It's really just the module documentation, it consists 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 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

@FishballNooodle can you pls help my bot send welcome messages?