Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create bot.py #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Discord-Role-Giver/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import csv
import discord

async def assign_roles(csv_file, guild):
# Get the Member role
member_role = None
for role in guild.roles:
if role.name == "Member":
member_role = role
break

if not member_role:
print("Error: Member role not found")
return

with open(csv_file, 'r') as file:
reader = csv.reader(file)
next(reader) # Skip the header row if present

for row in reader:
name = row[0]
role_name = row[1]
role = discord.utils.get(guild.roles, name=role_name)

member = guild.get_member_named(name)
if not member:
print(f"Error: Member {name} not found in server")
continue

# Add specified role in addition to Member role if specified in CSV
if role:
await member.add_roles(role)
print(f"Assigned role {role.name} to {member.name}")
await member.add_roles(member_role)
print(f"Assigned role {member_role.name} to {member.name}")

async def on_message(message):
if message.content.startswith('!assign_roles'):
await assign_roles('roles.csv', message.guild)
await message.channel.send('Roles assigned successfully!')

# Example usage
client.run('your_token_here')