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
Related
I tried to make a purge command, here is my code:
#bot.command(pass_context=True)
#commands.has_permissions(administrator=True)
async def purge(ctx, limit: int):
await ctx.channel.purge(limit=limit)
await ctx.message.delete()
await ctx.send(f'Deleted {limit} messages. Done by {ctx.author.mention}.')
await asyncio.sleep(2)
await bot.delete.message.most_recent()
but when i try to run the command, it deletes the messages but gives this error when it tries to delete the message:
Ignoring exception in command purge:
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 312, in purge
await ctx.message.delete()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/message.py", line 1023, in delete
await self._state.http.delete_message(self.channel.id, self.id)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 250, in request
raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message
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: NotFound: 404 Not Found (error code: 10008): Unknown Message
Any help?
You can try this instead:
#bot.command(pass_context=True)
#commands.has_permissions(administrator=True)
async def purge(ctx, limit: int):
await ctx.channel.purge(limit=limit)
await ctx.message.delete()
await ctx.send(f'Deleted {limit} messages. Done by {ctx.author.mention}.', delete_after = 2)
The delete_after deletes the message sent by bot after specific number of seconds.
Late answer but I think this might help you. Using your code all what you have to do is save the message that you just sent into a variable and then, delete that message.
#bot.command()
#commands.has_permissions(administrator=True)
async def purge(ctx, limit: int):
await ctx.channel.purge(limit=limit)
await ctx.message.delete()
message = await ctx.send(f'Deleted {limit} messages. Done by {ctx.author.mention}.')
await asyncio.sleep(2)
await message.delete()
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
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
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
i am making a discord music bot with a lot of other features and i am getting this error all the time
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
connect = await channel.connect()
await connect(channel)
#client.command(pass_context=True)
async def play(ctx, url):
voice_client = ctx.guild.voice_client
server = ctx.message.guild
voice_client = ctx.guild.voice_client(server)
player = await voice_client.create_ytdl_player(url)
players[server.id] = player
player.start()
this is the log line/terminal:
Ignoring exception in command play:
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 55, in play
voice_client = ctx.guild.voice_client(server)
TypeError: 'NoneType' object is not callable
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 855, 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: TypeError: 'NoneType' object is not callable
Can somebody help me with this thx.
You are trying to call guild.voice_client which is a property, not a method from what I can see in the docs.
You are getting a 'NoneType' object is not callable error because your guild object does not have a VoiceClient associated with it.
If you look at the docs for VoiceClient it looks like you retrieve one via VoiceChannel.connect()