How to make a discord bot that gives roles in Python?
26,402
Solution 1
import discord
from discord.utils import get
client = discord.Client()
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == 'give me admin':
role = get(message.server.roles, name='Admin')
await client.add_roles(message.author, role)
I think this should work. The documentation for discord.py is here.
You could also use the discord.ext.commands
extension:
from discord.ext.commands import Bot
import discord
bot = Bot(command_prefix='!')
@bot.command(pass_context=True)
async def addrole(ctx, role: discord.Role, member: discord.Member=None):
member = member or ctx.message.author
await client.add_roles(member, role)
bot.run("token")
Solution 2
All you need to do is
import discord
from discord.utils import get
@client.event
async def on_message(message):
if message.content == "give me admin":
member = message.author
role = get(member.guild.roles, name="Admin")
await member.add_roles(role)
Related videos on Youtube
![MikeyJY](https://lh3.googleusercontent.com/-NdYFUuc0Djs/AAAAAAAAAAI/AAAAAAAAAF8/S_xSBZSL9q8/photo.jpg?sz=256)
Author by
MikeyJY
Updated on July 13, 2021Comments
-
MikeyJY almost 3 years
I want to create a discord bot that gives roles to members in Python.
I tried this:
@async def on_message(message): if message.content == "give me admin" role = discord.utils.get(server.roles, name="Admin") await client.add_roles(message.author.id, role)
-
MikeyJY over 6 yearsThe bot has Administrator permissions!
-
Patrick Haugh over 6 yearsThe
@
in front ofasync
shouldn't be there. This function should be decorated with@client.event
, or something similar. Instead ofmessage.author.id
, just passmessage.author
toadd_roles
-
MikeyJY over 6 yearsThe program returned: "NameError: name 'server' is not defined"
-
Patrick Haugh over 6 yearsUse
message.server.roles
. -
MikeyJY over 6 yearsOk i will try. Thx!
-
MikeyJY over 6 yearsPls stay here if this doesn't working
-
MikeyJY over 6 yearsNameError: name 'name' is not defined(from name="admin")
-
Patrick Haugh over 6 yearsCould you copy/paste that line directly here?
name='Admin'
should work like you expect it to, so I suspect there's a syntax error somewhere that's causing the problem -
MikeyJY over 6 yearsI resolved the NameError: name 'name' is not defined(from name="admin") but I have a new problem: TypeError: find() got an unexpected keyword argument 'name'
-
Patrick Haugh over 6 yearsThat's because
find
doesn't take keyword arguments, it takes a predicate function. Are you usingfind
orget
? -
MikeyJY over 6 yearsI'm using find!
-
MikeyJY over 6 yearsShould I use get?
-
Patrick Haugh over 6 yearsYou can use either, they both do pretty much the same thing.
find(lambda x: x.name == 'Admin', message.server.roles)
andget(message.server.role, name='Admin')
should get the same result. I tend to preferget
, but you can use either. -
MikeyJY over 6 yearsCan you give me a full code! Please?
-
MikeyJY over 6 yearsOK!Be right back!
-
-
MikeyJY over 6 yearsThank You! You helped me very much!
-
MikeyJY over 6 yearsUsing this code I get this error: "new_roles = utils._unique(role.id for role in itertools.chain(member.roles, roles)) AttributeError: 'NoneType' object has no attribute 'id'"
-
Patrick Haugh over 6 years@MikeyJY Then there's no role on your server named
Admin
-
MikeyJY over 6 yearsI know what is the problem: The role is named: Admin+an emoji but I use Python IDLE and when I put emoji the file is closing. Can I use role id?
-
Patrick Haugh over 6 yearsYes. change
role = get(message.server.roles, name='Admin')
torole = get(message.server.roles, id='<role id>')
-
MikeyJY over 6 yearsOk I'll try this!
-
MikeyJY over 6 yearsBut how to get id of a role. Isn't "\@role"?
-
Patrick Haugh over 6 yearsYes. Some roles can't be mentioned though, so you would have to change that setting in the server settings
-
MikeyJY over 6 yearsCan I get the id of the admin using the bot?
-
Patrick Haugh over 6 yearsAre there any other roles named
Admin<something>
? If not, we could usefind(lambda x: x.startswith('Admin'), message.server.roles)
-
MikeyJY over 6 yearsI have one more question for you.
-
Kebab Programmer about 4 yearsHi Patrick, may I enlist your help with an issue i have regarding this? In my code I have, is a bit different to this. In my code I already know the discord if for a user, and i get the role using
ctx.guild.roles
when i try using client.add_roles(), i get an error saying bot has no attribute 'add_roles()' -
Patrick Haugh about 4 years@KebabProgrammer stackoverflow.com/questions/59052536/…