unmute member in all servers they are in - python

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

Related

Discord.py error while trying to create a private channel

I wanted to make a private channel when a user runs a simple command. The code is below:
#client.command(name='start')
async def createChannel(ctx):
guild = ctx.guild
member = ctx.author
admin_role = get(guild.roles, name = "Admin")
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True),
admin_role: discord.PermissionOverwrite(read_messages=True)
}
try:
channel = await guild.create_text_channel('{}_sentinel'.format(ctx.author), overwrites=overwrites)
except:
print("Error")
Full traceback:
Ignoring exception in command start:
Traceback (most recent call last):
File "/Users/raveeshyadav/Raveesh/sentinel/sentinel/lib/python3.9/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/Users/raveeshyadav/Raveesh/sentinel/bot.py", line 44, in createChannel
channel = await guild.create_text_channel('{}_sentinel'.format(ctx.author), overwrites=overwrites)
File "/Users/raveeshyadav/Raveesh/sentinel/sentinel/lib/python3.9/site-packages/discord/guild.py", line 948, in create_text_channel
data = await self._create_channel(name, overwrites, ChannelType.text, category, reason=reason, **options)
File "/Users/raveeshyadav/Raveesh/sentinel/sentinel/lib/python3.9/site-packages/discord/guild.py", line 844, in _create_channel
'id': target.id
AttributeError: 'NoneType' object has no attribute 'id'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/raveeshyadav/Raveesh/sentinel/sentinel/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/Users/raveeshyadav/Raveesh/sentinel/sentinel/lib/python3.9/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/Users/raveeshyadav/Raveesh/sentinel/sentinel/lib/python3.9/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'
Screenshot:
This error is being generated by passing an key with a value of None in overwrites.
It's probably admin_role.
You can check:
You actually have a role named Admin on the guild;
You have activated guild intents so that you can have the list of roles.

I am getting an error code when i try use (member.id)

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

Discord.py sudo all command not responding

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

"AttributeError: 'str' object has no attribute 'id'" error on discord.py

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

How to change permissions of all channels of a guild with discord.py?

I want to change permissions of all channels of a server from Read messages = True to False. Basically I don't want users with default-role to be able to see any of the channel.
What I wrote:
#bot.command()
async def maintainance(ctx):
channel = ctx.guild.channels
perms = channel.overwrites_for(ctx.guild.default_role)
perms.read_messages=False
for channel in ctx.guild.channels:
await channel.set_permissions(ctx.guild.default_role, overwrite=perms)
await ctx.message.add_reaction(emoji="<a:tick:748476262640779276>")
error I get:
Ignoring exception in command maintainance:
Traceback (most recent call last):
File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\Rohit\Desktop\discord bots\tutorial bot\bot.py", line 23, in maintainance
perms = channel.overwrites_for(ctx.guild.default_role)
AttributeError: 'list' object has no attribute 'overwrites_for'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'list' object has no attribute 'overwrites_for'
I don't know what I did wrong.
Overwrites are per channel so you need to do the get_overwrites inside the for loop, some psuedocode would look like
for every channel:
overwrites = get_overwrites()
overwrites.send_message = False
channel.set_permissions(default_role, overwrites=overwrites)
Another thing is that you should do ctx.message.add_reaction() outside the loop to avoid trying to add the same reaction multiple times

Categories

Resources