Skip to content

Commit c81710b

Browse files
feat: rewrite configuration setting to use modals
1 parent 87d1cf6 commit c81710b

File tree

2 files changed

+54
-31
lines changed

2 files changed

+54
-31
lines changed

monty/config_metadata.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class ConfigAttrMetadata:
6969
def __post_init__(self):
7070
if self.type not in (str, int, float, bool):
7171
raise ValueError("type must be one of str, int, float, or bool")
72+
if len(self.name) > 45:
73+
raise ValueError("name must be less than 45 characters")
74+
if len(self.description) > 100:
75+
raise ValueError("description must be less than 100 characters")
7276

7377

7478
METADATA: dict[str, ConfigAttrMetadata] = dict( # noqa: C408
@@ -92,7 +96,7 @@ def __post_init__(self):
9296
git_file_expansions=ConfigAttrMetadata(
9397
type=bool,
9498
name="GitHub/GitLab/BitBucket File Expansions",
95-
description="BitBucket, GitLab, and GitHub automatic file expansions.",
99+
description="Whether to automatically expand links to specific lines for GitHub, GitLab, and BitBucket",
96100
long_description=(
97101
"Automatically expand links to specific lines for GitHub, GitLab, and BitBucket when possible."
98102
),

monty/exts/backend/guild_config.py

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import asyncio
34
import dataclasses
45
import inspect
56
from typing import Literal, Optional, Union
@@ -146,7 +147,6 @@ async def set_command(
146147
self,
147148
inter: disnake.GuildCommandInteraction,
148149
option: str,
149-
value: str,
150150
) -> None:
151151
"""
152152
[BETA] Edit the specified config option to the provided value.
@@ -164,6 +164,54 @@ async def set_command(
164164
if metadata.requires_bot:
165165
self.require_bot(inter)
166166

167+
# determine components
168+
label_component = []
169+
if metadata.type is bool:
170+
label_component = disnake.ui.StringSelect(
171+
custom_id="value",
172+
options=[
173+
disnake.SelectOption(label="Enabled", value="true", default=old is True),
174+
disnake.SelectOption(label="Disabled", value="false", default=old is False),
175+
],
176+
required=True,
177+
max_values=1,
178+
min_values=1,
179+
)
180+
else:
181+
label_component = disnake.ui.TextInput(
182+
custom_id="value",
183+
style=disnake.TextInputStyle.paragraph,
184+
required=True,
185+
value=old,
186+
)
187+
188+
modal = disnake.ui.Modal(
189+
title="Set Configuration Option",
190+
custom_id=f"config:set:{option_name}:{inter.id}",
191+
components=disnake.ui.Label(
192+
text=get_localised_response(inter, "{name}", name=metadata.name),
193+
description=get_localised_response(inter, "{name}", name=metadata.description),
194+
component=label_component,
195+
),
196+
)
197+
198+
await inter.response.send_modal(modal)
199+
200+
try:
201+
inter: disnake.ModalInteraction = await self.bot.wait_for(
202+
"modal_submit",
203+
check=lambda i: i.custom_id == f"config:set:{option_name}:{inter.id}"
204+
and i.author.id == inter.author.id
205+
and i.guild_id == inter.guild_id,
206+
timeout=300,
207+
)
208+
except asyncio.TimeoutError:
209+
return
210+
211+
value = inter.values["value"]
212+
if isinstance(value, list):
213+
value = value[0]
214+
167215
try:
168216
# convert the value with the metadata.type
169217
param = inspect.Parameter(option_name, kind=inspect.Parameter.KEYWORD_ONLY)
@@ -277,35 +325,6 @@ async def clear_command(
277325
ephemeral=True,
278326
)
279327

280-
@set_command.autocomplete("value")
281-
async def set_value_autocomplete(
282-
self,
283-
inter: disnake.CommandInteraction,
284-
value: str,
285-
*,
286-
option: str = None,
287-
) -> Union[dict[str, str], list[str]]:
288-
"""Show autocomplete for setting a config option."""
289-
if not option:
290-
return ["Please fill out the option parameter with a valid option."]
291-
292-
try:
293-
metadata = METADATA[option]
294-
except KeyError:
295-
try:
296-
_, metadata = await config_option(inter, option=option)
297-
except commands.UserInputError:
298-
return ["Please fill out the option parameter with a valid option."]
299-
300-
if metadata.type is bool:
301-
return {
302-
"Enabled": "True",
303-
"Disabled": "False",
304-
get_localised_response(inter, "{name}", name=metadata.description): "_",
305-
}
306-
307-
return [value or get_localised_response(inter, "{name}", name=metadata.description)]
308-
309328
@set_command.autocomplete("option")
310329
@clear_command.autocomplete("option")
311330
@view_command.autocomplete("option")

0 commit comments

Comments
 (0)