Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

discord.py how to give a user a role

# If ctx is not defined and you are handling a message, use this code
ctx = await bot.get_context(message)

# This is the code needed to give a user a role
member = ctx.message.author # Member object that you want to add the role to
role = discord.utils.get(lambda role: role.name == "Role Name", ctx.guild.roles) # The role object
await member.add_roles(role) # Adds the role to the member
Comment

add role discord .py

member = message.author
var = discord.utils.get(message.guild.roles, name = "role name")
member.add_role(var)
Comment

add role discord .py


from discord.ext import commands
from discord.utils import get

bot = commands.Bot(command_prefix='!')

@bot.command(pass_context=True)
@commands.has_role("Admin") # This must be exactly the name of the appropriate role
async def addrole(ctx):
    member = ctx.message.author
    role = get(member.server.roles, name="Test")
    await bot.add_roles(member, role)

Comment

create a role with discord.py

@client.command(aliases=['make_role'])
@commands.has_permissions(manage_roles=True) # Check if the user executing the command can manage roles
async def create_role(ctx, *, name):
	guild = ctx.guild
	await guild.create_role(name=name)
	await ctx.send(f'Role `{name}` has been created')
Comment

adding roles discord py

# If you need this in a bot command
@discord.ext.commands.command()
async def online(ctx):
  	guild_id = ctx.guild.id # the guild id where the command has been called
    role = discord.utils.get(bot.get_guild(guild_id).roles, id ="""The Id of the role you search""")
    await member.add_roles(role) # The role gets added to a given Member 
Comment

adding roles discord py

# If you need this in a bot command
@bot.command()
async def online(ctx):
    role = discord.utils.get(bot.get_guild(ctx.guild.id).roles, id ="Role ID")
    await member.add_roles(role)
Comment

How to create role discord.py

guild = ctx.guild
await guild.create_role(name="role name")
Comment

PREVIOUS NEXT
Code Example
Python :: discord.py send image from url 
Python :: python recursively merge dictionaries 
Python :: python declare a variable 
Python :: python pillow cut image in half 
Python :: python invert an array 
Python :: python optional parameters 
Python :: python sort dictionary by value 
Python :: python shuffle array 
Python :: audio streaming python 
Python :: randomly shuffle pandas dataframe 
Python :: how to write a script to display an image in python 
Python :: how to remove none in python 
Python :: get name of month python 
Python :: iterate over classes in module python 
Python :: how to create adjacency matrix from adjacency list in python 
Python :: django environment variables 
Python :: string remove in python 
Python :: how to create python file in powershell 
Python :: pass a list to a function in python 
Python :: create python list 
Python :: replace matrix values python 
Python :: numpy random matrix 
Python :: python array scalar multiplication 
Python :: add value to dictionary python 
Python :: strftime python multiple formats 
Python :: length of string python 
Python :: make legend box transparent in matplotlib 
Python :: tkinter datatypes 
Python :: convert to datetime object 
Python :: install local package python 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =