Add role to user with user mention - python

I was trying to make a bot that is able to add a role to a user with a user mention. I also want to pass the role in the command.
So the syntax should be:
!addrole ROLENAME #user
I tried this:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.utils import get
def read_token():
with open("token.txt", "r") as f:
lines = f.readlines()
return lines[0].strip()
token = read_token()
bot = commands.Bot(command_prefix='!')
#bot.command(pass_context=True)
async def addrole(ctx, arg1, member: discord.Member):
role = get(member.guild.roles, name={arg1})
await member.add_roles(role)
bot.run(token)
but it doesn't work. I receive the error:
AttributeError: 'NoneType' object has no attribute 'id'

You're already using a converter for member, use one for role too:
from discord import Member, Role
#bot.command()
async def addrole(ctx, member: Member, *, role: Role):
await member.add_roles(role)
The , *, allows you to supply role names that have multiple words.

Related

How to make a person be given a role, discord.py?

For example, a person mentions the role of #role1 and the bot gives him this role. How to do it?
For the rewrite branch:
role = discord.utils.get(ctx.guild.roles, name="role to add name")
user = ctx.message.author
await user.add_roles(role)
For the async branch:
user = ctx.message.author
role = discord.utils.get(user.server.roles, name="role to add name")
await client.add_roles(user, role)
I think this is a good solution. Just make it so it requires a role, or a role mention.
We basically make a command where it requires two arguments: a role, and a mentioned member. You can configure this to make it so only administrators can run this command. If the Role doesn't exist, it will send a "This is not a correct role" message. Let me know if you face any issues!
from discord.ext import commands
from discord.utils import get
import discord
intents = discord.Intents.all()
client = commands.Bot(command_prefix = '!', intents=intents, case_intensitive=True)
#client.event
async def on_ready():
print('Bot is online!')
#client.command()
async def addrole(ctx, user: discord.Member, role: discord.Role):
try:
await user.add_roles(role)
await ctx.send(f'{user.mention} was given the role: {role.name}')
except commands.BadArgument:
await ctx.send('That is not a correct role!')
client.run('TOKEN')

assign role on command

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)
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)

Give role to user in message

print("dsdsdd")
import discord
import time
from discord.ext import commands
from discord.utils import get
TOKEN = "..."
ROLE = "Rekrutant"
bot = commands.Bot(command_prefix = "/")
#bot.event
async def on_ready():
print("Zalogowano: " + bot.user.name)
#bot.command()
async def setup(ctx, user: discord.Member = None):
ADMIN_ROLE = discord.utils.get(ctx.guild.roles, name="Admin")
if (ADMIN_ROLE in ctx.author.roles):
await user.add_roles("Rozmowy+")
else:
await ctx.send("Nie masz uprawnien, aby uzywac komendy")
bot.run(TOKEN)
I have problem with getting user form message.
I got an error:
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'str' object has no attribute 'id'
I don't know why it doesn't work.
This is because you are putting a string into the add_roles function, which really wants a role object instead. Try:
await ctx.author.add_roles(discord.utils.get(ctx.guild.roles,name="Rozmowy+"))
This new function turns the string into a role object, which is what the original function was looking for. Also, I changed user to ctx.author, because that was the only user object that worked for me. If you need any clarification, ask me in a comment down below.

How can my bot give a user a role through a command

For example, I want that if I enter !Cuff #user that this user gets the role of cuff. Does anyone know how to do this?
if message.content.startswith('!cuff'):
args = message.content.split(' ')
if len(args) == 2:
member: Member = discord.utils.find(lambda m: args[1] in m.name, message.guild.members)
if member:
role = get(message.server.roles, name='Cuff')
await client.add_roles('{}'.format(member.name))
If you want to make a command, you should use discord.ext.commands instead of on_message event. Then, you can get the discord.Member object easily with passing arguments.
Also, client object has no attribute add_roles. You should use discord.Member.add_roles(role) and message object has no attribute server, it has guild instead.
from discord.ext import commands
client = commands.Bot(command_prefix="!")
#client.command()
async def cuff(ctx, member: discord.Member=None):
if not member:
return # or you can send message to the channel that says "you have to pass member argument"
role = get(ctx.guild.roles, name='Cuff')
await member.add_roles(role)

How do you remove a role from a user?

I'm trying to remove a role from a user with a command but I'm not quite sure how the command works.
Here is the code:
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
await bot.remove_roles(user, 'member')
await bot.say("{} has been muted from chat".format(user.name))
It looks like remove_roles needs a Role object, not just the name of the role. We can use discord.utils.get to get the role
from discord.utils import get
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role = get(ctx.message.server.roles, name='member')
await bot.remove_roles(user, role)
await bot.say("{} has been muted from chat".format(user.name))
I don't know what happens if you try to remove a role that the target member doesn't have, so you might want to throw in some checks. This might also fail if you try to remove roles from an server admin, so you might want to check for that too.
To remove a role, you'll need to use the remove_roles() method. It works like this:
member.remove_roles(role)
The "role" is a role object, not the (role's) name, so it should be:
role_get = get(member.guild.roles, id=role_id)
await member.remove_roles(role_get)
With your code:
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role_get = get(member.guild.roles, id=role_id) #Returns a role object.
await member.remove_roles(role_get) #Remove the role (object) from the user.
await bot.say("{} has been muted from chat".format(user.name))
The remove_roles() function takes two parameters, the first of which is the user from which the role and the role itself are removed. It is possible not to write user: discord.Member in the function parameters, leave only ctx. You also need the role you want to delete. This can be done by:
role = get (ctx.author.guild.roles, name = "the name of the role you want to remove")
#also include 'from discord.utils import get'
And then to remove the obtained role (object), call the function:
await ctx.author.remove_roles (role)
So, the final code should look something like this:
from discord.utils import get
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx):
role = get (ctx.author.guild.roles, name = "the name of the role you want to remove")
await ctx.author..remove_roles(role)
await ctx.send("{} has been muted from chat".format(user.name))
Try that:
import discord
from discord.ext import commands
#bot.command()
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role = discord.utils.get(ctx.guild, name='member')
await user.remove_roles(role)
await ctx.send(f'{user.name} has been muted from chat')

Categories