Skip to content

Commit

Permalink
Added script for bulk assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieMc0 committed Oct 8, 2024
1 parent 0dfaf52 commit 2057715
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ cython_debug/
#.idea/


# Input Files
user_ids.txt

# Output Files
*report.csv
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
47 changes: 47 additions & 0 deletions bulk-assign-role.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 2057715

Please sign in to comment.