Skip to content

Commit

Permalink
migrate moderation to folder
Browse files Browse the repository at this point in the history
  • Loading branch information
solumath authored and peterdragun committed Mar 23, 2024
1 parent 1dc18a5 commit cdbfcfe
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 49 deletions.
7 changes: 7 additions & 0 deletions cogs/dynamicconfig/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from disnake.ext.commands import Bot

from .cog import DynamicConfig


def setup(bot: Bot):
bot.add_cog(DynamicConfig(bot))
59 changes: 26 additions & 33 deletions cogs/dynamicconfig.py → cogs/dynamicconfig/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,37 @@
import re
import shlex
from datetime import datetime
from pathlib import Path
from typing import List

import disnake
import toml
from disnake.ext import commands

from cogs.base import Base
from config.app_config import config_get_keys, load_config
from config.messages import Messages
from config.app_config import CONFIG_PATH, config_get_keys, load_config
from permissions import permission_check

from . import features
from .messages_cz import MessagesCZ

async def autocomp_keys(inter: disnake.ApplicationCommandInteraction, user_input: str) -> List[str]:

async def autocomp_keys(inter: disnake.ApplicationCommandInteraction, user_input: str) -> list[str]:
return [key for key in config_get_keys() if user_input.lower() in key][:25]


class DynamicConfig(Base, commands.Cog):
def __init__(self, bot: commands.Bot):
super().__init__()
self.bot = bot
# /rubbergod/cogs/dynamicconfig.py -> /rubbergod/config
self.config_dir = Path(__file__).parent.parent.joinpath("config")
self.config_path = self.config_dir.joinpath("config.toml")

@commands.check(permission_check.is_bot_admin)
@commands.slash_command(name="config")
async def config_cmd(self, inter):
async def config_cmd(self, inter: disnake.ApplicationCommandInteraction):
"""
Group of commands for dynamically changing config
"""
pass

@config_cmd.sub_command(name="set", description=Messages.config_set_brief)
@config_cmd.sub_command(name="set", description=MessagesCZ.set_brief)
async def set_value(
self,
inter: disnake.ApplicationCommandInteraction,
Expand All @@ -48,10 +45,10 @@ async def set_value(
"""
Dynamically change config values
"""
await self.change_value(inter, key, value, False)
await features.change_value(self, inter, key, value, False)
load_config()

@config_cmd.sub_command(description=Messages.config_append_brief)
@config_cmd.sub_command(description=MessagesCZ.append_brief)
async def append(
self,
inter: disnake.ApplicationCommandInteraction,
Expand All @@ -62,24 +59,24 @@ async def append(
Append value(s) to existing config
For string and int types command has same behaviour as `set_value`.
"""
await self.change_value(inter, key, value, True)
await features.change_value(self, inter, key, value, True)
load_config()

@config_cmd.sub_command(description=Messages.config_load_brief)
@config_cmd.sub_command(description=MessagesCZ.load_brief)
async def load(self, inter: disnake.ApplicationCommandInteraction):
"""
Load config from `config.toml`
"""
load_config()
await inter.send(Messages.config_loaded)
await inter.send(MessagesCZ.config_loaded)

@config_cmd.sub_command(name="list", description=Messages.config_list_brief)
@config_cmd.sub_command(name="list", description=MessagesCZ.list_brief)
async def list_all(self, inter: disnake.ApplicationCommandInteraction, regex: str = None):
if regex is not None:
try:
regex = re.compile(regex)
except re.error as ex:
await inter.send(Messages.config_list_invalid_regex(regex_err=str(ex)))
await inter.send(MessagesCZ.list_invalid_regex(regex_err=str(ex)))
return

output = "```\n"
Expand All @@ -89,7 +86,7 @@ async def list_all(self, inter: disnake.ApplicationCommandInteraction, regex: st
output += "```"
await inter.send(output)

@config_cmd.sub_command(description=Messages.config_get_brief)
@config_cmd.sub_command(description=MessagesCZ.get_brief)
async def get(
self,
inter: disnake.ApplicationCommandInteraction,
Expand All @@ -99,13 +96,13 @@ async def get(
Get value of specified key
"""
if not hasattr(self.config, key) or key in self.config.config_static:
await inter.send(Messages.config_wrong_key)
await inter.send(MessagesCZ.wrong_key)
return
value = getattr(self.config, key)
embed = disnake.Embed(title=key, description=str(value))
await inter.send(embed=embed)

@config_cmd.sub_command(description=Messages.config_backup_brief)
@config_cmd.sub_command(description=MessagesCZ.backup_brief)
async def backup(self, inter: disnake.ApplicationCommandInteraction):
"""
Create backup from current config. Backup filename will contain current date.
Expand All @@ -114,9 +111,9 @@ async def backup(self, inter: disnake.ApplicationCommandInteraction):
backup_path = self.config_dir.joinpath(f"config_backup_{date}.toml")
with open(backup_path, "w+", encoding="utf-8") as fd:
toml.dump(self.config.toml_dict, fd)
await inter.send(Messages.config_backup_created)
await inter.send(MessagesCZ.backup_created)

@config_cmd.sub_command(description=Messages.config_sync_template_brief)
@config_cmd.sub_command(description=MessagesCZ.sync_template_brief)
async def update(self, inter: disnake.ApplicationCommandInteraction):
template_path = self.config_dir.joinpath("config.template.toml")
template = toml.load(template_path, _dict=dict)
Expand All @@ -127,10 +124,10 @@ async def update(self, inter: disnake.ApplicationCommandInteraction):
self.config.toml_dict[section][key] = template[section][key]
else:
self.config.toml_dict[section] = template[section]
with open(self.config_path, "w+", encoding="utf-8") as fd:
with open(CONFIG_PATH, "w+", encoding="utf-8") as fd:
toml.dump(self.config.toml_dict, fd)
load_config()
await inter.send(Messages.config_updated)
await inter.send(MessagesCZ.config_updated)

async def change_value(
self, inter: disnake.ApplicationCommandInteraction, key: str, value: str, append: bool
Expand All @@ -140,7 +137,7 @@ async def change_value(
If `append` values are appended to current setting.
"""
if not hasattr(self.config, key) or key in self.config.config_static:
await inter.send(Messages.config_wrong_key)
await inter.send(MessagesCZ.config_wrong_key)
return
try:
value = shlex.split(value)
Expand All @@ -160,7 +157,7 @@ async def change_value(
try:
value[idx] = int(item)
except ValueError:
await inter.send(Messages.config_wrong_type)
await inter.send(MessagesCZ.config_wrong_type)
return
if append:
value = attr + value
Expand All @@ -177,20 +174,16 @@ async def change_value(
try:
value = int(value[0])
except ValueError:
await inter.send(Messages.config_wrong_type)
await inter.send(MessagesCZ.config_wrong_type)
return
self.config.toml_dict[section][key_toml] = value
break
else:
key_toml = key
else:
await inter.send(Messages.config_wrong_key)
await inter.send(MessagesCZ.config_wrong_key)
return
setattr(self.config, key, value)
with open(self.config_path, "w+", encoding="utf-8") as fd:
toml.dump(self.config.toml_dict, fd)
await inter.send(Messages.config_updated)


def setup(bot):
bot.add_cog(DynamicConfig(bot))
await inter.send(MessagesCZ.config_updated)
17 changes: 17 additions & 0 deletions cogs/dynamicconfig/messages_cz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from config.messages import Messages as GlobalMessages


class MessagesCZ(GlobalMessages):
set_brief = "Nastaví hodnotu v konfiguraci"
append_brief = "Přidá hodnotu do pole v konfiguraci"
load_brief = "Znovu načte třídu ze souboru. Pro aplikování změn je potřeba znovu načíst i cog"
list_brief = "Vypíše klíče konfigurace"
get_brief = "Získá hodnotu z konfigurace"
backup_brief = "Vytvoří záložní kopii konfigurace v novém souboru. Záloha bude obsahovat dnešní datum"
sync_template_brief = "Přidá nové klíče z template do konfigurace"
wrong_key = "Nesprávný klíč"
wrong_type = "Nesprávný typ"
backup_created = "Záloha konfigurace vytvořena."
list_invalid_regex = "Chybný regex\n`{regex_err}`"
config_updated = "Konfigurace aktualizována."
config_loaded = "Konfigurace načtena."
3 changes: 3 additions & 0 deletions config/app_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from datetime import timedelta
from pathlib import Path
from typing import Dict, List

import toml

CONFIG_PATH: Path = Path("config/config.toml").resolve()


def get_attr(toml_dict: dict, section: str, attr_key: str):
"""
Expand Down
16 changes: 0 additions & 16 deletions config/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,6 @@ class Messages(metaclass=Formatable):
cog_not_unloadable = "Toto rozšíření `{cog}` je neodebratelné."
cog_reloaded = "Rozšíření `{cog}` bylo načteno znovu."

# DYNAMICCONFIG
config_set_brief = "Nastaví hodnotu v konfiguraci"
config_append_brief = "Přidá hodnotu do pole v konfiguraci"
config_load_brief = "Znovu načte třídu ze souboru. Pro aplikování změn je potřeba znovu načíst i cog"
config_list_brief = "Vypíše klíče konfigurace"
config_get_brief = "Získá hodnotu z konfigurace"
config_backup_brief = "Vytvoří záložní kopii konfigurace v novém souboru. Záloha bude obsahovat dnešní datum"
config_sync_template_brief = "Přidá nové klíče z template do configu"
config_updated = "Config updated."
config_loaded = "Config loaded."
config_wrong_key = "Nesprávný klíč"
config_wrong_type = "Nesprávný typ"
config_backup_created = "Config backup created."
config_list_invalid_regex = "Chybný regex\n`{regex_err}`"
config_synced = "Config successfully synchronized."

# MEME REPOST
meme_repost_link = "[Odkaz na originál]({original_message_url}) v <#{original_channel}>"
meme_leaderboard_brief = "#better-memes leaderboard"
Expand Down

0 comments on commit cdbfcfe

Please sign in to comment.