I'm trying to make a simple command that allows a user to give themselves a role, but if the role doesn't exist, it will create the role first, then give them the role. Here is my current code:
#bot.command()
async def SetRole(ctx, arg):
roles = ctx.guild.roles
user = ctx.message.author
#check if role does not exist
if arg not in [role.name for role in roles]:
await ctx.guild.create_role(name=arg)
await user.add_roles(discord.utils.get(roles, name=arg))
The expected result when I run $SetRole test_role if test_role does not exist, would be that the bot creates the role and then gives me the role. However, the role is created but is not given to me. Here is there error and stack trace:
Ignoring exception in command SetRole:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 17, in SetRole
await user.add_roles(discord.utils.get(roles, name=arg))
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 777, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'id'
If I run the command a second time, the role is successfully given to me. What I presume is happening is for some reason, the user.add_roles() is happening before the create_role most likely because of some weirdness with async. How can I make sure that it adds the role after its created?
This isn't a race condition, just that discord.py doesn't track and mutate objects everywhere
A better implementation would be:
from typing import Union
#bot.command()
async def SetRole(ctx, role: Union[Role, str]):
if isinstance(role, str):
role = await ctx.guild.create_role(name=role)
await user.add_roles(role)
Union is just a way to represent "any of the given types", so discord.py [cleverly] either return a role or a string as fallback, we can then create the role if it doesn't already exist by checking if role is a string
Related
So i made mute command for discord bot. it works, but doesn't add any permissions to 'Muted' role? Any ideas what's wrong?
Code:
#client.command(name = 'mute')
#commands.has_permissions(manage_permissions = True)
async def mute(ctx, member: discord.Member, *, reason = None):
muted = discord.utils.get(ctx.guild.roles, name = 'muted')
perms = discord.Permissions(send_messages = False)
if muted not in ctx.guild.roles:
muted = await ctx.guild.create_role(name = 'muted', permissions = perms)
memroles = discord.utils.get(member.roles)
if muted in memroles:
await ctx.send(f':x: This person is already muted')
await member.add_roles(muted)
await ctx.send(f':white_check_mark: **User <#{member.id}> was muted!**')
print(f'User {member} was muted!')
Error:
Traceback (most recent call last):
File "C:\Users\endport\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "D:\endport_semegen\discordpy\smth.py", line 62, in mute
if muted in memroles:
TypeError: argument of type 'Role' is not iterable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\endport\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\endport\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\endport\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: argument of type 'Role' is not iterable```
The issue is when you discord.utils.get on the member roles.
From the docs, emphasis mine:
A helper that returns the first element in the iterable that meets all the traits passed in attrs. This is an alternative for find().
Since you didn't provide anything for it to check, it will return the first role that satisfies the requirements - the first role the member has. Obviously, this is not the list of roles that you wanted.
The solution is to simply remove the get. There's no reason to do it. Now, member.roles will be a list that you can do checking with.
memroles = member.roles
if muted in memroles:
await ctx.send(f':x: This person is already muted')
I want to make an Unmute command but not in only 1 server, but all of the servers a member is in.
The code I have that doesnt work:
async def unmuteAterx(ctx):
Aterx = 549328259331129364
if ctx.message.author.id == Aterx:
for guild in client.guilds:
member = guild.get_member(549328259331129364)
if member is not None:
role = discord.utils.get(guild.roles, name='Muted')
await member.remove_roles(role)
(i have it set to only me rn because im testing it)
I get an error: Command raised exception: AttributeError: 'NoneType' object has no attribute 'id'
Full error:
Ignoring exception in command unmuteAterx:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 477, in unmuteAterx
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 720, in remove_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'id'
As #Weylyn pointed out, this happens because the role with the name 'Muted' doesn't exist on all the server in which the bot is.
When this is the case, discord.utils.get(guild.roles, name='Muted') will return None so you can check for that before trying to remove the role.
Also this bit of code doesn't remove the muted role of the member on all of his servers, but on all the servers where the bot is
my current code is
#client.command()
async def check_vouches(ctx, member : discord.User=None):
users = await get_vouch_data()
test = ctx.author
role = discord.utils.get(test.guild.roles, name="10 vouches")
vouches_gotten = users[str(member.id)]["vouches_gotten"]
if member is not None:
if vouches_gotten == 10:
await test.add_roles(role)
await ctx.send("you now have the {role} role")
else:
await ctx.reply("first get 10 vouches")
which does not give an error code when i start the bot but when i try use the command i get the following error code:
Ignoring exception in command check_vouches:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 193, in check_vouches
vouches_gotten = users[str(member.id)]["vouches_gotten"]
AttributeError: 'NoneType' object has no attribute 'id'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'id'
i have already tried putting
async def check_vouches(ctx, member : discord.User=None,*, id):
but when i tried even with an id there it still didn't work. please help me as i am out of ideas as i have tried editing several things and it still gave me an error code
As said in the comments, you are only proceeding when the member is None, which shouldn't be the case.
If the member is None, you can do two things, give it a default value and proceed or stop the command and raise an error.
Stopping the command:
if member is None:
await ctx.send('You have to mention someone to check their vouches')
else:
# check and show vouch
Providing a default value: (author)
if member is None:
member = ctx.author
# don't check if member is None later
#check and show vouches
This might need a little explaining. I made a command that sudo's someone called p!sudo [member.mention] [message]. It works like this:
#client.command()
async def sudo(ctx, member: discord.Member, *, message=None):
await ctx.message.delete()
webhooks = await ctx.channel.webhooks()
for webhook in webhooks:
await webhook.delete()
webhook = await ctx.channel.create_webhook(name=member.name)
await webhook.send(str(message),
username=member.name,
avatar_url=member.avatar_url)
Now, I want to make another command that sudo's everyone in the server. Here's what I got:
#client.command()
async def sudoall(ctx, *, message=None):
for member in ctx.guild.member:
webhooks = await ctx.channel.webhooks()
for webhook in webhooks:
await webhook.delete()
webhook = await ctx.channel.create_webhook(name=member.name)
await webhook.send(str(message),
username=member.name,
avatar_url=member.avatar_url)
However, I get this error message:
Ignoring exception in command sudoall:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 34, in sudoall
for member in ctx.guild.member:
AttributeError: 'Guild' object has no attribute 'member'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Guild' object has no attribute 'member'
Use ctx.guild.members to get the list of members belonging in a guild
I'm making a bot for my Discord server using discord.py . I'm trying to make a command that gives a user a role named "Muted". But when I try and mute the user (by sending "0mute #user#0000"), I get errors.
My code for giving the role is this:
#client.command(pass_context=True)
#commands.has_any_role("Admin")
async def mute(ctx, user: discord.Member):
await user.add_roles("Muted", atomic=False)
Note: I'm using regular discord.py, NOT discord.py rewrite.
Edit:
Recently found out I'm not allowed to post images of errors or code, as such I'll past the errors here as text instead of an image.
Ignoring exception in command mute:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 43, in mute
await user.add_roles("Muted", atomic=False)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 669, in add_roles
new_roles = utils._unique(Object(id=r.id) for s in (self.roles[1:], roles) for r in s)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/utils.py", line 287, in _unique
return [x for x in iterable if not (x in seen or adder(x))]
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/utils.py", line 287, in <listcomp>
return [x for x in iterable if not (x in seen or adder(x))]
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 669, in <genexpr>
new_roles = utils._unique(Object(id=r.id) for s in (self.roles[1:], roles) for r in s)
AttributeError: 'str' object has no attribute 'id'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'id'
Member.add_roles takes discord.Role instances as the arguments, you're passing a string.
#client.command()
#commands.has_any_role("Admin")
async def mute(ctx, user: discord.Member):
role = discord.utils.get(ctx.guild.roles, name="Muted") # Getting the role
await user.add_roles(role, atomic=False)
Also the pass_context kwarg is not necessary in discord.py rewrite, the context is always passed
Reference:
utils.get
Member.add_roles