feat: Add Russian Roulette feature betwwen two players
Some checks failed
CD / Deploy to VPS (push) Has been cancelled
Some checks failed
CD / Deploy to VPS (push) Has been cancelled
This commit is contained in:
70
setup/bot.py
70
setup/bot.py
@@ -4,6 +4,7 @@ from discord.ext import bridge
|
||||
# from cogs.productivity import ProductivityCog
|
||||
from cogs.cs import CSCog
|
||||
from cogs.insult import InsultsCog
|
||||
from cogs.roulette import RouletteCog
|
||||
from setup.logger import LOGGER
|
||||
|
||||
bot_intents = discord.Intents.default()
|
||||
@@ -15,15 +16,70 @@ bot_intents.reactions = True
|
||||
|
||||
bot = bridge.Bot(command_prefix='$', intents=bot_intents)
|
||||
|
||||
COGS = [CSCog, InsultsCog]
|
||||
COG_REGISTRY = {
|
||||
'cs': CSCog,
|
||||
'insult': InsultsCog,
|
||||
'roulette': RouletteCog
|
||||
}
|
||||
|
||||
for cog in COGS:
|
||||
bot.add_cog(cog(bot))
|
||||
|
||||
def reload_feature(feature:str):
|
||||
def _normalize_cog_names(cogs=None):
|
||||
if cogs is None:
|
||||
return list(COG_REGISTRY.keys())
|
||||
|
||||
if isinstance(cogs, str):
|
||||
cogs = [cogs]
|
||||
|
||||
selected_cogs = []
|
||||
for cog_name in cogs:
|
||||
name = str(cog_name).strip().lower()
|
||||
if not name:
|
||||
continue
|
||||
if name in {'all', '*'}:
|
||||
return list(COG_REGISTRY.keys())
|
||||
if name in COG_REGISTRY:
|
||||
selected_cogs.append(name)
|
||||
else:
|
||||
LOGGER.warning("Unknown cog '%s'. Available cogs: %s", cog_name, ', '.join(COG_REGISTRY.keys()))
|
||||
|
||||
return list(dict.fromkeys(selected_cogs))
|
||||
|
||||
|
||||
def register_cogs(cogs=None):
|
||||
selected_cogs = _normalize_cog_names(cogs)
|
||||
|
||||
existing_cog_names = set(COG_REGISTRY.values())
|
||||
for existing_name in list(bot.cogs.keys()):
|
||||
if existing_name in {cog_class.__name__ for cog_class in existing_cog_names}:
|
||||
bot.remove_cog(existing_name)
|
||||
|
||||
for cog_name in selected_cogs:
|
||||
cog_class = COG_REGISTRY[cog_name]
|
||||
bot.add_cog(cog_class(bot))
|
||||
|
||||
return selected_cogs
|
||||
|
||||
|
||||
|
||||
def reload_feature(feature: str):
|
||||
normalized_feature = str(feature).strip().lower()
|
||||
LOGGER.debug(bot.cogs)
|
||||
|
||||
if normalized_feature in {'all', '*'}:
|
||||
register_cogs()
|
||||
LOGGER.debug('All cogs reloaded!')
|
||||
return
|
||||
|
||||
if normalized_feature not in COG_REGISTRY:
|
||||
LOGGER.warning("Unknown cog '%s'. Available cogs: %s", feature, ', '.join(COG_REGISTRY.keys()))
|
||||
return
|
||||
|
||||
LOGGER.debug(f'Reloading {feature}...')
|
||||
bot.remove_cog(feature.capitalize()+'Cog')
|
||||
cog = globals()[feature.capitalize()+'Cog']
|
||||
bot.add_cog(cog(bot))
|
||||
target_cog_name = COG_REGISTRY[normalized_feature].__name__
|
||||
for existing_name in list(bot.cogs.keys()):
|
||||
if existing_name == target_cog_name:
|
||||
bot.remove_cog(existing_name)
|
||||
break
|
||||
|
||||
bot.add_cog(COG_REGISTRY[normalized_feature](bot))
|
||||
LOGGER.debug(f'{feature} reloaded!')
|
||||
|
||||
18
setup/config.py
Normal file
18
setup/config.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
|
||||
class Config:
|
||||
load_dotenv()
|
||||
|
||||
TOKEN = str(os.getenv("TOKEN", ""))
|
||||
CONFIG_PATH = str(os.getenv("CONFIG_PATH"))
|
||||
COGS = os.getenv("COGS", "")
|
||||
|
||||
@classmethod
|
||||
def get_cogs(cls):
|
||||
raw_value = cls.COGS.strip()
|
||||
if not raw_value:
|
||||
return None
|
||||
return [item.strip().lower() for item in raw_value.split(",") if item.strip()]
|
||||
|
||||
Reference in New Issue
Block a user