general.py 11 KB

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