Skip to content

Commit

Permalink
Add config file and private roles (#32)
Browse files Browse the repository at this point in the history
* Add config file support

* Add roles cog

* Move into `src` folder and update docker build

* Add additional config options
  • Loading branch information
WhiteHoodHacker authored Aug 30, 2024
1 parent bbcdbd9 commit 864523f
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ debug*
*.sw*
__pycache__/
.vscode/

# config file
config.yml
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FROM python:3.10
# Change into the source directory
WORKDIR /bot

COPY . .
COPY src .

RUN pip install -r requirements.txt

Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ services:
- CTF_CATEGORY_CHANNELS
- CTF_ROLES
- UIUC_ROLES
volumes:
- ./.env:/bot/.env
- ./config.yml:/bot/config.yml
11 changes: 0 additions & 11 deletions lib/config.py

This file was deleted.

1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
73 changes: 73 additions & 0 deletions src/cogs/roles/roles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import interactions
from interactions import Extension, SlashContext

from lib.util import subcommand
from lib.config import UIUC_ROLES, PRIVATE_ROLES


class Roles(Extension):
'''Commands for managing private roles'''

def __init__(self, _):
self.roles = PRIVATE_ROLES

@subcommand(role={"description": "Role to add","autocomplete": True})
async def add(self, ctx: SlashContext, role: str) -> None:
"""Add a private role. Requires the UIUC role."""
if (ctx.guild == None):
await ctx.send(":x: You can only run this command in a server.")
return
user = ctx.guild.get_member(ctx.user.id)
if (user == None):
await ctx.send(":x: You aren't in the server! Are you a ghost?")
return
if (not any(user.has_role(role) for role in UIUC_ROLES)):
await ctx.send(":x: You need to be UIUC verified to use this command. Verify yourself at <https://sigpwny.com/auth>.")
for valid_role in self.roles:
valid_role_name = valid_role.get("name")
valid_role_id = valid_role.get("discord_role_id")
if not valid_role_name or not valid_role_id:
continue
if role == valid_role_name:
if user.has_role(valid_role_id):
await ctx.send(f":x: You already have the **{role}** role.")
return
await user.add_role(valid_role_id)
await ctx.send(f":white_check_mark: Added you to **{role}**.")
return
await ctx.send(":x: Invalid role.")

@subcommand(role={"description": "Role to remove","autocomplete": True})
async def remove(self, ctx: SlashContext, role: str) -> None:
"""Removes a private role."""
if (ctx.guild == None):
await ctx.send(":x: You can only run this command in a server.")
return
user = ctx.guild.get_member(ctx.user.id)
if (user == None):
await ctx.send(":x: You aren't in the server! Are you a ghost?")
return
for valid_role in self.roles:
valid_role_name = valid_role.get("name")
valid_role_id = valid_role.get("discord_role_id")
if not valid_role_name or not valid_role_id:
continue
if role == valid_role_name:
if not user.has_role(valid_role_id):
await ctx.send(f":x: You do not have the **{role}** role.")
return
await user.remove_role(valid_role_id)
await ctx.send(f":white_check_mark: Removed you from **{role}**.")
return
await ctx.send(":x: Invalid role.")

@add.autocomplete("role")
@remove.autocomplete("role")
async def find_role(self, ctx: interactions.AutocompleteContext):
current_input = ctx.kwargs.get("role")
autocomplete_options = []
for valid_role in self.roles:
valid_role_name = valid_role.get("name")
if valid_role_name and current_input in valid_role_name:
autocomplete_options.append(valid_role_name)
await ctx.send(autocomplete_options[:25])
File renamed without changes.
27 changes: 27 additions & 0 deletions src/lib/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import yaml
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

config = {}
try:
with open("config.yml", "r") as f:
try:
config = yaml.safe_load(f)
except yaml.YAMLError as e:
logger.error(f"Error loading config: {e}")
exit(1)
except FileNotFoundError:
logger.warning("No config file found.")

DISCORD_TOKEN = os.getenv('DISCORD_TOKEN') or config.get("discord_token")
GUILD_IDS = os.getenv('GUILD_IDS', "").split(",") or config.get("guild_ids", [])
CTF_CATEGORY_CHANNELS = os.getenv("CTF_CATEGORY_CHANNELS", "").split(",") or config.get("ctf_category_channels", [])
CTF_ROLES = os.getenv("CTF_ROLES", "").split(",") or config.get("ctf_roles", [])
UIUC_ROLES = os.getenv("UIUC_ROLES", "").split(",") or config.get("uiuc_roles", [])
PRIVATE_ROLES = config.get("private_roles", [])

CHALLENGE_CATEGORIES = ["crypto", "forensics", "misc", "pwn", "osint", "rev", "web"]
FORUM_GENERAL_CHANNEL = "General"
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
discord-py-interactions==5.11.0
PyYAML==6.0.2

0 comments on commit 864523f

Please sign in to comment.