I'm trying to make my bot give a role to someone that uses the command but it says "AttributeError: 'tuple' object has no attribute 'id'" - discord

I'm a complete beginner so can someone please explain me what is wrong here and how to fix it? Thank you
from discord.ext import commands
from discord.utils import get
#client.command()
async def reserve(message, *, stand):
channel = message.channel,
author = message.author,
guild = message.guild,
the_world = get(message.guild.roles,name="The World"),
if stand == 'The World':
await message.author.add_roles(the_world)
The error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'tuple' object has no attribute 'id'

Assuming as the rest of your bot runs, your provided code causes this error as you are using get(ctx.guild.roles,name="The World") instead, make sure to use discord.utils in your argument:
discord.utils.get(ctx.guild.roles,name="The World")
Additionally I've modified some more of your code, as a command, you don't need to pass message as your parameters, use ctx and it's attributes.
#client.command()
async def reserve(ctx, *, stand):
if stand == 'The World':
the_world = discord.utils.get(ctx.guild.roles,name = "The World"),
await ctx.author.add_roles(the_world)

Related

Unbanning a specific user

Important note: I'm not using the rewrite.
I've been trying to unban specific users on a message in discord.py with a command.
Example:
On the command "!unban," the bot unbans a user who was not mentioned in the command.
I've tried something like
#client.event
async def on_message(message):
if message.content == x:
await message.guild.unban('[User ID], *, reason=none')
which results in
await self._state.http.unban(user.id, self.id, reason=reason)
AttributeError: 'str' object has no attribute 'id'
If anybody could help me out, I'd appreciate it. I'm pretty lost.
The proper usage of that command is guild.unban(user, reason=None). The asterisk is not required in a call to the function, and =None indicates that reason is optional. If you do not have a reason:
In your case:
await message.guild.unban(userID)
Should do the job, so long as your user ID is in the format of a snowflake object (e.g. a User object for that user!)

If I want to remove an ID-specified role within a function, how can I do that?

If I want to remove an ID-specified role within a function, how can I do that?
#bot.command()
async def aa(ctx,member:discord.Member):
role_muted = discord.utils.get(ctx.guild.roles, name='test01')
await member.remove_roles(role_muted)
What I want is to specify an ID in a function like this.
#bot.command()
async def aa(ctx):
member = 414087251992117211 #ID User
role_muted = discord.utils.get(ctx.guild.roles, name='test01')
await member.remove_roles(role_muted)
how can i do it The reason I have to do this is because I will continue to develop such as When you receive this Role within 3 days if not online it will remove your Role.
Use Guild.get_member(id) to get a member by their ID. You can get the Guild either from the ctx if you want it to run in the guild where you call the command, or from a Guild ID that you store somewhere using Bot.get_guild(id).
If you want to pass the ID in as an argument to the function and have it convert to a member automatically, use a Converter.
Can you give an example? I tried to do as you said. but an error occurred
#bot.command()
async def aa(ctx):
member = discord.Guild.get_member(414087251992117211)
role_muted = discord.utils.get(ctx.guild.roles, name='test01')
await member.remove_roles(role_muted)
Error
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'remove_roles'
#UpFeR Pattamin, please comment on a reply instead of posting a new one. You're getting this error because you're not specifying which guild you are talking about. If you want to get the one that the command was executed in, I'm pretty sure you need to do ctx.guild.get_member().

'Member' object has no attribute 'hasPermission'

