Experimental Discord bot written in Python
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. """
  2. General utility functions.
  3. """
  4. import re
  5. import sys
  6. import traceback
  7. from datetime import datetime, timedelta
  8. from typing import Any, Optional, Union
  9. import discord
  10. from discord import Guild, Interaction, Permissions
  11. from discord.app_commands import Transformer
  12. from discord.ext.commands import BadArgument, Cog
  13. def dump_stacktrace(e: BaseException) -> None:
  14. print(e, file=sys.stderr)
  15. traceback.print_exception(type(e), e, e.__traceback__)
  16. def timedelta_from_str(s: str) -> timedelta:
  17. """
  18. Parses a timespan.
  19. Format examples:
  20. "30m"
  21. "10s"
  22. "90d"
  23. "1h30m"
  24. "73d18h22m52s"
  25. Parameters
  26. ----------
  27. s : str
  28. string to parse
  29. Returns
  30. -------
  31. timedelta
  32. Raises
  33. ------
  34. ValueError
  35. if parsing fails
  36. """
  37. p: re.Pattern = re.compile('^(?:[0-9]+[a-zA-Z])+$')
  38. if p.match(s) is None:
  39. raise ValueError(f'Illegal timespan value "{s}". Examples: 30s, 5m, 1h30m, 30d')
  40. p = re.compile('([0-9]+)([dhms])')
  41. days: int = 0
  42. hours: int = 0
  43. minutes: int = 0
  44. seconds: int = 0
  45. for m in p.finditer(s):
  46. scalar = int(m.group(1))
  47. unit = m.group(2).lower()
  48. if unit == 'd':
  49. days = scalar
  50. elif unit == 'h':
  51. hours = scalar
  52. elif unit == 'm':
  53. minutes = scalar
  54. elif unit == 's':
  55. seconds = scalar
  56. else:
  57. raise ValueError(f'Invalid unit "{unit}". Valid units: "s"=seconds, "m"=minutes, "h"=hours, "d"=days')
  58. return timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
  59. def str_from_timedelta(td: timedelta) -> str:
  60. """
  61. Encodes a timedelta as a str. E.g. "3d2h"
  62. """
  63. d: int = td.days
  64. h: int = td.seconds // 3600
  65. m: int = (td.seconds // 60) % 60
  66. s: int = td.seconds % 60
  67. components: list[str] = []
  68. if d != 0:
  69. components.append(f'{d}d')
  70. if h != 0:
  71. components.append(f'{h}h')
  72. if m != 0:
  73. components.append(f'{m}m')
  74. if s != 0 or len(components) == 0:
  75. components.append(f'{s}s')
  76. return ''.join(components)
  77. def describe_timedelta(td: timedelta, max_components: int = 2) -> str:
  78. """
  79. Formats a human-readable description of a time span. E.g. "3 days 2 hours".
  80. """
  81. d: int = td.days
  82. h: int = td.seconds // 3600
  83. m: int = (td.seconds // 60) % 60
  84. s: int = td.seconds % 60
  85. components: list[str] = []
  86. if d != 0:
  87. components.append('1 day' if d == 1 else f'{d} days')
  88. if h != 0:
  89. components.append('1 hour' if h == 1 else f'{h} hours')
  90. if m != 0:
  91. components.append('1 minute' if m == 1 else f'{m} minutes')
  92. if s != 0 or len(components) == 0:
  93. components.append('1 second' if s == 1 else f'{s} seconds')
  94. if len(components) > max_components:
  95. components = components[0:max_components]
  96. return ' '.join(components)
  97. def _old_first_command_group(cog: Cog) -> Optional[discord.ext.commands.Group]:
  98. """Returns the first command Group found in a cog."""
  99. for member_name in dir(cog):
  100. member = getattr(cog, member_name)
  101. if isinstance(member, discord.ext.commands.Group):
  102. return member
  103. return None
  104. def first_command_group(cog: Cog) -> Optional[discord.app_commands.Group]:
  105. """Returns the first slash command Group found in a cog."""
  106. for member_name in dir(cog):
  107. member = getattr(cog, member_name)
  108. if isinstance(member, discord.app_commands.Group):
  109. return member
  110. return None
  111. def bot_log(guild: Optional[Guild], cog_class: Optional[type], message: Any) -> None:
  112. """Logs a message to stdout with time, cog, and guild info."""
  113. now: datetime = datetime.now() # local
  114. s = f'[{now.strftime("%Y-%m-%dT%H:%M:%S")}|'
  115. s += f'{cog_class.__name__}|' if cog_class else '-|'
  116. s += f'{guild.name}] ' if guild else '-] '
  117. s += str(message)
  118. print(s)
  119. __QUOTE_CHARS: str = '\'"'
  120. __ID_REGEX: re.Pattern = re.compile('^[0-9]{17,20}$')
  121. __MENTION_REGEX: re.Pattern = re.compile('^<@[!&]([0-9]{17,20})>$')
  122. __USER_MENTION_REGEX: re.Pattern = re.compile('^<@!([0-9]{17,20})>$')
  123. __ROLE_MENTION_REGEX: re.Pattern = re.compile('^<@&([0-9]{17,20})>$')
  124. def is_user_id(val: str) -> bool:
  125. """Tests if a string is in user/role ID format."""
  126. return __ID_REGEX.match(val) is not None
  127. def is_mention(val: str) -> bool:
  128. """Tests if a string is a user or role mention."""
  129. return __MENTION_REGEX.match(val) is not None
  130. def is_role_mention(val: str) -> bool:
  131. """Tests if a string is a role mention."""
  132. return __ROLE_MENTION_REGEX.match(val) is not None
  133. def is_user_mention(val: str) -> bool:
  134. """Tests if a string is a user mention."""
  135. return __USER_MENTION_REGEX.match(val) is not None
  136. def user_id_from_mention(mention: str) -> str:
  137. """Extracts the user ID from a mention. Raises a ValueError if malformed."""
  138. m = __USER_MENTION_REGEX.match(mention)
  139. if m:
  140. return m.group(1)
  141. raise ValueError(f'"{mention}" is not an @ user mention')
  142. def mention_from_user_id(user_id: Union[str, int]) -> str:
  143. """Returns a Markdown user mention from a user id."""
  144. return f'<@!{user_id}>'
  145. def mention_from_role_id(role_id: Union[str, int]) -> str:
  146. """Returns a Markdown role mention from a role id."""
  147. return f'<@&{role_id}>'
  148. def str_from_quoted_str(val: str) -> str:
  149. """Removes the leading and trailing quotes from a string."""
  150. if len(val) < 2 or val[0:1] not in __QUOTE_CHARS or val[-1:] not in __QUOTE_CHARS:
  151. raise ValueError(f'Not a quoted string: {val}')
  152. return val[1:-1]
  153. def blockquote_markdown(markdown: str) -> str:
  154. """Encloses some Markdown in a blockquote."""
  155. return '> ' + (markdown.replace('\n', '\n> '))
  156. def indent_markdown(markdown: str) -> str:
  157. """Indents a block of Markdown by one level."""
  158. return ' ' + (markdown.replace('\n', '\n '))
  159. def suppress_markdown_url_previews(markdown: str) -> str:
  160. """Finds URLs in markdown and encloses them in <...> to suppress the preview."""
  161. return re.sub(r'(?<!<)(https?://\S+)(?!>)', '<\\1>', markdown)
  162. def format_bytes(size: int) -> str:
  163. """Formats s size in bytes to a human readable description (e.g. "3.2 KiB")"""
  164. if size < 0:
  165. size = 0
  166. kib = 1024
  167. mib = kib * kib
  168. gib = mib * kib
  169. if size < kib:
  170. return f"{size:,} bytes"
  171. if size < 10 * kib:
  172. return f"{size/kib:,.1f} KiB"
  173. if size < mib:
  174. return f"{size/kib:,.0f} KiB"
  175. if size < 10 * mib:
  176. return f"{size/mib:,.1f} MiB"
  177. if size < gib:
  178. return f"{size/mib:,.0f} MiB"
  179. if size < 10 * gib:
  180. return f"{size/gib:,.1f} GiB"
  181. return f"{size/gib:,.0f} GiB"
  182. MOD_PERMISSIONS: Permissions = Permissions(Permissions.manage_messages.flag)
  183. class TimeDeltaTransformer(Transformer):
  184. async def transform(self, interaction: Interaction, value: Any) -> timedelta:
  185. try:
  186. return timedelta_from_str(str(value))
  187. except ValueError as e:
  188. print("Invalid time delta:", e)
  189. raise BadArgument(str(e))
  190. @property
  191. def _error_display_name(self) -> str:
  192. return 'timedelta'