Give role to user in message - python

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.

Related

'Member' has no attribute 'server'

In the given code below I was trying to give a role to the person who reacts to a message by using any emoji but this code throws me an error saying that 'Member' has no attribute 'server' what should I do now?
import discord
from discord.ext import commands
import random
client = discord.Client()
#client.event
async def on_ready():
print('ready')
#client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
await channel.send(f'{user.name} has reacted by using {reaction.emoji} emoji, his message was {reaction.message.content}')
role = discord.utils.get(user.server.roles, name = 'Bot')
await client.add_roles(user, role)
client.run('TOKEN')
First off take a look at the documentation. It'll tell you everything you need to know about what has what attributes. Also, server is called guild in discord.py. So use user.guild.roles instead.

Discord.py I am looking for make command to set role for everyone using the command

import discord
from discord.utils import get
client = commands.Bot(command_prefix="!")
#client.command(pass_context=True)
async def addrole(ctx):
user = ctx.message.author
role = discord.utils.get(user.server.roles, name="Open")
await ctx.add_roles(user, role)
but when I using this command . i getting this error (discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Member' object has no attribute 'server')
server is changed to guild and its Member.add_roles()
Below is the revised code:
import discord
from discord.utils import get
client = commands.Bot(command_prefix="!")
#client.command()
async def addrole(ctx):
role = get(ctx.guild.roles, name="Open")
await ctx.author.add_roles(role)

Issue with giving roles with a command Discord Py

I have made a command to set someone ROLE, However, it's throwing errors.
Command raised an exception: AttributeError: 'str' object has no attribute 'add_roles'
Is there anything wrong I have did? I am using the latest discord py version.
#bot.command()
async def set_role(ctx,member = None,val: int = None):
ab = member
ab = ab.replace("<","")
ab = ab.replace(">","")
ab = ab.replace("#","")
ab = ab.replace("!","")
user = bot.get_user(int(ab))
if val == 1:
role = discord.utils.get(ctx.guild.roles, name="Test")
await user.add_roles(role)
await ctx.send('Updated')
user = bot.get_user(int(ab))
This creates a user object. It is not affiliated to a guild / discord server. By design you cannot do add_roles.
Reason being your bot might not share a guild with this user. Or your bot might share multiple guilds with this user. But how does it know which guild you are actually adressing?
You need to create a member object. This could be done with:
member = ctx.guild.get_member(int(ab))
Now you have a member object and you can await add_roles.
await member.add_roles(role)
https://discordpy.readthedocs.io/en/latest/api.html#discord.Member.add_roles
to give a role to a member u need to give the member and the name of the role as inputs.
for example:
" ?set_role #clay#9871 example "
the bot gonna search for a role name 'example' from the guild roles
if the bot didn't find the role it will send "role not found" that's what the "try" and "except" for
#bot.command()
async def set_role(ctx,member :discord.Member,role):
try:
add_role= discord.utils.get(ctx.guild.roles, name=role)
await member.add_roles(add_role)
await ctx.send('Updated')
except:
await ctx.send("role not found!")

Discord Bot - Give roles after Message, Python

I am trying to make a simple discord bot, that gives a role to a user after he gives the bot a certain command
On the command !role the user should get a role called Beta
I first tried this:
from discord_webhook import DiscordWebhook, DiscordEmbed
import discord
from discord.ext.commands import Bot
from discord.ext import commands
client = commands.Bot(command_prefix = "!")
Client = discord.Client()
#client.event
async def on_message(message):
member = message.author
if member.bot:
return
if message.attachments:
return
print(message.content)
print(str(message.author))
if "role" in message.content:
embed=discord.Embed(title="Giving role.", color=0x00ff40)
await message.channel.send(message.channel, embed=embed)
role = discord.utils.get(server.roles, name="Beta")
await client.add_roles(message.author, role)
client.run("BOT TOKEN")
But I always get the following problem:
AttributeError: 'list' object has no attribute 'roles'
Thanks a lot for taking time to read this and if you can help me. Thanks
When you do:
role = discord.utils.get(server.roles, name="Beta")
You have to use
message.guild.roles
instead of:
server.roles
to access the list of roles, newer discord versions use guild instead of server to avoid confusion with voice servers.

Add role to user with user mention

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.

Categories