Skip to content

Commit f7ad48b

Browse files
committed
checking in broken code for now
1 parent 7b0703a commit f7ad48b

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

requirements.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
aiohappyeyeballs==2.4.6
2+
aiohttp==3.11.13
3+
aiosignal==1.3.2
4+
attrs==25.1.0
5+
audioop-lts==0.2.1
6+
discord.py==2.5.0
7+
fire==0.7.0
8+
frozenlist==1.5.0
9+
idna==3.10
10+
multidict==6.1.0
11+
propcache==0.3.0
12+
setuptools==75.8.2
13+
termcolor==2.5.0
14+
yarl==1.18.3

setup.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from setuptools import setup
2+
3+
setup(
4+
name='dsb-discord-admin-cli',
5+
version='0.1',
6+
py_modules=['src'],
7+
install_requires=[
8+
'discord.py',
9+
'fire'
10+
],
11+
entry_points={
12+
'console_scripts': [
13+
'src = src:main'
14+
]
15+
},
16+
author='Damien Burks',
17+
description='CLI tool for DSB Discord admin tasks',
18+
classifiers=[
19+
'Programming Language :: Python :: 3',
20+
'Operating System :: OS Independent',
21+
]
22+
)

src/_init_.py

Whitespace-only changes.

src/client/_init_.py

Whitespace-only changes.

src/client/discord_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
import discord
3+
from discord.ext import commands
4+
5+
TOKEN = os.getenv("DISCORD_TOKEN")
6+
7+
class DiscordClient:
8+
def __init__(self):
9+
# Set up intents (ensure the "Server Members Intent" is enabled for your bot)
10+
intents = discord.Intents.default()
11+
intents.members = True
12+
13+
self.bot = commands.Bot(command_prefix="!", intents=intents)
14+
15+
def run(self):
16+
self.bot.run(TOKEN)
17+
18+
def close(self):
19+
self.bot.close()

src/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import fire
2+
3+
from tasks.role_assigner import DiscordRoleAssigner
4+
5+
def main():
6+
fire.Fire(DiscordRoleAssigner)
7+
8+
if __name__ == '__main__':
9+
main()

src/tasks/_init_.py

Whitespace-only changes.

src/tasks/role_assigner.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import asyncio
2+
import discord
3+
from client.discord_client import DiscordClient, TOKEN
4+
5+
class DiscordRoleAssigner:
6+
"""
7+
A class for assigning a role to every member in a Discord server.
8+
"""
9+
10+
def assign(self, guild: int, role: str, delay: float = 3.0):
11+
"""
12+
Assign a role to every member in a Discord server.
13+
14+
Args:
15+
guild (int): The Discord server's guild ID.
16+
role (str): The name of the role to assign.
17+
token (str): Your Discord bot token.
18+
delay (float, optional): Delay in seconds between role assignments (default: 3.0).
19+
"""
20+
client = DiscordClient()
21+
22+
@client.bot.event
23+
async def on_ready():
24+
print(f"Logged in as {client.bot.user}")
25+
26+
# Fetch the guild using the provided guild ID
27+
guild_obj = client.bot.get_guild(guild)
28+
if not guild_obj:
29+
print("Guild not found. Please check your guild ID.")
30+
await client.bot.close()
31+
return
32+
33+
# Retrieve the role by name
34+
role_obj = discord.utils.get(guild_obj.roles, name=role)
35+
if not role_obj:
36+
print("Role not found. Please check the role name.")
37+
await client.bot.close()
38+
return
39+
40+
print(f"Assigning role '{role_obj.name}' to all members of '{guild_obj.name}'...")
41+
42+
# Iterate through each member and add the role if they don't have it
43+
for member in guild_obj.members:
44+
if role_obj not in member.roles:
45+
try:
46+
await member.add_roles(role_obj)
47+
print(f"Added role to {member.name}#{member.discriminator}")
48+
await asyncio.sleep(delay)
49+
except Exception as e:
50+
print(f"Failed to add role to {member.name}#{member.discriminator}: {e}")
51+
52+
print("Finished processing all members.")
53+
await client.close()
54+
55+
client.run()

0 commit comments

Comments
 (0)