Experimental Discord bot written in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from discord import Guild, Message, TextChannel
  2. from discord.ext import commands
  3. from storage import StateKey, Storage
  4. class BaseCog(commands.Cog):
  5. def __init__(self, bot):
  6. self.bot = bot
  7. @staticmethod
  8. def save_setting(guild: Guild, name: str, value):
  9. """
  10. Saves one value to a guild's persisted state. The given value must
  11. be a JSON-encodable type.
  12. """
  13. if value is not None and not isinstance(value, (bool, int, float, str, list, dict)):
  14. raise Exception(f'value for key {name} is not supported JSON type! {type(value)}')
  15. state: dict = Storage.state_for_guild(guild)
  16. if value is None:
  17. del state[name]
  18. else:
  19. state[name] = value
  20. Storage.save_guild_state(guild)
  21. @staticmethod
  22. async def warn(guild: Guild, message: str) -> bool:
  23. """
  24. Sends a warning message to the configured warning channel for the
  25. given guild. If no warning channel is configured no action is taken.
  26. Returns True if the message was sent successfully or False if the
  27. warning channel could not be found.
  28. """
  29. state: dict = Storage.state_for_guild(guild)
  30. channel_id = state.get(StateKey.WARNING_CHANNEL_ID)
  31. if channel_id is None:
  32. guild_trace(guild, 'No warning channel set! No warning issued.')
  33. return False
  34. channel: TextChannel = guild.get_channel(channel_id)
  35. if channel is None:
  36. guild_trace(guild, 'Configured warning channel no longer exists!')
  37. return False
  38. message: Message = await channel.send(message)
  39. return message is not None
  40. @staticmethod
  41. def guild_trace(guild: Guild, message: str) -> None:
  42. print(f'[guild {guild.id}|{guild.name}] {message}')