Experimental Discord bot written in Python
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from discord import Guild, Message, TextChannel
  2. from discord.ext import commands
  3. from storage import ConfigKey, Storage
  4. import json
  5. class BaseCog(commands.Cog):
  6. def __init__(self, bot):
  7. self.bot = bot
  8. @classmethod
  9. async def warn(cls, guild: Guild, message: str) -> Message:
  10. """
  11. Sends a warning message to the configured warning channel for the
  12. given guild. If no warning channel is configured no action is taken.
  13. Returns the Message if successful or None if not.
  14. """
  15. channel_id = Storage.get_config_value(guild, ConfigKey.WARNING_CHANNEL_ID)
  16. if channel_id is None:
  17. cls.guild_trace(guild, 'No warning channel set! No warning issued.')
  18. return None
  19. channel: TextChannel = guild.get_channel(channel_id)
  20. if channel is None:
  21. cls.guild_trace(guild, 'Configured warning channel does not exist!')
  22. return None
  23. mention: str = Storage.get_config_value(guild, ConfigKey.WARNING_MENTION)
  24. text: str = message
  25. if mention is not None:
  26. text = f'{mention} {text}'
  27. msg: Message = await channel.send(text)
  28. return msg
  29. @classmethod
  30. async def update_warn(cls, warn_message: Message, new_text: str) -> None:
  31. """
  32. Updates the text of a previously posted `warn`. Includes configured
  33. mentions if necessary.
  34. """
  35. text: str = new_text
  36. mention: str = Storage.get_config_value(
  37. warn_message.guild,
  38. ConfigKey.WARNING_MENTION)
  39. if mention is not None:
  40. text = f'{mention} {text}'
  41. await warn_message.edit(content=text)
  42. @classmethod
  43. def guild_trace(cls, guild: Guild, message: str) -> None:
  44. print(f'[guild {guild.id}|{guild.name}] {message}')