Experimental Discord bot written in Python
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

bangcommandcog.py 11KB

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