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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """
  2. Cog handling general configuration for a guild.
  3. """
  4. from typing import Union, Optional
  5. from discord import Guild, Permissions, TextChannel, Interaction, Role, User
  6. from discord.app_commands import Group
  7. from discord.ext.commands import Bot
  8. from config import CONFIG
  9. from rocketbot.storage import ConfigKey, Storage
  10. from rocketbot.cogs.basecog import BaseCog
  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. name='configuration',
  21. short_description='Manages general bot configuration.',
  22. )
  23. config = Group(
  24. name='config',
  25. description='Manages general bot configuration',
  26. guild_only=True,
  27. default_permissions=MOD_PERMISSIONS,
  28. )
  29. @config.command()
  30. async def set_warning_channel(self, interaction: Interaction) -> None:
  31. """
  32. Sets mod warnings to post in the current channel.
  33. Run this command in the channel where bot messages intended for server
  34. moderators should be sent. Other bot messages may still be posted in
  35. the channel a command was invoked in. If no output channel is set,
  36. mod-related messages will not be posted!
  37. """
  38. guild: Guild = interaction.guild
  39. channel: TextChannel = interaction.channel
  40. Storage.set_config_value(guild, ConfigKey.WARNING_CHANNEL_ID,
  41. interaction.channel.id)
  42. await interaction.response.send_message(
  43. f'{CONFIG["success_emoji"]} Warning channel updated to {channel.mention}.',
  44. ephemeral=True,
  45. )
  46. @config.command()
  47. async def get_warning_channel(self, interaction: Interaction) -> None:
  48. """
  49. Shows the mod warning channel, if any.
  50. Shows the configured channel (if any) where mod warnings and other bot
  51. output will be posted.
  52. """
  53. guild: Guild = interaction.guild
  54. channel_id = Storage.get_config_value(guild, ConfigKey.WARNING_CHANNEL_ID)
  55. if channel_id is None:
  56. await interaction.response.send_message(
  57. f'{CONFIG["info_emoji"]} No warning channel is configured.',
  58. ephemeral=True,
  59. )
  60. else:
  61. channel = guild.get_channel(channel_id)
  62. await interaction.response.send_message(
  63. f'{CONFIG["info_emoji"]} Warning channel is configured as {channel.mention}.',
  64. ephemeral=True,
  65. )
  66. @config.command()
  67. async def set_warning_mention(self,
  68. interaction: Interaction,
  69. mention: Optional[Union[User, Role]] = None) -> None:
  70. """
  71. Sets a user/role to mention in warning messages.
  72. Configures an role or other prefix to include at the beginning of
  73. warning messages. The intent is to get the attention of certain users
  74. in case action is needed. Leave blank to tag no one.
  75. Parameters
  76. ----------
  77. interaction: Interaction
  78. mention: User or Role
  79. The user or role to mention in warning messages
  80. """
  81. guild: Guild = interaction.guild
  82. Storage.set_config_value(guild, ConfigKey.WARNING_MENTION, mention.mention if mention else None)
  83. if mention is None:
  84. await interaction.response.send_message(
  85. f'{CONFIG["success_emoji"]} Warning messages will not tag anyone.',
  86. ephemeral=True,
  87. )
  88. else:
  89. await interaction.response.send_message(
  90. f'{CONFIG["success_emoji"]} Warning messages will now tag {mention.mention}.',
  91. ephemeral=True,
  92. )
  93. @config.command()
  94. async def get_warning_mention(self, interaction: Interaction) -> None:
  95. """
  96. Shows the configured user/role to mention in warning messages.
  97. """
  98. guild: Guild = interaction.guild
  99. mention: str = Storage.get_config_value(guild, ConfigKey.WARNING_MENTION)
  100. if mention is None:
  101. await interaction.response.send_message(
  102. f'{CONFIG["info_emoji"]} No warning mention configured.',
  103. ephemeral=True,
  104. )
  105. else:
  106. await interaction.response.send_message(
  107. f'{CONFIG["info_emoji"]} Warning messages will tag {mention}',
  108. ephemeral=True,
  109. )