i wanted to make a bot to discord for delete messages easly. it works 3 months ago but now i got an error " 'Member' object has no attribute 'hasPermission' ". thanks for everyone to share opinion. have a nice day.
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = 'botcuk')
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Streaming(name="admin biseyler deniyor", url='https://www.youtube.com/watch?v=fw7L7ZO4z_A'))
if message.content.startswith('botcuksil'):
if (message.author.hasPermission('MANAGE_MESSAGES')):
args = message.content.split(' ')
if len(args) == 2:
if args[1].isdigit():
count = int(args[1]) + 1
deleted = await message.channel.purge(limit = count)
await message.channel.send('{} mesaj silindi'.format(len(deleted)-1))
It is throwing 'Member' object has no attribute 'hasPermission' this error because 'Member' does not have an attribute named 'hasPermission'. To fix your problem, I have re-written your code and also given some explanation below the code:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = 'botcuk')
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Streaming(name="admin biseyler deniyor", url='https://www.youtube.com/watch?v=fw7L7ZO4z_A'))
#client.event # we need to add and check for a client event
async def on_message(): # we need to execute the command when we recieve an message so we define a function called 'async def on_message():'
if message.content.startswith('botcuksil'):
if message.author.guild_permissions.manage_messages: # we need 'author.guild_permissions.(permission)' intead of 'if (message.author.hasPermission('MANAGE_MESSAGES'))'
args = message.content.split(' ')
if len(args) == 2:
if args[1].isdigit():
count = int(args[1]) + 1
deleted = await message.channel.purge(limit = count)
await message.channel.send('{} mesaj silindi'.format(len(deleted)-1))
I have not tested this yet, but according to me, your problem: 'Member' object has no attribute 'hasPermission' should be solved.
What you have done was message.author.hasPermission('MANAGE_MESSAGES') but message.author has no attribute like hasPermission.
Summary of what I have done:
I have added a client event (#client.event) and an async function on_message().
What it does is it checks if it is getting an message or not. If it detects a new message in any of the servers it is invited to, it triggers the code in on_message().
As I said: What you have done was message.author.hasPermission('MANAGE_MESSAGES') but message.author has no attribute like hasPermission.
Instead of writing that, we can use message.author.guild_permissions.manage_messages. It checks if the message author's server's permissions include "manage_messages". Hope my solution fixes your problem.
Always happy to help!
-sqd mountains

#commands.has_permissions(administrator=True) not working properly?

I am trying to make a simple moderator bot.
the code looks something like this.
import discord
from discord.ext import commands
from random import choice
import os
client = commands.Bot(command_prefix=commands.when_mentioned_or("%"), description='A simple Moderator bot')
def colour():
l = [ 1752220, 3066993, 3447003, 10181046, 15844367, 15105570, 15158332, 3426654, 16580705 ]
return choice(l)
#client.event
async def on_ready():
change_status.start()
print("The Bot is online!")
#client.command()
#commands.has_permissions(administrator=True)
async def kick(ctx, user : discord.Member = None, *,reason = "No reason provided"):
await user.kick(reason = reason)
await ctx.send("Kicked the user.")
#client.command()
#commands.has_permissions(administrator=True)
async def ban(ctx, user : discord.Member = None, *,reason = "No reason provided"):
await user.ban(reason = reason)
await ctx.send("Banned the user")
#client.command()
#commands.has_permissions(administrator=True)
async def warn(ctx, user : discord.Member = None, *,reason = "No reason provided"):
await user.send(f"You have been **Warned** by **{ctx.author.name}** in the **{ctx.guild.name}** for the reason: **{reason}** ")
await ctx.send("Warned the user")
client.run(os.environ['token'])
In this only warn command is working successfully rest all the commands throws an error which looks like this.
Ignoring exception in command kick:
Traceback (most recent call last):
File "main.py", line 140, in kick
await user.kick(reason = reason)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/member.py", line 524, in kick
await self.guild.kick(self, reason=reason)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/guild.py", line 1886, in kick
await self._state.http.kick(user.id, self.id, reason=reason)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/http.py", line 241, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
It is throwing a missing permission but I am the owner of the server and even added the bot as an admistrator.
Even tried by making an administrator role and giving all the permissions to it but still no luck.
This error is coming again and again.
Even if anyone know how to check between multiple roles like if anyone has administrator or kick member permission then do this, please let me know how to do that.
Thanks in advance for the help.
If any more information needed, do let me know.
The error you are getting on the last line from your console; discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions Is because the bot is missing permissions to perform these actions against the given permissions the bot currently has.
This could occur from the following;
Sufficient Bot permissions haven't been enabled from the invite link
The member has a higher or equal role than the bot
The #everyone role has no permissions
Moreover, these issues aren't at fault with the code you have written and is something that can be fixed in your server with the role hierarchy or bot invite link permissions.
I would also recommend comparing roles to make sure users can't attempt moderation on a higher level member in the first place, this could also help to mitigate the problem
With this, you can use >= and the top_role attribute which would compare the roles that the user compared to the member you are attempting to take moderation on, if your permissions are lower than the member you are attempting to moderate, it will prevent the rest of the code running. Here is a simple way of doing this,
if member.top_role >= ctx.author.top_role:
await ctx.send(f"You can only moderate members below your role")
return

Autorole discord bot python

Alright, what I need is to put autorole code to my bot but only for specific server.
So please, help me!
Here is the code so far but I made it only beacuse stack overflow can't accept my text body:
#client.event
async def on_member_join(member : discord.Member):
server = ctx.message.server
rolelol = client.get_role('770262439937048577')
if server == server.id:('753667215710224574'):
await client.add_roles(member, rolelol)
else:
await client.say('')
All I need is code for autorole this doesn't code i made doesn't really matter...
You have a few things wrong :
add_roles is a Member object method, so you have to use member.add_roles()
ctx isn't defined since you're not in a command, to get the discord guild object, you have to write member.guild
get_role() is a Guild object method so you need to write member.guild.get_role(id)
Your IDs need to be passed as int, not str.
Client objects don't have any say() method
With all thoses changes, your code would look like this :
#client.event
async def on_member_join(member : discord.Member):
role = member.guild.get_role(770262439937048577)
if member.guild.id == 753667215710224574:
await member.add_roles(role)
Reference : https://discordpy.readthedocs.io/en/latest/

Resources