Experimental Discord bot written in Python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

configcog.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from discord import Guild, TextChannel
  2. from discord.ext import commands
  3. from storage import ConfigKey, Storage
  4. from cogs.basecog import BaseCog
  5. class ConfigCog(BaseCog):
  6. """
  7. Cog for handling general bot configuration.
  8. """
  9. def __init__(self, bot):
  10. super().__init__(bot)
  11. @commands.group(
  12. brief='Manages general bot configuration'
  13. )
  14. @commands.has_permissions(ban_members=True)
  15. @commands.guild_only()
  16. async def config(self, context: commands.Context):
  17. 'Command group'
  18. if context.invoked_subcommand is None:
  19. await context.send_help()
  20. @config.command(
  21. name='setwarningchannel',
  22. brief='Sets the channel where mod warnings are posted',
  23. description='Run this command in the channel where bot messages ' +
  24. 'intended for server moderators should be sent. Other bot messages ' +
  25. 'may still be posted in the channel a command was invoked in. If ' +
  26. 'no output channel is set, mod-related messages will not be posted!',
  27. )
  28. async def config_setwarningchannel(self, context: commands.Context) -> None:
  29. 'Command handler'
  30. guild: Guild = context.guild
  31. channel: TextChannel = context.channel
  32. Storage.set_config_value(guild, ConfigKey.WARNING_CHANNEL_ID,
  33. context.channel.id)
  34. await context.message.reply(
  35. f'Warning channel updated to {channel.mention}.',
  36. mention_author=False)
  37. @config.command(
  38. name='getwarningchannel',
  39. brief='Shows the channel where mod warnings are posted',
  40. )
  41. async def config_getwarningchannel(self, context: commands.Context) -> None:
  42. 'Command handler'
  43. guild: Guild = context.guild
  44. channel_id = Storage.get_config_value(guild, ConfigKey.WARNING_CHANNEL_ID)
  45. if channel_id is None:
  46. await context.message.reply(
  47. 'No warning channel is configured.',
  48. mention_author=False)
  49. else:
  50. channel = guild.get_channel(channel_id)
  51. await context.message.reply(
  52. f'Warning channel is configured as {channel.mention}.',
  53. mention_author=False)
  54. @config.command(
  55. name='setwarningmention',
  56. brief='Sets a user/role to mention in warning messages',
  57. usage='<@user|@role>',
  58. description='Configures an role or other prefix to include at the ' +
  59. 'beginning of warning messages. If the intent is to get the ' +
  60. 'attention of certain users, be sure to specify a properly ' +
  61. 'formed @ tag, not just the name of the user/role.'
  62. )
  63. async def config_setwarningmention(self, context: commands.Context, mention: str = None) -> None:
  64. guild: Guild = context.guild
  65. Storage.set_config_value(guild, ConfigKey.WARNING_MENTION, mention)
  66. if mention is None:
  67. await context.message.reply(
  68. 'Warning messages will not tag anyone.',
  69. mention_author=False)
  70. else:
  71. await context.message.reply(
  72. f'Warning messages will now tag {mention}.',
  73. mention_author=False)
  74. @config.command(
  75. name='getwarningmention',
  76. brief='Shows the user/role to mention in warning messages',
  77. description='Shows the text, if any, that will be prefixed on any ' +
  78. 'warning messages.'
  79. )
  80. async def config_getwarningmention(self, context: commands.Context) -> None:
  81. guild: Guild = context.guild
  82. mention: str = Storage.get_config_value(guild, ConfigKey.WARNING_MENTION)
  83. if mention is None:
  84. await context.message.reply(
  85. 'No warning mention configured.',
  86. mention_author=False)
  87. else:
  88. await context.message.reply(
  89. f'Warning messages will tag {mention}',
  90. mention_author=False)