I'm pretty new to python and discord bots in general. I am trying to make it so when a user runs !addrole (role) they will get that role. I need it to work for multiple users and multiple roles. It would be nice if users could also give other users that role.
from discord.ext import commands
import os
import random
import discord
from discord.utils import get
my_secret = os.environ['TOKEN']
GUILD = os.getenv('DISCORD_GUILD')
bot = commands.Bot(command_prefix='!') #prefix
@bot.command(pass_context=True)
@commands.has_role("admin")
async def addrole(ctx, *, rolewanted):
member = ctx.message.author
role = rolewanted
await bot.add_roles(member, role)
bot.run(my_secret)
CodePudding user response:
Make sure to read the documentation, you're code is pretty outdated. You can type hint member
and role
to discord objects and use the add_roles
method to do what you need
@bot.command()
@commands.has_role('admin')
async def addrole(ctx, member: discord.Member, role: discord.Role):
await member.add_roles(role)
CodePudding user response:
Try this:
async def addrole(ctx, *, rolewanted):
member = ctx.message.author
role = get(member.server.roles, name=rolewanted)
await bot.add_roles(member, role)