-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config file and private roles (#32)
* Add config file support * Add roles cog * Move into `src` folder and update docker build * Add additional config options
- Loading branch information
1 parent
bbcdbd9
commit 864523f
Showing
17 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ debug* | |
*.sw* | ||
__pycache__/ | ||
.vscode/ | ||
|
||
# config file | ||
config.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
discord-py-interactions==5.11.0 | ||
PyYAML==6.0.2 |