Experimental Discord bot written in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

configcog.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. class ConfigCog(BaseCog, name='Configuration'):
  12. """
  13. Cog for handling general bot configuration.
  14. """
  15. def __init__(self, bot: Bot) -> None:
  16. super().__init__(
  17. bot,
  18. config_prefix='config',
  19. name='configuration',
  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=Permissions(Permissions.manage_messages.flag)
  27. )
  28. @config.command(
  29. description='Sets the mod warning channel',
  30. extras={
  31. 'long_description': 'Run this command in the channel where bot messages ' +
  32. 'intended for server moderators should be sent. Other bot ' +
  33. 'messages may still be posted in the channel a command was ' +
  34. 'invoked in. If no output channel is set, mod-related messages ' +
  35. 'will not be posted!',
  36. }
  37. )
  38. async def setwarningchannel(self, interaction: Interaction) -> None:
  39. """Command handler"""
  40. guild: Guild = interaction.guild
  41. channel: TextChannel = interaction.channel
  42. Storage.set_config_value(guild, ConfigKey.WARNING_CHANNEL_ID,
  43. interaction.channel.id)
  44. await interaction.response.send_message(
  45. f'{CONFIG["success_emoji"]} Warning channel updated to {channel.mention}.',
  46. ephemeral=True,
  47. )
  48. @config.command(
  49. description='Shows the mod warning channel',
  50. extras={
  51. 'long_description': 'Shows the configured channel (if any) where mod ' + \
  52. 'warnings will be posted.',
  53. },
  54. )
  55. async def getwarningchannel(self, interaction: Interaction) -> None:
  56. """Command handler"""
  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 a user/role to mention in warning messages',
  72. extras={
  73. 'usage': '<@user|@role>',
  74. 'long_description': 'Configures an role or other prefix to include at the ' +
  75. 'beginning of warning messages. If the intent is to get the ' +
  76. 'attention of certain users, be sure to specify a properly ' +
  77. 'formed @ tag, not just the name of the user/role.',
  78. },
  79. )
  80. async def setwarningmention(self,
  81. interaction: Interaction,
  82. mention: Optional[Union[User, Role]] = None) -> None:
  83. """Command handler"""
  84. guild: Guild = interaction.guild
  85. Storage.set_config_value(guild, ConfigKey.WARNING_MENTION, mention.mention if mention else None)
  86. if mention is None:
  87. await interaction.response.send_message(
  88. f'{CONFIG["success_emoji"]} Warning messages will not tag anyone.',
  89. ephemeral=True,
  90. )
  91. else:
  92. await interaction.response.send_message(
  93. f'{CONFIG["success_emoji"]} Warning messages will now tag {mention}.',
  94. ephemeral=True,
  95. )
  96. @config.command(
  97. description='Shows the user/role to mention in warning messages',
  98. extras={
  99. 'long_description': 'Shows the text, if any, that will be prefixed on any ' +
  100. 'warning messages.',
  101. },
  102. )
  103. async def getwarningmention(self, interaction: Interaction) -> None:
  104. """Command handler"""
  105. guild: Guild = interaction.guild
  106. mention: str = Storage.get_config_value(guild, ConfigKey.WARNING_MENTION)
  107. if mention is None:
  108. await interaction.response.send_message(
  109. f'{CONFIG["info_emoji"]} No warning mention configured.',
  110. ephemeral=True,
  111. )
  112. else:
  113. await interaction.response.send_message(
  114. f'{CONFIG["info_emoji"]} Warning messages will tag {mention}',
  115. ephemeral=True,
  116. )