So I am new to Python, Discord.py, and programming in general, but I am using Discord as a good opportunity to learn. So please excuse any illiteracy on my part.
I have assembled a bot from bits and pieces here and there on the Internet, and it’s working as intended. But I am now stumped when trying to add a specific function.
For context, the Discord server I am an admin of is a video game publishing server, so we use it for beta test and feedback collection. For an upcoming beta test, we would like to automatically add a "beta-test" role to a list of users (a .csv file), that will give them access to specific channels and commands.
The issue is with the file reading part. I can get the bot from opening the .csv file and printing or sending its content to a specific channel BUT it does not recognize any printed name as a server member.
So first, here is the code:
discord.Intents.default()
@bot.command(name='addrole', hidden=True)
@has_permissions(administrator=True)
async def add_beta_role(ctx, role='beta-test'):
with open('./beta_list.csv', 'r+') as beta_role_file:
for item in beta_role_file:
print(f'{item}')
guild = discord.utils.find(lambda g : g.id == ctx.guild, bot.guilds)
member = discord.utils.find(lambda m : m.id == item, ctx.guild.members)
if role is not None:
if member is not None:
await member.add_roles()
print(f'{role} added to {member}')
else:
print(f'Member not found.')
else:
print(f'Role not found.')
The .csv file that I use contains 4 lines, for 4 different users, with differently formatted names:
The output I have is:
User1
Member not found.
User2
Member not found.
User3
Member not found.
User4
Member not found.
I would like the bot to be able to find the server members based on the list of users in the .csv file, but it does not work. I have tried to modify the code so that the bot would first send the list to a Discord channel and then use the message content to find the Discord members, but I have had no better luck.
If anyone could point out how to do it or where the issue lies, I would be very grateful
edit: edited for clarity
You can't just iterate over a csv file to get the items. Python has a great built-in csv library to do that.
import csv
with open('./beta_list.csv', 'r+') as beta_role_file:
reader = csv.reader(beta_role_file)
for row in reader:
print(row)
Adapt this code with your csv file