Experimental Discord bot written in Python
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """
  2. Cog handling general configuration for a guild.
  3. """
  4. from typing import Optional, Union
  5. from discord import Guild, Interaction, Role, TextChannel, User
  6. from discord.app_commands import Group
  7. from discord.ext.commands import Bot
  8. from config import CONFIG
  9. from rocketbot.cogs.basecog import BaseCog
  10. from rocketbot.storage import ConfigKey, Storage
  11. from rocketbot.utils import MOD_PERMISSIONS
  12. class ConfigCog(BaseCog, name='Configuration'):
  13. """
  14. Cog for handling general bot configuration.
  15. """
  16. def __init__(self, bot: Bot) -> None:
  17. super().__init__(
  18. bot,
  19. config_prefix='config',
  20. short_description='Manages general bot configuration.',
  21. )
  22. config = Group(
  23. name='config',
  24. description='Manages general bot configuration.',
  25. guild_only=True,
  26. default_permissions=MOD_PERMISSIONS,
  27. )
  28. @config.command(
  29. description='Sets mod warnings to post in the current channel.',
  30. extras={
  31. 'long_description': 'Run this command in the channel where bot messages intended '
  32. 'for server moderators should be sent. Other bot messages may '
  33. 'still be posted in the channel a command was invoked in. **If '
  34. 'no output channel is set, mod-related messages will not be posted!**',
  35. },
  36. )
  37. async def set_warning_channel(self, interaction: Interaction) -> None:
  38. """
  39. Sets mod warnings to post in the current channel.
  40. Run this command in the channel where bot messages intended for server
  41. moderators should be sent. Other bot messages may still be posted in
  42. the channel a command was invoked in. If no output channel is set,
  43. mod-related messages will not be posted!
  44. """
  45. guild: Guild = interaction.guild
  46. channel: TextChannel = interaction.channel
  47. Storage.set_config_value(guild, ConfigKey.WARNING_CHANNEL_ID,
  48. interaction.channel.id)
  49. await interaction.response.send_message(
  50. f'{CONFIG["success_emoji"]} Warning channel updated to {channel.mention}.',
  51. ephemeral=True,
  52. )
  53. @config.command(
  54. description='Shows the configured mod warning channel, if any.'
  55. )
  56. async def get_warning_channel(self, interaction: Interaction) -> None:
  57. guild: Guild = interaction.guild
  58. channel_id = Storage.get_config_value(guild, ConfigKey.WARNING_CHANNEL_ID)
  59. if channel_id is None:
  60. await interaction.response.send_message(
  61. f'{CONFIG["info_emoji"]} No warning channel is configured.',
  62. ephemeral=True,
  63. )
  64. else:
  65. channel = guild.get_channel(channel_id)
  66. await interaction.response.send_message(
  67. f'{CONFIG["info_emoji"]} Warning channel is configured as {channel.mention}.',
  68. ephemeral=True,
  69. )
  70. @config.command(
  71. description='Sets the user or role to tag in warning messages.',
  72. extras={
  73. 'long_description': 'Calling this without a value disables tagging in warnings.',
  74. }
  75. )
  76. async def set_warning_mention(self,
  77. interaction: Interaction,
  78. mention: Optional[Union[User, Role]] = None) -> None:
  79. """
  80. Sets a user/role to mention in warning messages.
  81. Parameters
  82. ----------
  83. interaction: Interaction
  84. mention: User or Role
  85. the user or role to mention in warning messages
  86. """
  87. guild: Guild = interaction.guild
  88. Storage.set_config_value(guild, ConfigKey.WARNING_MENTION, mention.mention if mention else None)
  89. if mention is None:
  90. await interaction.response.send_message(
  91. f'{CONFIG["success_emoji"]} Warning messages will not tag anyone.',
  92. ephemeral=True,
  93. )
  94. else:
  95. await interaction.response.send_message(
  96. f'{CONFIG["success_emoji"]} Warning messages will now tag {mention.mention}.',
  97. ephemeral=True,
  98. )
  99. @config.command(
  100. description='Shows the configured user or role tagged in warning messages.',
  101. )
  102. async def get_warning_mention(self, interaction: Interaction) -> None:
  103. guild: Guild = interaction.guild
  104. mention: str = Storage.get_config_value(guild, ConfigKey.WARNING_MENTION)
  105. if mention is None:
  106. await interaction.response.send_message(
  107. f'{CONFIG["info_emoji"]} No warning mention configured.',
  108. ephemeral=True,
  109. )
  110. else:
  111. await interaction.response.send_message(
  112. f'{CONFIG["info_emoji"]} Warning messages will tag {mention}',
  113. ephemeral=True,
  114. )