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

how to mention a role discord.py

#To mention a role you need the role ID
#Exaple
#you can use ~bot~ or ~client~ it dont matter
@bot.command
async def exaple_test(ctx):
	await ctx.send(f"Role: <@&_your_role_id_here_)>")
Comment

PREVIOUS NEXT
Code Example
Python :: with open as file python 
Python :: how to convert list into object and transform into tensors 
Python :: Triangle Quest 
Python :: copy files to a directory using text file 
Python :: compute condition number python 
Python :: seir model python 
Python :: dijkstras python 
Python :: django messages 
Python :: python loop opening file from directory 
Python :: print inline output in python 
Python :: python - calculate the value range on a df 
Python :: flatten tf keras 
Python :: creating a sqlite3 table in python and inserting data in it 
Python :: how to view all attributes and methods of an object python 
Python :: integer xticks 
Python :: python concatenate lists 
Python :: python read values from file 
Python :: How to take space-separated integer input in Python 3 
Python :: django authenticate 
Python :: get_absolute_url django 
Python :: how to make text change lines pygame 
Python :: how to send file in python request 
Python :: sklearn predict threshold 
Python :: remove space from string python 
Python :: get multiple inputs in python using map 
Python :: Program for length of the shortest word 
Python :: python winsound 
Python :: start a django project 
Python :: Handling categorical feature 
Python :: python if 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =