Skip to content

Commit 81051da

Browse files
committed
Add options help
1 parent 9474950 commit 81051da

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

bot/cogs/config.py

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime
44
import difflib
55
from enum import Enum
6+
from pathlib import Path
67
from typing import (
78
TYPE_CHECKING,
89
Annotated,
@@ -29,6 +30,7 @@
2930
check_permissions,
3031
is_manager,
3132
)
33+
from libs.utils.config import OptionsHelp
3234
from libs.utils.embeds import CooldownEmbed, Embed
3335
from libs.utils.pages import SimplePages
3436
from libs.utils.pages.paginator import RoboPages
@@ -37,12 +39,14 @@
3739

3840
if TYPE_CHECKING:
3941
from cogs.tickets import Tickets
42+
4043
from rodhaj import Rodhaj
4144

4245

4346
UNKNOWN_ERROR_MESSAGE = (
4447
"An unknown error happened. Please contact the dev team for assistance"
4548
)
49+
OPTIONS_FILE = Path(__file__).parents[1] / "locale" / "options.json"
4650

4751

4852
class BlocklistTicket(NamedTuple):
@@ -347,6 +351,7 @@ def __init__(self, bot: Rodhaj) -> None:
347351
"anon_reply_without_command",
348352
"anon_snippets",
349353
]
354+
self.options_help = OptionsHelp(OPTIONS_FILE)
350355

351356
@property
352357
def display_emoji(self) -> discord.PartialEmoji:

bot/libs/utils/config.py

+37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pathlib import Path
22
from typing import Any, Generic, Optional, TypeVar, Union, overload
33

4+
import orjson
45
import yaml
56

67
_T = TypeVar("_T")
@@ -44,3 +45,39 @@ def __len__(self) -> int:
4445

4546
def all(self) -> dict[str, Union[_T, Any]]:
4647
return self._config
48+
49+
50+
class OptionsHelp(Generic[_T]):
51+
def __init__(self, path: Path):
52+
self.path = path
53+
self._config: dict[str, Union[_T, Any]] = {}
54+
self.load_from_file()
55+
56+
def load_from_file(self) -> None:
57+
try:
58+
with open(self.path, "r") as f:
59+
self._config: dict[str, Union[_T, Any]] = orjson.loads(f.read())
60+
except FileNotFoundError:
61+
self._config = {}
62+
63+
@overload
64+
def get(self, key: Any) -> Optional[Union[_T, Any]]: ...
65+
66+
@overload
67+
def get(self, key: Any, default: Any) -> Union[_T, Any]: ...
68+
69+
def get(self, key: Any, default: Any = None) -> Optional[Union[_T, Any]]:
70+
"""Retrieves a config entry."""
71+
return self._config.get(str(key), default)
72+
73+
def __contains__(self, item: Any) -> bool:
74+
return str(item) in self._config
75+
76+
def __getitem__(self, item: Any) -> Union[_T, Any]:
77+
return self._config[str(item)]
78+
79+
def __len__(self) -> int:
80+
return len(self._config)
81+
82+
def all(self) -> dict[str, Union[_T, Any]]:
83+
return self._config

bot/locale/options.json

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"guild_age": {
3+
"default": "2 days",
4+
"description": "Sets the default age required for an guild in order to use Rodhaj",
5+
"examples": [
6+
"`config set-age guild 2 days`"
7+
],
8+
"notes": []
9+
},
10+
"account_age": {
11+
"default": "2 hours",
12+
"description": "Sets the default age required for an account that has joined the guild that Rodhaj is operating on",
13+
"examples": [
14+
"`config set-age account 2 days`"
15+
],
16+
"notes": []
17+
},
18+
"mention": {
19+
"default": "@here",
20+
"description": "Sets the default mention to use when new tickets are created",
21+
"examples": [
22+
"`config set mention @here`"
23+
],
24+
"notes": ["Pass in an empty string in order to disable this"]
25+
},
26+
"anon_replies": {
27+
"default": false,
28+
"description": "Enables the anonymous replying feature of Rodhaj. This affects the guild itself.",
29+
"examples": [
30+
"`config toggle anon_replies false`"
31+
],
32+
"notes": []
33+
},
34+
"anon_reply_without_command": {
35+
"default": false,
36+
"description": "Anonymously reply without using the command",
37+
"examples": [
38+
"`config toggle anon_reply_without_command true`"
39+
],
40+
"notes": []
41+
},
42+
"anon_snippets": {
43+
"default": false,
44+
"description": "Enable the usage of sending snippets anonymously",
45+
"examples": [
46+
"`config toggle anon_snippets true`"
47+
],
48+
"notes": []
49+
}
50+
}

0 commit comments

Comments
 (0)