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.

bangcommandcog.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import re
  2. from typing import Optional, TypedDict
  3. from discord import Guild, Interaction, Message, SelectOption, TextChannel, TextStyle
  4. from discord.app_commands import Choice, Group, autocomplete
  5. from discord.ext.commands import Cog
  6. from discord.ui import Label, Modal, Select, TextInput
  7. from config import CONFIG
  8. from rocketbot.bot import Rocketbot
  9. from rocketbot.cogs.basecog import BaseCog
  10. from rocketbot.cogsetting import CogSetting
  11. from rocketbot.ui.pagedcontent import PAGE_BREAK, paginate, update_paged_content
  12. from rocketbot.utils import (
  13. MOD_PERMISSIONS,
  14. blockquote_markdown,
  15. dump_stacktrace,
  16. indent_markdown,
  17. )
  18. _CURRENT_DATA_VERSION = 1
  19. _MAX_CONTENT_LENGTH = 2000
  20. class BangCommand(TypedDict):
  21. content: str
  22. mod_only: bool
  23. version: int
  24. async def command_autocomplete(interaction: Interaction, text: str) -> list[Choice[str]]:
  25. cmds = BangCommandCog.shared.get_saved_commands(interaction.guild)
  26. return [
  27. Choice(name=f'!{name}', value=name)
  28. for name, cmd in sorted(cmds.items())
  29. if len(text) == 0 or text.lower() in name
  30. ]
  31. class BangCommandCog(BaseCog, name='Bang Commands'):
  32. SETTING_COMMANDS = CogSetting(
  33. name='commands',
  34. datatype=dict[str, BangCommand],
  35. default_value={},
  36. )
  37. shared: Optional['BangCommandCog'] = None
  38. def __init__(self, bot: Rocketbot):
  39. super().__init__(
  40. bot,
  41. config_prefix='bangcommand',
  42. short_description='Provides custom informational chat !commands.',
  43. long_description='Bang commands are simple one-word chat messages starting with an exclamation '
  44. '(a "bang") that will make the bot respond with simple informational replies. '
  45. 'This functionality is similar to Twitch bots. Useful for posting answers to '
  46. 'frequently asked questions, reminding users of rules, and similar canned responses. '
  47. 'Commands can be individually made mod-only or usable by anyone.'
  48. )
  49. BangCommandCog.shared = self
  50. def get_saved_commands(self, guild: Guild) -> dict[str, BangCommand]:
  51. return self.get_guild_setting(guild, BangCommandCog.SETTING_COMMANDS)
  52. def get_saved_command(self, guild: Guild, name: str) -> Optional[BangCommand]:
  53. cmds = self.get_saved_commands(guild)
  54. name = BangCommandCog._normalize_name(name)
  55. return cmds.get(name, None)
  56. def set_saved_commands(self, guild: Guild, commands: dict[str, BangCommand]) -> None:
  57. self.set_guild_setting(guild, BangCommandCog.SETTING_COMMANDS, commands)
  58. bang = Group(
  59. name='command',
  60. description='Provides custom informational chat !commands.',
  61. guild_only=True,
  62. default_permissions=MOD_PERMISSIONS,
  63. )
  64. @bang.command(
  65. name='define',
  66. extras={
  67. 'long_description': 'Simple one-line content can be specified in the command. '
  68. 'For multi-line content, run the command without content '
  69. 'specified to use the editor popup.'
  70. }
  71. )
  72. @autocomplete(name=command_autocomplete)
  73. async def define_command(self, interaction: Interaction, name: str, definition: Optional[str] = None, mod_only: bool = False) -> None:
  74. """
  75. Defines or redefines a bang command.
  76. Parameters
  77. ----------
  78. interaction: Interaction
  79. name: string
  80. name of the command (lowercase a-z, underscores, and hyphens)
  81. definition: string
  82. content of the command
  83. mod_only: bool
  84. whether the command will only be recognized when a mod uses it
  85. """
  86. self.log(interaction.guild, f'{interaction.user.name} used command /bangcommand define {name} {definition} {mod_only}')
  87. name = BangCommandCog._normalize_name(name)
  88. if definition is None:
  89. cmd = self.get_saved_command(interaction.guild, name)
  90. await interaction.response.send_modal(
  91. _EditModal(
  92. name,
  93. content=cmd['content'] if cmd else None,
  94. mod_only=cmd['mod_only'] if cmd else None,
  95. exists=cmd is not None,
  96. )
  97. )
  98. return
  99. try:
  100. self.define(interaction.guild, name, definition, mod_only)
  101. await interaction.response.send_message(
  102. f'{CONFIG["success_emoji"]} Command `!{name}` has been defined.\n\n{blockquote_markdown(definition)}',
  103. ephemeral=True,
  104. )
  105. except ValueError as e:
  106. await interaction.response.send_message(
  107. f'{CONFIG["failure_emoji"]} {e}',
  108. ephemeral=True,
  109. )
  110. return
  111. @bang.command(
  112. name='undefine'
  113. )
  114. @autocomplete(name=command_autocomplete)
  115. async def undefine_command(self, interaction: Interaction, name: str) -> None:
  116. """
  117. Removes a bang command.
  118. Parameters
  119. ----------
  120. interaction: Interaction
  121. name: string
  122. name of the previously defined command
  123. """
  124. try:
  125. self.undefine(interaction.guild, name)
  126. await interaction.response.send_message(
  127. f'{CONFIG["success_emoji"]} Command `!{name}` removed.',
  128. ephemeral=True,
  129. )
  130. except ValueError as e:
  131. await interaction.response.send_message(
  132. f'{CONFIG["failure_emoji"]} {e}',
  133. ephemeral=True,
  134. )
  135. @bang.command(
  136. name='list'
  137. )
  138. async def list_command(self, interaction: Interaction) -> None:
  139. """
  140. Lists all defined bang commands.
  141. Parameters
  142. ----------
  143. interaction: Interaction
  144. """
  145. cmds = self.get_saved_commands(interaction.guild)
  146. if cmds is None or len(cmds) == 0:
  147. await interaction.response.send_message(
  148. f'{CONFIG["info_emoji"]} No commands defined.',
  149. ephemeral=True,
  150. delete_after=15,
  151. )
  152. return
  153. text = '## Commands'
  154. for name, cmd in sorted(cmds.items()):
  155. text += PAGE_BREAK + f'\n- `!{name}`'
  156. if cmd['mod_only']:
  157. text += ' - **mod only**'
  158. text += f'\n{indent_markdown(cmd["content"])}'
  159. pages = paginate(text)
  160. await update_paged_content(interaction, None, 0, pages)
  161. @bang.command(
  162. name='invoke',
  163. extras={
  164. 'long_description': 'Useful when you do not want the command to show up in chat.',
  165. }
  166. )
  167. @autocomplete(name=command_autocomplete)
  168. async def invoke_command(self, interaction: Interaction, name: str) -> None:
  169. """
  170. Invokes a bang command without typing it in chat.
  171. Parameters
  172. ----------
  173. interaction: Interaction
  174. name: string
  175. the bang command name
  176. """
  177. cmd = self.get_saved_command(interaction.guild, name)
  178. if cmd is None:
  179. await interaction.response.send_message(
  180. f'{CONFIG["failure_emoji"]} Command `!{name}` does not exist.',
  181. ephemeral=True,
  182. )
  183. return
  184. resp = await interaction.response.defer(ephemeral=True, thinking=False)
  185. await interaction.channel.send(
  186. cmd['content']
  187. )
  188. if resp.resource:
  189. await resp.resource.delete()
  190. def define(self, guild: Guild, name: str, content: str, mod_only: bool, check_exists: bool = False) -> None:
  191. if not BangCommandCog._is_valid_name(name):
  192. raise ValueError('Invalid command name. Must consist of lowercase letters, underscores, and hyphens (no spaces).')
  193. name = BangCommandCog._normalize_name(name)
  194. if len(content) < 1 or len(content) > 2000:
  195. raise ValueError(f'Content must be between 1 and {_MAX_CONTENT_LENGTH} characters.')
  196. cmds = self.get_saved_commands(guild)
  197. if check_exists:
  198. if cmds.get(name, None) is not None:
  199. raise ValueError(f'Command with name "{name}" already exists.')
  200. cmds[name] = {
  201. 'content': content,
  202. 'mod_only': mod_only,
  203. 'version': _CURRENT_DATA_VERSION,
  204. }
  205. self.set_saved_commands(guild, cmds)
  206. def undefine(self, guild: Guild, name: str) -> None:
  207. name = BangCommandCog._normalize_name(name)
  208. cmds = self.get_saved_commands(guild)
  209. if cmds.get(name, None) is None:
  210. raise ValueError(f'Command with name "{name}" does not exist.')
  211. del cmds[name]
  212. self.set_saved_commands(guild, cmds)
  213. @Cog.listener()
  214. async def on_message(self, message: Message) -> None:
  215. if message.guild is None or message.channel is None or not isinstance(message.channel, TextChannel):
  216. return
  217. content = message.content
  218. if content is None or not content.startswith('!') or not BangCommandCog._is_valid_name(content):
  219. return
  220. name = BangCommandCog._normalize_name(content)
  221. cmd = self.get_saved_command(message.guild, name)
  222. if cmd is None:
  223. return
  224. if cmd['mod_only'] and not message.author.guild_permissions.ban_members:
  225. return
  226. text = cmd["content"]
  227. # text = f'{text}\n\n-# {message.author.name} used `!{name}`'
  228. await message.channel.send(
  229. text,
  230. )
  231. @staticmethod
  232. def _normalize_name(name: str) -> str:
  233. name = name.lower().strip()
  234. if name.startswith('!'):
  235. name = name[1:]
  236. return name
  237. @staticmethod
  238. def _is_valid_name(name: Optional[str]) -> bool:
  239. if name is None:
  240. return False
  241. return re.match(r'^!?([a-z]+)([_-][a-z]+)*$', name) is not None
  242. class _EditModal(Modal, title='Edit Command'):
  243. name_label = Label(
  244. text='Command name',
  245. description='What gets typed in chat to trigger the command. Must be a-z, underscores, and hyphens (no spaces).',
  246. component=TextInput(
  247. style=TextStyle.short, # one line
  248. placeholder='!command_name',
  249. min_length=1,
  250. max_length=100,
  251. )
  252. )
  253. content_label = Label(
  254. text='Content',
  255. description='The text the bot will respond with when someone uses the command. Can contain markdown.',
  256. component=TextInput(
  257. style=TextStyle.paragraph,
  258. placeholder='Lorem ipsum dolor...',
  259. min_length=1,
  260. max_length=2000,
  261. )
  262. )
  263. mod_only_label = Label(
  264. text='Mod only?',
  265. description='Whether mods are the only users who can invoke this command.',
  266. component=Select(
  267. options=[
  268. SelectOption(label='No', value='False',
  269. description='Anyone can invoke this command.'),
  270. SelectOption(label='Yes', value='True',
  271. description='Only mods can invoke this command.'),
  272. ],
  273. )
  274. )
  275. def __init__(self, name: Optional[str] = None, content: Optional[str] = None, mod_only: Optional[bool] = None, exists: bool = False):
  276. super().__init__()
  277. self.exists = exists
  278. # noinspection PyTypeChecker
  279. name_input: TextInput = self.name_label.component
  280. # noinspection PyTypeChecker
  281. content_input: TextInput = self.content_label.component
  282. # noinspection PyTypeChecker
  283. mod_only_input: Select = self.mod_only_label.component
  284. name_input.default = name
  285. content_input.default = content
  286. resolved_mod_only = mod_only if mod_only is not None else False
  287. mod_only_input.options[0].default = not resolved_mod_only
  288. mod_only_input.options[1].default = resolved_mod_only
  289. async def on_submit(self, interaction: Interaction) -> None:
  290. # noinspection PyTypeChecker
  291. name_input: TextInput = self.name_label.component
  292. # noinspection PyTypeChecker
  293. content_input: TextInput = self.content_label.component
  294. # noinspection PyTypeChecker
  295. mod_only_input: Select = self.mod_only_label.component
  296. name = name_input.value
  297. content = content_input.value
  298. mod_only = mod_only_input.values[0] == 'True'
  299. try:
  300. BangCommandCog.shared.define(interaction.guild, name, content, mod_only, not self.exists)
  301. await interaction.response.send_message(
  302. f'{CONFIG["success_emoji"]} Command `!{name}` has been defined.\n\n{blockquote_markdown(content)}',
  303. ephemeral=True,
  304. )
  305. except ValueError as e:
  306. await interaction.response.send_message(
  307. f'{CONFIG["failure_emoji"]} {e}',
  308. ephemeral=True,
  309. )
  310. async def on_error(self, interaction: Interaction, error: Exception) -> None:
  311. dump_stacktrace(error)
  312. try:
  313. await interaction.response.send_message(
  314. f'{CONFIG["failure_emoji"]} Save failed',
  315. ephemeral=True,
  316. )
  317. except BaseException:
  318. pass