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 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)
Share:
26,402

Related videos on Youtube

MikeyJY
Author by

MikeyJY

Updated on July 13, 2021

Comments

  • MikeyJY
    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
      MikeyJY over 6 years
      The bot has Administrator permissions!
    • Patrick Haugh
      Patrick Haugh over 6 years
      The @ in front of async shouldn't be there. This function should be decorated with @client.event, or something similar. Instead of message.author.id, just pass message.author to add_roles
    • MikeyJY
      MikeyJY over 6 years
      The program returned: "NameError: name 'server' is not defined"
    • Patrick Haugh
      Patrick Haugh over 6 years
      Use message.server.roles.
    • MikeyJY
      MikeyJY over 6 years
      Ok i will try. Thx!
    • MikeyJY
      MikeyJY over 6 years
      Pls stay here if this doesn't working
    • MikeyJY
      MikeyJY over 6 years
      NameError: name 'name' is not defined(from name="admin")
    • Patrick Haugh
      Patrick Haugh over 6 years
      Could 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
      MikeyJY over 6 years
      I 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
      Patrick Haugh over 6 years
      That's because find doesn't take keyword arguments, it takes a predicate function. Are you using find or get?
    • MikeyJY
      MikeyJY over 6 years
      I'm using find!
    • MikeyJY
      MikeyJY over 6 years
      Should I use get?
    • Patrick Haugh
      Patrick Haugh over 6 years
      You can use either, they both do pretty much the same thing. find(lambda x: x.name == 'Admin', message.server.roles) and get(message.server.role, name='Admin') should get the same result. I tend to prefer get, but you can use either.
    • MikeyJY
      MikeyJY over 6 years
      Can you give me a full code! Please?
    • MikeyJY
      MikeyJY over 6 years
      OK!Be right back!
  • MikeyJY
    MikeyJY over 6 years
    Thank You! You helped me very much!
  • MikeyJY
    MikeyJY over 6 years
    Using 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
    Patrick Haugh over 6 years
    @MikeyJY Then there's no role on your server named Admin
  • MikeyJY
    MikeyJY over 6 years
    I 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
    Patrick Haugh over 6 years
    Yes. change role = get(message.server.roles, name='Admin') to role = get(message.server.roles, id='<role id>')
  • MikeyJY
    MikeyJY over 6 years
    Ok I'll try this!
  • MikeyJY
    MikeyJY over 6 years
    But how to get id of a role. Isn't "\@role"?
  • Patrick Haugh
    Patrick Haugh over 6 years
    Yes. Some roles can't be mentioned though, so you would have to change that setting in the server settings
  • MikeyJY
    MikeyJY over 6 years
    Can I get the id of the admin using the bot?
  • Patrick Haugh
    Patrick Haugh over 6 years
    Are there any other roles named Admin<something>? If not, we could use find(lambda x: x.startswith('Admin'), message.server.roles)
  • MikeyJY
    MikeyJY over 6 years
    I have one more question for you.
  • Kebab Programmer
    Kebab Programmer about 4 years
    Hi 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
    Patrick Haugh about 4 years