I’ve been tormented by this error for a week, I seriously don’t understand what’s the matter, help plz
'int' object has no attribute 'id'
there is my code:
import discord
import random
import asyncio
from discord import Member
from discord.utils import get
from discord.ext import commands
from discord.ext.commands import Bot
from discord.ext.commands import check
from discord.ext.commands import has_role
ROLE = 730459135170183170
bot = commands.Bot(command_prefix='$')
#bot.command(pass_context = True)
#commands.has_role(730459135170183170)
async def use_shovel(ctx,user: discord.Member):
x = random.randint(1,50)
role = get(user.guild.roles, name =ROLE)
if x == 1:
await ctx.send("You've found REQUIEM ARROW!")
await bot.author.add_roles(731166197030322216)
else:
await ctx.send("meh, nothing")
await user.remove_roles(731166197030322216)
bot.run(TOKEN)
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "use_shove" is not found
Ignoring exception in command use_shovel:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "meh.py", line 24, in use_shovel
await user.remove_roles(731166197030322216)
File "C:\Python\lib\site-packages\discord\member.py", line 685, in remove_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'int' object has no attribute 'id'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Python\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Python\lib\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: 'int' object has no attribute 'id'
I really cannot understand why this error happen. So,how can I fix this?I
There are multiple errors in your code :
First, you're looking for a role with 730459135170183170 as a name, so your role variable's value will be None.
ROLE = 730459135170183170
(...)
role = get(user.guild.roles, name =ROLE)
Then, both add_roles and remove_roles take a role object as argument, not an int.
await bot.author.add_roles(731166197030322216)
(...)
await user.remove_roles(731166197030322216)
Finally, never ever share your token on any website:
bot.run('TOKEN')
You should go to your discord bot app and create a new token, so you're the only one who can use your bot app.
To solve your problem, you can get rid of your ROLE variable and replaces these lines:
#commands.has_role("Exact role name")
(...)
role = get(user.guild.roles, name="Exact role name")
(...)
await bot.author.add_roles(role)
(...)
await user.remove_roles(role)
Related
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 am trying to make a bot that gives a role to members when they join. However, it keeps coming up with the error message
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 lol
await member.add_roles(probation)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 764, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 248, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
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: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
I have given it the "manage roles permission (see below), but it still comes up with this error.
Is there a fix for this?
Also my python code:
import discord
from discord.ext import commands
from dotenv import load_dotenv
import os
import json
from datetime import datetime, timedelta
import asyncio
load_dotenv()
token = os.getenv('Gov')
intents=discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents = intents)
#bot.event
async def on_member_join(member):
join_guild = await bot.fetch_guild(793659768606425140)
probation = join_guild.get_role(838212050332549142)
await member.add_roles(probation)
I didn't see any problem on your code. You said bot has add roles permission but can you try to check the role that your bot gives to member is on top of your bot's role.
You could try:
#bot.event async def on_member_join(member):
channel =discord.utils.get(member.guild.text_channels,name="channelnamehere")
role = discord.utils.get(member.guild.roles, name='rolenamehere')
await member.add_roles(role)
await channel.send(f"{member.mention} welcome, you have been assigned {role}!")
This should work without having to pull channel IDs and whatnot!
If this doesn't work, my best advice would be checking that your bot is above all the roles you are trying to assign!
I have a bot that sends dm pings to a user whos Id is given as a parameter in a command.
This is the command:.spam ID #_OF_PINGS
I run into the following error:
Ignoring exception in command spam:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "bot.py", line 16, in spam
await ctx.send(f'Started pinging {user.name} {num} times.')
AttributeError: 'NoneType' object has no attribute 'name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/app/.heroku/python/lib/python3.6/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 'name'
Here is my code:
from discord.ext import commands
token = 'MyBotTokenHere'
prefix = '.'
client = commands.Bot(command_prefix=prefix)
client.remove_command("help")
#client.event
async def on_ready():
print('Ready')
#client.command()
async def spam(ctx, id1: int, num: int):
user = client.get_user(id1)
await ctx.send(f'Started pinging {user.name} {num} times.')
for i in range(num):
await user.send(f'<#{str(id1)}>')
await ctx.send(f'Finished {num} pings for {user.name}')
client.run(token)
It was working fine yesterday, but today, it broke down for some reason.
How do I fix it?
P.S. I hosted it on Heroku
You can use a converter for this:
#client.command()
async def spam(ctx, user: discord.User, num: int):
await ctx.send(f'Started pinging {user.name} {num} times.')
for i in range(num):
await user.send(f'<#{str(id1)}>')
await ctx.send(f'Finished {num} pings for {user.name}')
This way, Discord will automatically try to get the discord.User instance that corresponds to the id that you pass as an argument, and you can also #mention someone if you want to & it'll still work.
Also, starting from Discord 1.5.0, you now need to pass in intents when initializing a bot, which you clearly aren't doing yet. Update your Discord, and use the following to initialize your bot:
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=prefix, intents=intents)
More info on the how's and why's of intents in the relevant API documentation. You'll also want to enable members on your bot's Privileged Intents page, which is explained in the linked API docs as well.
I want to make a discord bot. Here is what i want: When i say '!yoklama 11.10.2020.txt'(or something) bot sends me a list of participants of which voice channel i am in.[not 1 channel(which i am in)] But i dont know how can i do.
Then i was some research and i find similar things about my request.
But they did not work properly.
Here is my found codes:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.command()
async def attendance(ctx, filename):
channel = client.get_channel(755129236397752441) # Replace with voice channel ID
with open(filename, 'w') as file:
for member in channel.members:
file.write(member.name + '\n')
client.run('mytoken')
This codes are working(when i run they dont give me error) but when i say !attendence test.txt bot
does not say anything and my python shell say to me:
Ignoring exception in command attendance:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(args, **kwargs)
File "C:\bot2\bot2.py", line 10, in attendance
for member in channel.members:
AttributeError: 'NoneType' object has no attribute 'members'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 859, in invoke
await injected(ctx.args, **ctx.kwargs)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\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: AttributeError:
'NoneType' object has no attribute 'members
You see guys i dont understand anything please help me.
I found these question but it does not work for me:
Discord Python bot creating a file of active member in a vocal channel
#client.command()
async def test(ctx):
guild = ctx.guild
channel = discord.utils.get(guild.voice_channels, name='General')
for member in channel.members:
print('test')
What I would do instead of having to pass in the file in the command, is just in the function writing:
with open("file.txt", "w") as f:
So I am working on a Discord Bot and trying to work on role assignments. I made sure that I am using the new rewrite system, I made sure I gave the bot administrative privileges. I have taken just about every measure I could think of and it simply is not working. I looked at the updated API to handle it, used sample code as a framework, This is the code
import os
import discord
from discord.utils import get as dget
from discord.ext.commands import Bot
from dotenv import load_dotenv
load_dotenv('key.env')
TOKEN = os.getenv('DISCORD_TOKEN')
client = Bot(command_prefix = '!')
#client.event
async def on_ready():
print('Connected')
#client.command()
async def role(ctx):
user = ctx.message.author
role = discord.utils.get(ctx.guild.roles, name="Sample")
await user.add_roles(role)
client.run(TOKEN)
and this is the error that i get:
Ignoring exception in command role:
Traceback (most recent call last):
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "d:/Google Drive/Coding/Khasbot/main.py", line 22, in role
await user.add_roles(role)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 221, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\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: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
I honestly have no idea what could be going wrong. I have tried endless things and even created a whole new bot altogether. Still the same issue. It worked last night and literally just won't do it.
Make sure that your bot must have Manage Roles permission on your server and the role for adding roles must be lower than your bot top role.
Make you did these 2 things.