Skip to content

Commit

Permalink
Added role support for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanAkkerman committed Aug 1, 2024
1 parent ff4b896 commit 5fd4352
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
22 changes: 12 additions & 10 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,13 @@ LOOPS:
### COMMANDS ###
##################

# The options for ROLE are:
# - None (all users can use this command)
# - Admin (Only users with the discord admin role)
# You can also fill in a custom role name that you use in your server to only enable it for users that have that role
# If you want multiple roles then use a comma like `ROLE: Admin, Pro`

COMMANDS:
# Role options are: None and Admin
ANALYZE:
ENABLED: True
ROLE: None
Expand All @@ -244,10 +249,6 @@ COMMANDS:
ENABLED: True
ROLE: None

FOLLOW:
ENABLED: True
ROLE: None

HELP:
ENABLED: True
ROLE: None
Expand All @@ -256,15 +257,15 @@ COMMANDS:
ENABLED: True
ROLE: None

SENTIMENT:
RESTART:
ENABLED: True
ROLE: None
ROLE: Admin

STOCK:
SENTIMENT:
ENABLED: True
ROLE: None

RESTART:
STOCK:
ENABLED: True
ROLE: None

Expand All @@ -278,7 +279,8 @@ LISTENERS:
ON_RAW_REACTION_ADD:
ENABLED: True
CHANNEL: 💸┃highlights
ROLE: None
ROLE: Admin

# Sends a custom message when a member joins
ON_MEMBER_JOIN:
ENABLED: True
5 changes: 1 addition & 4 deletions src/cogs/commands/restart.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import os
import sys

import discord
import pandas as pd
from discord.commands import Option
from discord.commands.context import ApplicationContext
from discord.ext import commands

from util.disc_util import conditional_role_decorator
from util.vars import config, get_json_data, logger
from util.vars import config


class Restart(commands.Cog):
Expand Down
11 changes: 10 additions & 1 deletion src/cogs/listeners/on_raw_reaction_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,16 @@ async def on_raw_reaction_add(
):
await self.classify_reaction(reaction, message)
elif str(reaction.emoji) == "💸":
await self.highlight(message, reaction.member)
# Check if user has the role or is an admin
if config["LISTENERS"]["ON_RAW_REACTION_ADD"]["ROLE"] != "None":
if (
config["LISTENERS"]["ON_RAW_REACTION_ADD"]["ROLE"]
in reaction.member.roles
or reaction.member.guild_permissions.administrator
):
await self.highlight(message, reaction.member)
else:
await self.highlight(message, reaction.member)
elif str(reaction.emoji) == "❤️":
await self.send_dm(message, reaction.member)

Expand Down
9 changes: 8 additions & 1 deletion src/util/disc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ def noop_decorator(func):


def conditional_role_decorator(role: str):
if "," in role:
roles = role.split(",")
# remove whitespace
roles = [r.strip() for r in role]
# capitalize
roles = [r.capitalize() for r in role]
return commands.has_any_role(*roles)

role = role.capitalize()
if role == "Admin":
return commands.has_permissions(administrator=True)
if role == "None":
return noop_decorator

return commands.has_role(role)
# multiple roles: commands.has_any_role("Big Cheese", "Medium Cheese")


def get_guild(bot: commands.Bot) -> discord.Guild:
Expand Down
3 changes: 2 additions & 1 deletion src/util/yf_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@


def yf_info(ticker: str, do_format_change: bool = True):
# This can be blocking
try:
stock_info = Ticker(ticker, asynchronous=False).price
stock_info = Ticker(ticker, asynchronous=True).price
except Exception as e:
logger.error(f"Error in getting Yahoo Finance data for {ticker}: {e}")
return None
Expand Down

0 comments on commit 5fd4352

Please sign in to comment.