from discord import Guild, Message, TextChannel from discord.ext import commands from storage import ConfigKey, Storage import json class BaseCog(commands.Cog): def __init__(self, bot): self.bot = bot @classmethod async def warn(cls, guild: Guild, message: str) -> Message: """ Sends a warning message to the configured warning channel for the given guild. If no warning channel is configured no action is taken. Returns the Message if successful or None if not. """ channel_id = Storage.get_config_value(guild, ConfigKey.WARNING_CHANNEL_ID) if channel_id is None: cls.guild_trace(guild, 'No warning channel set! No warning issued.') return None channel: TextChannel = guild.get_channel(channel_id) if channel is None: cls.guild_trace(guild, 'Configured warning channel does not exist!') return None mention: str = Storage.get_config_value(guild, ConfigKey.WARNING_MENTION) text: str = message if mention is not None: text = f'{mention} {text}' msg: Message = await channel.send(text) return msg @classmethod async def update_warn(cls, warn_message: Message, new_text: str) -> None: """ Updates the text of a previously posted `warn`. Includes configured mentions if necessary. """ text: str = new_text mention: str = Storage.get_config_value( warn_message.guild, ConfigKey.WARNING_MENTION) if mention is not None: text = f'{mention} {text}' await warn_message.edit(content=text) @classmethod def guild_trace(cls, guild: Guild, message: str) -> None: print(f'[guild {guild.id}|{guild.name}] {message}')