""" Cog handling general configuration for a guild. """ from typing import Optional, Union from discord import Guild, Interaction, Role, TextChannel, User from discord.app_commands import Group from discord.ext.commands import Bot from config import CONFIG from rocketbot.cogs.basecog import BaseCog from rocketbot.storage import ConfigKey, Storage 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( description='Sets mod warnings to post in the current channel.', extras={ 'long_description': '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!**', }, ) 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( description='Shows the configured mod warning channel, if any.' ) async def get_warning_channel(self, interaction: Interaction) -> None: 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( description='Sets the user or role to tag in warning messages.', extras={ 'long_description': 'Calling this without a value disables tagging in warnings.', } ) async def set_warning_mention(self, interaction: Interaction, mention: Optional[Union[User, Role]] = None) -> None: """ Sets a user/role to mention in warning messages. 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( description='Shows the configured user or role tagged in warning messages.', ) async def get_warning_mention(self, interaction: Interaction) -> None: 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, )