diff --git a/.gitignore b/.gitignore index 1d0e6b4..6a3c76b 100644 --- a/.gitignore +++ b/.gitignore @@ -160,6 +160,8 @@ cython_debug/ #.idea/ +# Input Files +user_ids.txt # Output Files *report.csv diff --git a/README.md b/README.md index cdbbc13..f68d8af 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,6 @@ string. For example, kick any unverified users but ban anyone with "AIRDROP SCAM" in the username. For this script to work you must have connected your bot to the server with proper permissions + +## Bulk Assign +This takes an list of discord user Ids as an input and assigns a new role to them. Role is specificed with env variable - DISCORD_ROLE_ID_FOR_ASSIGNMENT or you can edit value in the script. \ No newline at end of file diff --git a/bulk-assign-role.py b/bulk-assign-role.py new file mode 100644 index 0000000..9ab7822 --- /dev/null +++ b/bulk-assign-role.py @@ -0,0 +1,47 @@ +import discord +import logging +import os +import csv + +TOKEN = os.environ['DISCORD_BOT_TOKEN'] +GUILD_NAME = os.environ['DISCORD_GUILD_NAME'] +GUILD_ID = os.environ['DISCORD_GUILD_ID'] +ROLE_ID = int(os.environ['DISCORD_ROLE_ID_FOR_ASSIGNMENT']) +USER_IDS_FILE = "user_ids.txt" + +logging.basicConfig(level=logging.INFO) +intents = discord.Intents.all() +client = discord.Client(intents=intents) + + +@client.event +async def on_ready(): + for guild in client.guilds: + if guild.name == GUILD_NAME or guild.id == GUILD_ID: + logging.debug( + f'{client.user} is running in the following server:\n{guild.name} (id: {guild.id})') + + role = discord.utils.get(guild.roles, id=ROLE_ID) + if not role: + logging.error(f"Role with ID '{ROLE_ID}' not found in the server.") + await client.close() + return + + with open(USER_IDS_FILE, 'r') as file: + reader = csv.reader(file) + for row in reader: + user_id = int(row[0]) + member = guild.get_member(user_id) + if member: + try: + # await member.add_roles(role) + logging.info(f"Assigned role with ID '{ROLE_ID}' to member {member.name} (id: {member.id})") + except Exception as e: + logging.error(f"An error occurred while assigning role to member {member.name} (id: {member.id}): {e}") + else: + logging.warning(f"Member with id {user_id} not found in the server.") + + await client.close() + + +client.run(TOKEN)