Experimental Discord bot written in Python
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

configcog.py 3.7KB

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