""" Cog handling general configuration for a guild. """ from typing import Union, Optional from discord import Guild, Permissions, TextChannel, Interaction, Role, User from discord.app_commands import Group from discord.ext.commands import Bot from config import CONFIG from rocketbot.storage import ConfigKey, Storage from rocketbot.cogs.basecog import BaseCog from rocketbot.utils import MOD_PERMISSIONS class ConfigCog(BaseCog, name='Configuration'): """ Cog for handling general bot configuration. """ def __init__(self, bot: Bot) -> None: super().__init__( bot, config_prefix='config', short_description='Manages general bot configuration.', ) config = Group( name='config', description='Manages general bot configuration.', guild_only=True, default_permissions=MOD_PERMISSIONS, ) @config.command() async def set_warning_channel(self, interaction: Interaction) -> None: """ Sets mod warnings to post in the current channel. Run this command in the channel where bot messages intended for server moderators should be sent. Other bot messages may still be posted in the channel a command was invoked in. If no output channel is set, mod-related messages will not be posted! """ guild: Guild = interaction.guild channel: TextChannel = interaction.channel Storage.set_config_value(guild, ConfigKey.WARNING_CHANNEL_ID, interaction.channel.id) await interaction.response.send_message( f'{CONFIG["success_emoji"]} Warning channel updated to {channel.mention}.', ephemeral=True, ) @config.command() async def get_warning_channel(self, interaction: Interaction) -> None: """ Shows the mod warning channel, if any. Shows the configured channel (if any) where mod warnings and other bot output will be posted. """ guild: Guild = interaction.guild channel_id = Storage.get_config_value(guild, ConfigKey.WARNING_CHANNEL_ID) if channel_id is None: await interaction.response.send_message( f'{CONFIG["info_emoji"]} No warning channel is configured.', ephemeral=True, ) else: channel = guild.get_channel(channel_id) await interaction.response.send_message( f'{CONFIG["info_emoji"]} Warning channel is configured as {channel.mention}.', ephemeral=True, ) @config.command() async def set_warning_mention(self, interaction: Interaction, mention: Optional[Union[User, Role]] = None) -> None: """ Sets a user/role to mention in warning messages. Configures an role or other prefix to include at the beginning of warning messages. The intent is to get the attention of certain users in case action is needed. Leave blank to tag no one. Parameters ---------- interaction: Interaction mention: User or Role The user or role to mention in warning messages """ guild: Guild = interaction.guild Storage.set_config_value(guild, ConfigKey.WARNING_MENTION, mention.mention if mention else None) if mention is None: await interaction.response.send_message( f'{CONFIG["success_emoji"]} Warning messages will not tag anyone.', ephemeral=True, ) else: await interaction.response.send_message( f'{CONFIG["success_emoji"]} Warning messages will now tag {mention.mention}.', ephemeral=True, ) @config.command() async def get_warning_mention(self, interaction: Interaction) -> None: """ Shows the configured user/role to mention in warning messages. """ guild: Guild = interaction.guild mention: str = Storage.get_config_value(guild, ConfigKey.WARNING_MENTION) if mention is None: await interaction.response.send_message( f'{CONFIG["info_emoji"]} No warning mention configured.', ephemeral=True, ) else: await interaction.response.send_message( f'{CONFIG["info_emoji"]} Warning messages will tag {mention}', ephemeral=True, )