Reputation: 17
I am looking for some guidance on how to resolve my current problem I've been stuck on for 2 days. The goal for this discord bot is check my CSV file and determine if the user has been on the server before. If there is record of the that particular member the bot will then reassign past roles upon the member rejoining the server again.
The error arises when it comes time for the bot to reassign the roles referenced from my CSV file. I have rechecked two things (1) my bot has the powers to assign roles (2) the roles I am attempting to assign exist. I keep getting this error:
2022-12-25 12:52:24 ERROR discord.client Ignoring exception in on_member_join Traceback (most recent call last): File "C:\Users\slade\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 409, in _run_event await coro(*args, **kwargs) File "c:\Users\slade\Desktop\Discord Role Manager\main.py", line 43, in on_member_join await member.add_roles(*roles) File "C:\Users\slade\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\member.py", line 1018, in add_roles await req(guild_id, user_id, role.id, reason=reason) File "C:\Users\slade\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 740, in request raise NotFound(response, data) discord.errors.NotFound: 404 Not Found (error code: 10011): Unknown Role
Here is my code:
import discord
import asyncio
import csv
import ast
intents = discord.Intents().all()
client = discord.Client(intents=intents)
async def background_task(member, role):
await asyncio.sleep(30) # 14 days in seconds
new_role = discord.utils.get(member.guild.roles, name="Trader")
await member.remove_roles(role)
await member.add_roles(new_role)
print(f'User {member} has been taken off trial')
@client.event
async def on_member_join(member):
print(f"User joined: {member.id}")
#Opens up the CSV file containing the member data base
with open("roles.csv", "r") as csv_file:
reader = csv.DictReader(csv_file)
print("CSV was opened")
#Loop that reads each role in the "reader" varialbe
for row in reader:
#runs the loop until it runs into the user's ID.
str_member = str(member.id) #converts member id to a string so it matches the CSV data type
if row[0] == str_member:
print(row[0])
print(str_member)
roles = [discord.utils.get_role(member.guild.roles, id=role_id) for role_id in ast.literal_eval(row[1])]
print(f"row[1]: {row[1]}")
print(type(row[1]))
print(f"Roles: {roles}")
roles = [role for role in roles if role is not None]
if roles:
await member.add_roles(*roles)
print(f"User found")
else:
print("No roles found")
#if the user is not found the bot assigns the member the "Insider Trial" role and adds a timer
role = discord.utils.get(member.guild.roles, name="Insider Trial")
await member.add_roles(role)
asyncio.create_task(background_task(member, role))
@client.event
async def on_member_remove(member):
#The var that we use to seek with
trial_removal = "Insider Trial"
roles = [role.name for role in member.roles]
print(f"{member.name} had the following roles: {', '.join(roles)}")
if trial_removal in roles:
with open("roles.csv", "a", newline="") as csv_file:
writer = csv.writer(csv_file)
writer.writerow([member.id, "934070928281841664"])
print("This user was on trial")
else:
with open("roles.csv", "a", newline="") as csv_file:
writer = csv.writer(csv_file)
writer.writerow([member.id, [role.id for role in member.roles]])
print("User's roles were recorded!")
client.wait_until_ready()
client.run('KEY')
Any help is appreciated!
Upvotes: 1
Views: 114
Reputation: 852
you should try to replace in line 43
await member.add_roles(*roles)
with
for role in roles:
await member.add_roles(*roles)
because I think only a single role can be added in one api request
Upvotes: 0