generated from pygame-community/pgcbots-bot-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize message validation system for showcase messages in 'showca…
…se' extension
- Loading branch information
Showing
6 changed files
with
1,429 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Collection | ||
import discord | ||
import snakecore | ||
|
||
from .utils import ShowcaseChannelConfig | ||
|
||
BotT = snakecore.commands.Bot | snakecore.commands.AutoShardedBot | ||
|
||
|
||
@snakecore.commands.decorators.with_config_kwargs | ||
async def setup( | ||
bot: BotT, | ||
showcase_channels_config: Collection[ShowcaseChannelConfig], | ||
theme_color: int | discord.Color = 0, | ||
): | ||
# validate showcase channels config | ||
for i, showcase_channel_config in enumerate(showcase_channels_config): | ||
if "channel_id" not in showcase_channel_config: | ||
raise ValueError("Showcase channel config must have a 'channel_id' key") | ||
elif ( | ||
"default_auto_archive_duration" in showcase_channel_config | ||
and not isinstance( | ||
showcase_channel_config["default_auto_archive_duration"], int | ||
) | ||
): | ||
raise ValueError( | ||
"Showcase channel config 'default_auto_archive_duration' must be an integer" | ||
) | ||
elif ( | ||
"default_thread_slowmode_delay" in showcase_channel_config | ||
and not isinstance( | ||
showcase_channel_config["default_thread_slowmode_delay"], int | ||
) | ||
): | ||
raise ValueError( | ||
"Showcase channel config 'default_thread_slowmode_delay' must be an integer" | ||
) | ||
elif "showcase_message_rules" not in showcase_channel_config: | ||
raise ValueError( | ||
"Showcase channel config must have a 'showcase_message_rules' key" | ||
) | ||
|
||
from .utils import dispatch_rule_specifier_dict_validator, BadRuleSpecifier | ||
|
||
specifier_dict_validator = dispatch_rule_specifier_dict_validator( | ||
showcase_channel_config["showcase_message_rules"] | ||
) | ||
|
||
# validate 'showcase_message_rules' value | ||
try: | ||
specifier_dict_validator( | ||
showcase_channel_config["showcase_message_rules"] # type: ignore | ||
) | ||
except BadRuleSpecifier as e: | ||
raise ValueError( | ||
f"Error while parsing config.{i}.showcase_message_rules field: {e}" | ||
) from e | ||
|
||
from .cogs import Showcasing | ||
|
||
await bot.add_cog(Showcasing(bot, showcase_channels_config, theme_color)) |
Oops, something went wrong.