| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- from discord import Guild, Message, TextChannel
- from discord.ext import commands
-
- from storage import StateKey, Storage
-
- class BaseCog(commands.Cog):
- def __init__(self, bot):
- self.bot = bot
-
- @classmethod
- def save_setting(cls, guild: Guild, name: str, value):
- """
- Saves one value to a guild's persisted state. The given value must
- be a JSON-encodable type.
- """
- if value is not None and not isinstance(value, (bool, int, float, str, list, dict)):
- raise Exception(f'value for key {name} is not supported JSON type! {type(value)}')
- state: dict = Storage.state_for_guild(guild)
- if value is None:
- del state[name]
- else:
- state[name] = value
- Storage.save_guild_state(guild)
-
- @classmethod
- async def warn(cls, guild: Guild, message: str) -> bool:
- """
- Sends a warning message to the configured warning channel for the
- given guild. If no warning channel is configured no action is taken.
- Returns True if the message was sent successfully or False if the
- warning channel could not be found.
- """
- state: dict = Storage.state_for_guild(guild)
- channel_id = state.get(StateKey.WARNING_CHANNEL_ID)
- if channel_id is None:
- cls.guild_trace(guild, 'No warning channel set! No warning issued.')
- return False
- channel: TextChannel = guild.get_channel(channel_id)
- if channel is None:
- cls.guild_trace(guild, 'Configured warning channel no longer exists!')
- return False
- message: Message = await channel.send(message)
- return message is not None
-
- @classmethod
- def guild_trace(cls, guild: Guild, message: str) -> None:
- print(f'[guild {guild.id}|{guild.name}] {message}')
|