Experimental Discord bot written in Python
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. """
  2. A guild configuration setting available for editing via bot commands.
  3. """
  4. from datetime import timedelta
  5. from typing import TYPE_CHECKING, Any, Literal, Optional, Type, Union
  6. from discord import Interaction, Permissions
  7. from discord.app_commands import Range, Transform, describe
  8. from discord.app_commands.commands import Command, CommandCallback, Group, rename
  9. from discord.ext.commands import Bot
  10. from config import CONFIG
  11. from rocketbot.storage import Storage
  12. from rocketbot.utils import (
  13. MOD_PERMISSIONS,
  14. TimeDeltaTransformer,
  15. bot_log,
  16. dump_stacktrace,
  17. str_from_timedelta,
  18. )
  19. if TYPE_CHECKING:
  20. from rocketbot.cogs.basecog import BaseCog
  21. else:
  22. BaseCog = 'BaseCog'
  23. def describe_type(datatype: Type) -> str:
  24. if datatype is int:
  25. return 'integer'
  26. if datatype is float:
  27. return 'float'
  28. if datatype is str:
  29. return 'string'
  30. if datatype is bool:
  31. return 'boolean'
  32. if datatype is timedelta:
  33. return 'timespan'
  34. if getattr(datatype, '__origin__', None) is Union:
  35. return '|'.join([ describe_type(a) for a in datatype.__args__ ])
  36. if getattr(datatype, '__origin__', None) is Literal:
  37. return '"' + ('"|"'.join(datatype.__args__)) + '"'
  38. return datatype.__class__.__name__
  39. class CogSetting:
  40. """
  41. Describes a configuration setting for a guild that can be edited by the
  42. mods of those guilds. BaseCog can generate "/get" and "/set" commands (or
  43. "/enable" and "/disable" commands for boolean values) automatically, reducing
  44. the boilerplate of generating commands manually. Offers simple validation rules.
  45. """
  46. permissions: Permissions = Permissions(Permissions.manage_messages.flag)
  47. def __init__(self,
  48. name: str,
  49. datatype: Optional[Type],
  50. default_value: Any,
  51. brief: Optional[str] = None,
  52. description: Optional[str] = None,
  53. min_value: Optional[Any] = None,
  54. max_value: Optional[Any] = None,
  55. enum_values: Optional[set[Any]] = None):
  56. """
  57. Parameters
  58. ----------
  59. name: str
  60. Setting identifier. Must follow variable naming conventions.
  61. datatype: Optional[Type]
  62. Datatype of the setting. E.g. int, float, str
  63. default_value: Any
  64. Value to use if a guild has not yet configured one.
  65. brief: Optional[str]
  66. Description of the setting, starting with lower case.
  67. Will be inserted into phrases like "Sets <brief>" and
  68. "Gets <brief>".
  69. description: Optional[str]
  70. Long-form description. Min, max, and enum values will be
  71. appended to the end, so does not need to include these.
  72. min_value: Optional[Any]
  73. Smallest allowable value. Must be of the same datatype as
  74. the value. None for no minimum.
  75. max_value: Optional[Any]
  76. Largest allowable value. None for no maximum.
  77. enum_values: Optional[set[Any]]
  78. Set of allowed values. None if unconstrained.
  79. """
  80. self.name: str = name
  81. self.datatype: Type = datatype
  82. self.default_value = default_value
  83. self.brief: Optional[str] = brief
  84. self.description: str = description or '' # Can't be None
  85. self.min_value: Optional[Any] = min_value
  86. self.max_value: Optional[Any] = max_value
  87. self.enum_values: Optional[set[Any]] = enum_values
  88. if self.enum_values:
  89. value_list = '`' + ('`, `'.join(self.enum_values)) + '`'
  90. self.description += f' (Permitted values: {value_list})'
  91. elif self.min_value is not None and self.max_value is not None:
  92. self.description += f' (Value must be between `{self.min_value}` and `{self.max_value}`)'
  93. elif self.min_value is not None:
  94. self.description += f' (Minimum value: {self.min_value})'
  95. elif self.max_value is not None:
  96. self.description += f' (Maximum value: {self.max_value})'
  97. def validate_value(self, new_value: Any) -> None:
  98. """
  99. Checks if a value is legal for this setting. Raises a ValueError if not.
  100. """
  101. if self.min_value is not None and new_value < self.min_value:
  102. raise ValueError(f'`{self.name}` must be >= {self.min_value}')
  103. if self.max_value is not None and new_value > self.max_value:
  104. raise ValueError(f'`{self.name}` must be <= {self.max_value}')
  105. if self.enum_values is not None and new_value not in self.enum_values:
  106. allowed_values = '`' + ('`, `'.join(self.enum_values)) + '`'
  107. raise ValueError(f'`{self.name}` must be one of {allowed_values}')
  108. def set_up(self, cog: BaseCog) -> None:
  109. """
  110. Sets up getter and setter commands for this setting. This should
  111. usually only be called by BaseCog.
  112. """
  113. if self.name in ('enabled', 'is_enabled'):
  114. self.__enable_group.add_command(self.__make_enable_command(cog))
  115. self.__disable_group.add_command(self.__make_disable_command(cog))
  116. else:
  117. self.__get_group.add_command(self.__make_getter_command(cog))
  118. self.__set_group.add_command(self.__make_setter_command(cog))
  119. def to_stored_value(self, native_value: Any) -> Any:
  120. """Converts a configuration value to a JSON-compatible datatype."""
  121. if self.datatype is timedelta:
  122. return native_value.total_seconds()
  123. return native_value
  124. def to_native_value(self, stored_value: Any) -> Any:
  125. """Converts the stored JSON-compatible datatype to its actual value."""
  126. if self.datatype is timedelta and isinstance(stored_value, (int, float)):
  127. return timedelta(seconds=stored_value)
  128. return stored_value
  129. @staticmethod
  130. def native_value_to_str(native_value: Any) -> str:
  131. """Formats a native configuration value to a user-presentable string."""
  132. if native_value is None:
  133. return '<no value>'
  134. if isinstance(native_value, timedelta):
  135. return str_from_timedelta(native_value)
  136. if isinstance(native_value, bool):
  137. return 'true' if native_value else 'false'
  138. return f'{native_value}'
  139. def __make_getter_command(self, cog: BaseCog) -> Command:
  140. setting: CogSetting = self
  141. setting_name = setting.name
  142. if cog.config_prefix is not None:
  143. setting_name = f'{cog.config_prefix}_{setting_name}'
  144. async def getter(interaction: Interaction) -> None:
  145. key = f'{cog.__class__.__name__}.{setting.name}'
  146. value = setting.to_native_value(Storage.get_config_value(interaction.guild, key))
  147. cog.log(interaction.guild, f'{interaction.user.name} used /get setting_name')
  148. if value is None:
  149. value = setting.default_value
  150. await interaction.response.send_message(
  151. f'{CONFIG["info_emoji"]} `{setting_name}` is using default of `{CogSetting.native_value_to_str(value)}`',
  152. ephemeral=True
  153. )
  154. else:
  155. await interaction.response.send_message(
  156. f'{CONFIG["info_emoji"]} `{setting_name}` is set to `{CogSetting.native_value_to_str(value)}`',
  157. ephemeral=True
  158. )
  159. bot_log(None, cog.__class__, f"Creating command: /get {setting_name}")
  160. command = Command(
  161. name=setting_name,
  162. description=f'Shows {self.brief}.',
  163. callback=getter,
  164. parent=CogSetting.__get_group,
  165. extras={
  166. 'cog': cog,
  167. 'setting': setting,
  168. 'long_description': setting.description,
  169. },
  170. )
  171. return command
  172. def __make_setter_command(self, cog: BaseCog) -> Command:
  173. setting: CogSetting = self
  174. setting_name = setting.name
  175. if cog.config_prefix is not None:
  176. setting_name = f'{cog.config_prefix}_{setting_name}'
  177. async def setter_general(interaction: Interaction, new_value) -> None:
  178. cog.log(interaction.guild, f'{interaction.user.name} used /set setting_name {new_value}')
  179. try:
  180. setting.validate_value(new_value)
  181. except ValueError as ve:
  182. await interaction.response.send_message(
  183. f'{CONFIG["failure_emoji"]} {ve}',
  184. ephemeral=True
  185. )
  186. return
  187. key = f'{cog.__class__.__name__}.{setting.name}'
  188. Storage.set_config_value(interaction.guild, key, setting.to_stored_value(new_value))
  189. await interaction.response.send_message(
  190. f'{CONFIG["success_emoji"]} `{setting_name}` is now set to `{setting.to_native_value(new_value)}`',
  191. ephemeral=True
  192. )
  193. await cog.on_setting_updated(interaction.guild, setting)
  194. setter: CommandCallback = setter_general
  195. field_description: str = self.brief
  196. if self.datatype is int:
  197. if self.min_value is not None or self.max_value is not None:
  198. r_min = self.min_value
  199. r_max = self.max_value
  200. async def setter_range(interaction: Interaction, new_value: Range[int, r_min, r_max]) -> None:
  201. await setter_general(interaction, new_value)
  202. setter = setter_range
  203. else:
  204. async def setter_int(interaction: Interaction, new_value: int) -> None:
  205. await setter_general(interaction, new_value)
  206. setter = setter_int
  207. elif self.datatype is float:
  208. async def setter_float(interaction: Interaction, new_value: float) -> None:
  209. await setter_general(interaction, new_value)
  210. setter = setter_float
  211. elif self.datatype is timedelta:
  212. field_description += ' (e.g. 30s, 5m, 1h30s, 7d)'
  213. async def setter_timedelta(interaction: Interaction, new_value: Transform[timedelta, TimeDeltaTransformer]) -> None:
  214. await setter_general(interaction, new_value)
  215. setter = setter_timedelta
  216. elif getattr(self.datatype, '__origin__', None) == Literal:
  217. dt = self.datatype
  218. async def setter_enum(interaction: Interaction, new_value: dt) -> None:
  219. await setter_general(interaction, new_value)
  220. setter = setter_enum
  221. elif self.datatype is str:
  222. if self.enum_values is not None:
  223. raise ValueError('Type for a setting with enum values should be typing.Literal')
  224. else:
  225. async def setter_str(interaction: Interaction, new_value: str) -> None:
  226. await setter_general(interaction, new_value)
  227. setter = setter_str
  228. elif self.datatype is bool:
  229. async def setter_bool(interaction: Interaction, new_value: bool) -> None:
  230. await setter_general(interaction, new_value)
  231. setter = setter_bool
  232. elif self.datatype is not None:
  233. raise ValueError(f'Invalid type {self.datatype}')
  234. setter = describe(new_value=field_description)(rename(new_value=self.name)(setter))
  235. bot_log(None, cog.__class__, f"Creating command: /set {setting_name} <{describe_type(self.datatype)}>")
  236. command = Command(
  237. name=setting_name,
  238. description=f'Sets {self.brief}.',
  239. callback=setter,
  240. parent=CogSetting.__set_group,
  241. extras={
  242. 'cog': cog,
  243. 'setting': setting,
  244. 'long_description': setting.description,
  245. },
  246. )
  247. return command
  248. def __make_enable_command(self, cog: BaseCog) -> Command:
  249. setting: CogSetting = self
  250. async def enabler(interaction: Interaction) -> None:
  251. key = f'{cog.__class__.__name__}.{setting.name}'
  252. Storage.set_config_value(interaction.guild, key, True)
  253. await interaction.response.send_message(
  254. f'{CONFIG["success_emoji"]} {setting.brief.capitalize()} enabled.',
  255. ephemeral=True
  256. )
  257. await cog.on_setting_updated(interaction.guild, setting)
  258. cog.log(interaction.guild, f'{interaction.user.name} used /enable {cog.__class__.__name__}')
  259. bot_log(None, cog.__class__, f"Creating command: /enable {cog.config_prefix}")
  260. command = Command(
  261. name=cog.config_prefix,
  262. description=f'Enables {cog.qualified_name} functionality.',
  263. callback=enabler,
  264. parent=CogSetting.__enable_group,
  265. extras={
  266. 'cog': cog,
  267. 'setting': setting,
  268. 'long_description': setting.description,
  269. },
  270. )
  271. return command
  272. def __make_disable_command(self, cog: BaseCog) -> Command:
  273. setting: CogSetting = self
  274. async def disabler(interaction: Interaction) -> None:
  275. key = f'{cog.__class__.__name__}.{setting.name}'
  276. Storage.set_config_value(interaction.guild, key, False)
  277. await interaction.response.send_message(
  278. f'{CONFIG["success_emoji"]} {setting.brief.capitalize()} disabled.',
  279. ephemeral=True
  280. )
  281. await cog.on_setting_updated(interaction.guild, setting)
  282. cog.log(interaction.guild, f'{interaction.user.name} used /disable {cog.__class__.__name__}')
  283. bot_log(None, cog.__class__, f"Creating command: /disable {cog.config_prefix}")
  284. command = Command(
  285. name=cog.config_prefix,
  286. description=f'Disables {cog.config_prefix} functionality',
  287. callback=disabler,
  288. parent=CogSetting.__disable_group,
  289. extras={
  290. 'cog': cog,
  291. 'setting': setting,
  292. 'long_description': setting.description,
  293. },
  294. )
  295. return command
  296. __set_group: Group
  297. __get_group: Group
  298. __enable_group: Group
  299. __disable_group: Group
  300. @classmethod
  301. def set_up_all(cls, cog: BaseCog, bot: Bot, settings: list['CogSetting']) -> None:
  302. """
  303. Sets up editing commands for a list of CogSettings and adds them to a
  304. cog. If the cog has a command Group, commands will be added to it.
  305. Otherwise, they will be added at the top level.
  306. """
  307. cls.__set_up_base_commands(bot)
  308. if len(settings) == 0:
  309. return
  310. for setting in settings:
  311. setting.set_up(cog)
  312. @classmethod
  313. def __set_up_base_commands(cls, bot: Bot) -> None:
  314. if getattr(cls, '_CogSetting__set_group', None) is not None:
  315. return
  316. cls.__set_group = Group(
  317. name='set',
  318. description='Sets a configuration value for this guild.',
  319. guild_only=True,
  320. default_permissions=MOD_PERMISSIONS,
  321. extras={
  322. 'long_description': 'Settings are guild-specific. If no value is set, a default is used. Use `/get` to '
  323. 'see the current value for this guild.',
  324. },
  325. )
  326. cls.__get_group = Group(
  327. name='get',
  328. description='Shows a configuration value for this guild.',
  329. guild_only=True,
  330. default_permissions=MOD_PERMISSIONS,
  331. extras={
  332. 'long_description': 'Settings are guild-specific. If no value is set, a default is used. Use `/set` to '
  333. 'change the value.',
  334. },
  335. )
  336. cls.__enable_group = Group(
  337. name='enable',
  338. description='Enables a module for this guild.',
  339. guild_only=True,
  340. default_permissions=MOD_PERMISSIONS,
  341. extras={
  342. 'long_description': 'Modules are enabled on a per-guild basis and are off by default. Use `/disable` '
  343. 'to disable an enabled module.',
  344. },
  345. )
  346. cls.__disable_group = Group(
  347. name='disable',
  348. description='Disables a module for this guild.',
  349. guild_only=True,
  350. default_permissions=MOD_PERMISSIONS,
  351. extras={
  352. 'long_description': 'Modules are enabled on a per-guild basis and are off by default. Use `/enable` '
  353. 're-enable a disabled module.',
  354. },
  355. )
  356. bot.tree.add_command(cls.__set_group)
  357. bot.tree.add_command(cls.__get_group)
  358. bot.tree.add_command(cls.__enable_group)
  359. bot.tree.add_command(cls.__disable_group)
  360. async def show_all(interaction: Interaction) -> None:
  361. from rocketbot.cogs.basecog import BaseCog
  362. try:
  363. bot_log(interaction.guild, None, f'{interaction.user.name} used /get all')
  364. guild = interaction.guild
  365. if guild is None:
  366. await interaction.response.send_message(
  367. f'{CONFIG["failure_emoji"]} No guild.',
  368. ephemeral=True,
  369. delete_after=10,
  370. )
  371. return
  372. text = '## :information_source: Configuration'
  373. for cog_name, cog in sorted(bot.cogs.items()):
  374. if not isinstance(cog, BaseCog):
  375. continue
  376. bcog: BaseCog = cog
  377. if len(bcog.settings) == 0:
  378. continue
  379. text += f'\n### {bcog.qualified_name} Module'
  380. for setting in sorted(bcog.settings, key=lambda s: (s.name != 'enabled', s.name)):
  381. key = f'{bcog.__class__.__name__}.{setting.name}'
  382. value = setting.to_native_value(Storage.get_config_value(guild, key))
  383. deflt = setting.default_value
  384. if setting.name == 'enabled':
  385. text += '\n- Module is '
  386. if value is not None:
  387. text += '**' + ('enabled' if value else 'disabled') + '**'
  388. else:
  389. text += ('enabled' if deflt else 'disabled') + ' _(default)_'
  390. else:
  391. if value is not None:
  392. text += f'\n- `{bcog.config_prefix}_{setting.name}` = **{CogSetting.native_value_to_str(value)}**'
  393. else:
  394. text += f'\n- `{bcog.config_prefix}_{setting.name}` = {CogSetting.native_value_to_str(deflt)} _(using default)_'
  395. await interaction.response.send_message(
  396. text,
  397. ephemeral=True,
  398. )
  399. except BaseException as e:
  400. dump_stacktrace(e)
  401. show_all_command = Command(
  402. name='all',
  403. description='Shows all configuration for this guild.',
  404. callback=show_all,
  405. )
  406. cls.__get_group.add_command(show_all_command)