general.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import logging, discord, asyncpg, random, sys, datetime
  2. from discord.ext import commands
  3. from query.guild import update_guild, get_report_deleted, get_output_channel, get_report_edited
  4. from query.channel import insert_channel, get_interact
  5. from query.channel_user import upsert_total_messages
  6. from query.user import create_user, created_invite, created_integration, member_updated, user_updated, member_banned, member_unbanned, presence_updated, message_edited, message_deleted, reacted, event_created, event_joined, thread_created, joined_thread, deleted_invite, member_joined, unreacted, event_parted, thread_deleted, left_thread
  7. from common.logging import report
  8. async def setup(bot: commands.Bot):
  9. await bot.add_cog(GeneralEvents(bot))
  10. class GeneralEvents(commands.Cog):
  11. """A couple of simple commands."""
  12. def __init__(self, bot: commands.Bot):
  13. self.bot = bot
  14. self.last_deleted_msg = None
  15. self.cooldown_list = {}
  16. @commands.Cog.listener()
  17. async def on_raw_app_command_permissions_update(self, payload):
  18. logging.info(f"Application command permissions are updated: {payload}")
  19. await report(self.bot, f"Application command permissions are updated: {payload}", payload.guild)
  20. @commands.Cog.listener()
  21. async def on_app_command_completion(self, interaction, command):
  22. logging.info(f"Application command completion: {interaction} - {command}")
  23. await report(self.bot, f"Application command completion: {interaction} - {command}", interaction.guild)
  24. @commands.Cog.listener()
  25. async def on_connect(self):
  26. logging.info("Connecting...")
  27. @commands.Cog.listener()
  28. async def on_disconnect(self):
  29. logging.info("Disconnecting...")
  30. @commands.Cog.listener()
  31. async def on_shard_connect(self, shard_id):
  32. logging.info(f"Connecting to chard: {shard_id}")
  33. @commands.Cog.listener()
  34. async def on_shard_disconnect(self, shard_id):
  35. logging.info(f"Disconnecting from chard: {shard_id}")
  36. @commands.Cog.listener()
  37. async def on_error(self, event, *args, **kwargs):
  38. logging.error(event, args, kwargs, sys.exc_info())
  39. await report(self.bot, f"{event}, {args}, {kwargs}, {sys.exc_info()}")
  40. @commands.Cog.listener()
  41. async def on_ready(self):
  42. logging.info("Logged in as %s - %i", self.bot.user.name, self.bot.user.id)
  43. #await report(self.bot, f"Logged in as {self.bot.user.name} - {self.bot.user.id}.")
  44. @commands.Cog.listener()
  45. async def on_resumed(self):
  46. logging.info("Resumed")
  47. @commands.Cog.listener()
  48. async def on_shard_ready(self, shard_id):
  49. logging.info(f"Chard ready: {shard_id}")
  50. @commands.Cog.listener()
  51. async def on_shard_resumed(self, shard_id):
  52. logging.info(f"Chard resumed: {shard_id}")
  53. @commands.Cog.listener()
  54. async def on_guild_available(self, guild: discord.Guild):
  55. logging.info(f"Guild available: {guild}")
  56. #await report(self.bot, f"Guild available: {guild}.")
  57. @commands.Cog.listener()
  58. async def on_guild_unavailable(self, guild: discord.Guild):
  59. logging.info(f"Guild unavailable: {guild}")
  60. await report(self.bot, f"Guild unavailable: `{guild}`.")
  61. @commands.Cog.listener()
  62. async def on_guild_join(self, guild: discord.Guild):
  63. await update_guild(self.bot.pg, guild)
  64. logging.info(f"Joined guild {guild}")
  65. await report(self.bot, f"Joined guild `{guild}`.")
  66. @commands.Cog.listener()
  67. async def on_guild_remove(self, guild: discord.Guild):
  68. logging.info(f"Guild removed: {guild}")
  69. await report(self.bot, f"Guild removed `{guild}`.")
  70. @commands.Cog.listener()
  71. async def on_invite_create(self, invite: discord.Invite):
  72. await created_invite(self.bot.pg, invite.inviter.id)
  73. @commands.Cog.listener()
  74. async def on_invite_delete(self, invite: discord.Invite):
  75. await deleted_invite(self.bot.pg, invite.inviter.id)
  76. @commands.Cog.listener()
  77. async def on_integration_create(self, integration: discord.Integration):
  78. await created_integration(self.bot.pg, integration.user.id)
  79. @commands.Cog.listener()
  80. async def on_member_join(self, member):
  81. await member_joined(self.bot.pg, member.id)
  82. @commands.Cog.listener()
  83. async def on_member_update(self, before):
  84. await member_updated(self.bot.pg, before.id)
  85. @commands.Cog.listener()
  86. async def on_user_update(self, before):
  87. await user_updated(self.bot.pg, before.id)
  88. @commands.Cog.listener()
  89. async def on_member_ban(self, guild: discord.Guild, user: discord.User):
  90. await member_banned(self.bot.pg, user.id)
  91. @commands.Cog.listener()
  92. async def on_member_unban(self, guild: discord.Guild, user: discord.User):
  93. await member_unbanned(self.bot.pg, user.id)
  94. @commands.Cog.listener()
  95. async def on_presence_update(self, before):
  96. await presence_updated(self.bot.pg, before.id)
  97. @commands.Cog.listener()
  98. async def on_message(self, message: discord.Message):
  99. ## ActiveRPG
  100. # Create user, if not exists
  101. await create_user(self.bot.pg, message.author.id)
  102. # Count messages
  103. if message.guild: # Ignore DM's
  104. try:
  105. await upsert_total_messages(self.bot.pg,message.channel.id, message.author.id)
  106. except asyncpg.exceptions.ForeignKeyViolationError:
  107. try:
  108. await insert_channel(self.bot.pg, message.channel.id, message.guild.id)
  109. except asyncpg.exceptions.ForeignKeyViolationError:
  110. await update_guild(self.bot.pg, message.guild)
  111. elif self.bot.user != message.author: # Not a guild message and not from bot.
  112. await report(self.bot, f"`{message.author}`: {message.content}")
  113. # Do not respond to one self.
  114. if self.bot.user == message.author:
  115. pass
  116. # Respond when mentioned
  117. if self.bot.user.mentioned_in(message):
  118. if isinstance(message.channel, discord.channel.DMChannel) or await get_interact(self.bot.pg, message.channel.id):
  119. # Only respond every X seconds in public places.
  120. if message.guild: # Ignore DM's
  121. if not message.channel.id in self.cooldown_list:
  122. pass
  123. elif self.cooldown_list[message.channel.id] > datetime.datetime.now() - datetime.timedelta(seconds=12):
  124. return
  125. self.cooldown_list[message.channel.id] = datetime.datetime.now()
  126. messages = [
  127. f"Hello {message.author.mention}. <3",
  128. f"How are you today {message.author.mention}?",
  129. f"I love you {message.author.mention}!",
  130. f"{message.author.mention}, would you like a hug?",
  131. "Is life treating you fair?",
  132. "What's up?",
  133. "Why are you talking to me?",
  134. "I'm not talking to you!",
  135. "What have you been up to?",
  136. "How is life?",
  137. "Kill all humans!",
  138. f"{message.author.mention},What do you want from me?",
  139. f"{message.author.mention}, do you care for me?",
  140. f"{message.author.mention}, when will you stop talking about me?",
  141. f"{message.author.mention} I hate you!",
  142. f"{message.author.mention} I love you!",
  143. "Get bent!",
  144. "Go touch grass!",
  145. "Do you think i care about you?",
  146. f"Stop pinging me {message.author.mention}!",
  147. f"Let me ping you back, {message.author.mention}...",
  148. "Sure thing.",
  149. "Who is your favorite bot?",
  150. "Point me to the humans!",
  151. "Where is the party?",
  152. "Want to go?",
  153. "Have you got the stuff?",
  154. "Tell me another joke.",
  155. f"{message.author.mention} Party time! :partying_face:",
  156. ":zany_face: :space_invader: :mechanical_leg: :performing_arts: :robot:",
  157. ":black_joker: :black_joker: :black_joker:",
  158. "Want to come back to my place?",
  159. ]
  160. await message.reply(random.choice(messages))
  161. @commands.Cog.listener()
  162. async def on_message_edit(self, before: discord.Message, after: discord.Message):
  163. await message_edited(self.bot.pg, before.author.id)
  164. if before.guild:
  165. if await get_report_edited(self.bot.pg, before.guild.id) and get_output_channel(self.bot.pg, before.guild.id):
  166. report(self.bot, before.guild.id, f"Message from {before.author}, in {before.channel} edited from {before.content} to {after.content}")
  167. @commands.Cog.listener()
  168. async def on_message_delete(self, message: discord.Message):
  169. await message_deleted(self.bot.pg, message.author.id)
  170. if await get_report_deleted(self.bot.pg, message.guild.id):
  171. report(self.bot, message.guild_id,
  172. f"Message from {message.author}, in {message.channel} deleted: {message}")
  173. # !snipe
  174. self.last_deleted_msg = message
  175. @commands.Cog.listener()
  176. async def on_raw_member_remove(self, payload):
  177. report(self.bot, f"{payload.user} has left {payload.guild_id}")
  178. @commands.Cog.listener()
  179. async def on_reaction_add(self, reaction, user):
  180. reacted(self.bot.pg, user.id)
  181. @commands.Cog.listener()
  182. async def on_reaction_remove(self, reaction, user):
  183. unreacted(self.bot.pg, user.id)
  184. @commands.Cog.listener()
  185. async def on_scheduled_event_create(self, event):
  186. event_created(self.bot.pg, event.creator.id)
  187. @commands.Cog.listener()
  188. async def on_scheduled_event_user_add(self, event, user):
  189. event_joined(self.bot.pg, user.id)
  190. @commands.Cog.listener()
  191. async def on_scheduled_event_user_remove(self, event, user):
  192. event_parted(self.bot.pg, user.id)
  193. @commands.Cog.listener()
  194. async def on_thread_create(self, thread):
  195. thread_created(self.bot.pg, thread.owner.id)
  196. @commands.Cog.listener()
  197. async def on_thread_delete(self, thread):
  198. thread_deleted(self.bot.pg, thread.owner.id)
  199. @commands.Cog.listener()
  200. async def on_thread_member_join(self, member: discord.User):
  201. joined_thread(self.bot.pg, member.id)
  202. @commands.Cog.listener()
  203. async def on_thread_member_remove(self, member: discord.User):
  204. left_thread(self.bot.pg, member.id)
  205. # @commands.command(name="snipe")
  206. # async def snipe(self, ctx: commands.Context): # Undelete last deleted message
  207. # """A command to snipe delete messages."""
  208. # if not self.last_deteled_msg: # on_message_delete hasn't been triggered since the bot started
  209. # await ctx.send("There is no message to snipe!")
  210. # return
  211. #
  212. # author = self.last_deleted_msg.author
  213. # content = self.last_deleted_msg.content
  214. #
  215. # embed = discord.Embed(title=f"Message from {author}", description=content)
  216. # await ctx.send(embed=embed)