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.

basecog.py 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. """
  2. Base cog class and helper classes.
  3. """
  4. from datetime import datetime, timedelta, timezone
  5. from typing import Optional
  6. from discord import Guild, Member, Message, RawReactionActionEvent, TextChannel
  7. from discord.abc import GuildChannel
  8. from discord.ext import commands
  9. from config import CONFIG
  10. from rocketbot.bot import Rocketbot
  11. from rocketbot.botmessage import BotMessage, BotMessageReaction
  12. from rocketbot.cogsetting import CogSetting
  13. from rocketbot.collections import AgeBoundDict
  14. from rocketbot.storage import Storage
  15. from rocketbot.utils import bot_log
  16. class WarningContext:
  17. def __init__(self, member: Member, warn_time: datetime):
  18. self.member = member
  19. self.last_warned = warn_time
  20. class BaseCog(commands.Cog):
  21. STATE_KEY_RECENT_WARNINGS = "BaseCog.recent_warnings"
  22. """
  23. Superclass for all Rocketbot cogs. Provides lots of conveniences for
  24. common tasks.
  25. """
  26. def __init__(self, bot: Rocketbot, config_prefix: Optional[str]):
  27. """
  28. Parameters
  29. ----------
  30. bot: Rocketbot
  31. config_prefix: str
  32. Prefix to show on variables in /set and /get commands to namespace
  33. configuration variables. E.g. if config_prefix is "foo", a config
  34. variable named "bar" in that cog will show as "foo.bar". If None,
  35. config variable acts as a top-level variable with no prefix.
  36. """
  37. self.bot: Rocketbot = bot
  38. self.are_settings_setup: bool = False
  39. self.settings: list[CogSetting] = []
  40. self.config_prefix: Optional[str] = config_prefix
  41. # Config
  42. @classmethod
  43. def get_cog_default(cls, key: str):
  44. """
  45. Convenience method for getting a cog configuration default from
  46. `CONFIG['cogs'][<cog_name>][<key>]`. These values are used for
  47. CogSettings when no guild-specific value is configured yet.
  48. """
  49. cogs: dict = CONFIG['cog_defaults']
  50. cog = cogs.get(cls.__name__)
  51. if cog is None:
  52. return None
  53. return cog.get(key)
  54. def add_setting(self, setting: CogSetting) -> None:
  55. """
  56. Called by a subclass in __init__ to register a mod-configurable
  57. guild setting. A "get" and "set" command will be generated. If the
  58. setting is named "enabled" (exactly) then "enable" and "disable"
  59. commands will be created instead which set the setting to True/False.
  60. If the cog has a command group it will be detected automatically and
  61. the commands added to that. Otherwise, the commands will be added at
  62. the top level.
  63. Changes to settings can be detected by overriding `on_setting_updated`.
  64. """
  65. self.settings.append(setting)
  66. @classmethod
  67. def get_guild_setting(cls,
  68. guild: Guild,
  69. setting: CogSetting,
  70. use_cog_default_if_not_set: bool = True):
  71. """
  72. Returns the configured value for a setting for the given guild. If no
  73. setting is configured the default for the cog will be returned,
  74. unless the optional `use_cog_default_if_not_set` is `False`, then
  75. `None` will be returned.
  76. """
  77. key = f'{cls.__name__}.{setting.name}'
  78. value = Storage.get_config_value(guild, key)
  79. if value is None and use_cog_default_if_not_set:
  80. value = cls.get_cog_default(setting.name)
  81. return value
  82. @classmethod
  83. def set_guild_setting(cls,
  84. guild: Guild,
  85. setting: CogSetting,
  86. new_value) -> None:
  87. """
  88. Manually sets a setting for the given guild. BaseCog creates "get" and
  89. "set" commands for guild administrators to configure values themselves,
  90. but this method can be used for hidden settings from code. A ValueError
  91. will be raised if the new value does not pass validation specified in
  92. the CogSetting.
  93. """
  94. setting.validate_value(new_value)
  95. key = f'{cls.__name__}.{setting.name}'
  96. Storage.set_config_value(guild, key, new_value)
  97. @commands.Cog.listener()
  98. async def on_ready(self):
  99. """Event listener"""
  100. if not self.are_settings_setup:
  101. self.are_settings_setup = True
  102. CogSetting.set_up_all(self, self.bot, self.settings)
  103. async def on_setting_updated(self, guild: Guild, setting: CogSetting) -> None:
  104. """
  105. Subclass override point for being notified when a CogSetting is edited.
  106. """
  107. # Warning squelch
  108. def was_warned_recently(self, member: Member) -> bool:
  109. """
  110. Tests if a given member was included in a mod warning message recently.
  111. Used to suppress redundant messages. Should be checked before pinging
  112. mods for relatively minor warnings about single users, but warnings
  113. about larger threats involving several members (e.g. join raids) should
  114. issue warnings regardless. Call record_warning or record_warnings after
  115. triggering a mod warning.
  116. """
  117. recent_warns: AgeBoundDict[int, WarningContext, datetime, timedelta] = Storage.get_state_value(member.guild,
  118. BaseCog.STATE_KEY_RECENT_WARNINGS)
  119. if recent_warns is None:
  120. return False
  121. context: WarningContext = recent_warns.get(member.id)
  122. if context is None:
  123. return False
  124. squelch_warning_seconds: int = CONFIG['squelch_warning_seconds']
  125. return datetime.now() - context.last_warned < timedelta(seconds=squelch_warning_seconds)
  126. def record_warning(self, member: Member):
  127. """
  128. Records that mods have been warned about a member and do not need to be
  129. warned about them again for a short while.
  130. """
  131. recent_warns: AgeBoundDict[int, WarningContext, datetime, timedelta] = Storage.get_state_value(member.guild,
  132. BaseCog.STATE_KEY_RECENT_WARNINGS)
  133. if recent_warns is None:
  134. recent_warns = AgeBoundDict(timedelta(seconds=CONFIG['squelch_warning_seconds']),
  135. lambda i, context : context.last_warned)
  136. Storage.set_state_value(member.guild, BaseCog.STATE_KEY_RECENT_WARNINGS, recent_warns)
  137. context: WarningContext = recent_warns.get(member.id)
  138. if context is None:
  139. context = WarningContext(member, datetime.now())
  140. recent_warns[member.id] = context
  141. else:
  142. context.last_warned = datetime.now()
  143. def record_warnings(self, members: list):
  144. """
  145. Records that mods have been warned about some members and do not need to
  146. be warned about them again for a short while.
  147. """
  148. for member in members:
  149. self.record_warning(member)
  150. # Bot message handling
  151. @classmethod
  152. def __bot_messages(cls, guild: Guild) -> AgeBoundDict[int, BotMessage, datetime, timedelta]:
  153. bm: AgeBoundDict[int, BotMessage, datetime, timedelta] = Storage.get_state_value(guild, 'bot_messages')
  154. if bm is None:
  155. far_future = datetime.now(timezone.utc) + timedelta(days=1000)
  156. bm = AgeBoundDict(timedelta(seconds=600),
  157. lambda k, v : v.message_sent_at() or far_future)
  158. Storage.set_state_value(guild, 'bot_messages', bm)
  159. return bm
  160. async def post_message(self, message: BotMessage) -> bool:
  161. """
  162. Posts a BotMessage to a guild. Returns whether it was successful. If
  163. the caller wants to listen to reactions they should be added before
  164. calling this method. Listen to reactions by overriding `on_mod_react`.
  165. """
  166. message.source_cog = self
  167. await message.update()
  168. return message.is_sent()
  169. @commands.Cog.listener()
  170. async def on_raw_reaction_add(self, payload: RawReactionActionEvent):
  171. """Event handler"""
  172. # Avoid any unnecessary requests. Gets called for every reaction
  173. # multiplied by every active cog.
  174. if payload.user_id == self.bot.user.id:
  175. # Ignore bot's own reactions
  176. return
  177. guild: Guild = self.bot.get_guild(payload.guild_id) or await self.bot.fetch_guild(payload.guild_id)
  178. if guild is None:
  179. # Possibly a DM
  180. return
  181. guild_messages: dict[int, BotMessage] = Storage.get_bot_messages(guild)
  182. bot_message = guild_messages.get(payload.message_id)
  183. if bot_message is None:
  184. # Unknown message (expired or was never tracked)
  185. return
  186. if self is not bot_message.source_cog:
  187. # Belongs to a different cog
  188. return
  189. reaction = bot_message.reaction_for_emoji(payload.emoji)
  190. if reaction is None or not reaction.is_enabled:
  191. # Can't use this reaction with this message
  192. return
  193. g_channel: GuildChannel = guild.get_channel(payload.channel_id) or await guild.fetch_channel(payload.channel_id)
  194. if g_channel is None:
  195. # Possibly a DM
  196. return
  197. if not isinstance(g_channel, TextChannel):
  198. return
  199. channel: TextChannel = g_channel
  200. member: Member = payload.member
  201. if member is None:
  202. return
  203. if not channel.permissions_for(member).ban_members:
  204. # Not a mod (could make permissions configurable per BotMessageReaction some day)
  205. return
  206. message: Message = await channel.fetch_message(payload.message_id)
  207. if message is None:
  208. # Message deleted?
  209. return
  210. if message.author.id != self.bot.user.id:
  211. # Bot didn't author this
  212. return
  213. await self.on_mod_react(bot_message, reaction, member)
  214. async def on_mod_react(self,
  215. bot_message: BotMessage,
  216. reaction: BotMessageReaction,
  217. reacted_by: Member) -> None:
  218. """
  219. Subclass override point for receiving mod reactions to bot messages sent
  220. via `post_message()`.
  221. """
  222. # Helpers
  223. @classmethod
  224. def log(cls, guild: Optional[Guild], message) -> None:
  225. """
  226. Writes a message to the console. Intended for significant events only.
  227. """
  228. bot_log(guild, cls, message)