Discord error: Unable to assign roles thorugh csv file
Traceback (most recent call last):
File "C:\Users\Ankita ghosh\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "c:\Users\Ankita ghosh\Desktop\Discord\bot.py", line 19, in on_ready
role =discord.utils.get(user.guild.roles, id=int(role_id))
AttributeError: 'NoneType' object has no attribute 'guild'
I am trying to make a Discord bot that assigns a user the roles according to a CSV file but I am getting this error.
Here is my code:
import discord
import csv
import discord.utils
from discord.ext import commands
import os
intents = discord.Intents.default()
client = discord.Client(command_prefix='!', intents=intents)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
with open('mycsvfile.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
user_id = row['user_id']
role_id = row['role_id']
user =discord.utils.find(lambda u:开发者_JAVA百科 u.id == int(user_id), client.get_all_members())
role =discord.utils.get(user.guild.roles, id=int(role_id))
await user.add_roles(role)
print(f'Role {role.name} has been added to {user.name}')
client.run(os.getenv('TOKEN'))
I tried to read the CSV file and assign roles according to it and got the same error.
精彩评论