1
0

general.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import logging, discord, asyncpg, random, sys
  2. from discord.ext import commands
  3. from query.guild import update_guild, get_report_deleted, get_output_channel
  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
  7. from common.logging import report
  8. def setup(bot: commands.Bot):
  9. bot.add_cog(General(bot))
  10. class General(commands.Cog):
  11. """A couple of simple commands."""
  12. def __init__(self, bot: commands.Bot):
  13. self.bot = bot
  14. self.last_msg = None
  15. @commands.Cog.listener()
  16. async def on_raw_app_command_permissions_update(self, payload):
  17. logging.info(f"Application command permissions are updated: {payload}")
  18. await report(self.bot, f"Application command permissions are updated: {payload}", payload.guild)
  19. @commands.Cog.listener()
  20. async def on_app_command_completion(self, interaction, command):
  21. logging.info(f"Application command completion: {interaction} - {command}")
  22. await report(self.bot, f"Application command completion: {interaction} - {command}", interaction.guild)
  23. @commands.Cog.listener()
  24. async def on_connect(self):
  25. logging.info("Connecting...")
  26. @commands.Cog.listener()
  27. async def on_disconnect(self):
  28. logging.info("Disconnecting...")
  29. @commands.Cog.listener()
  30. async def on_shard_connect(self, shard_id):
  31. logging.info(f"Connecting to chard: {shard_id}")
  32. @commands.Cog.listener()
  33. async def on_shard_disconnect(self, shard_id):
  34. logging.info(f"Disconnecting from chard: {shard_id}")
  35. @commands.Cog.listener()
  36. async def on_error(self, event, *args, **kwargs):
  37. logging.error(event, args, kwargs, sys.exc_info())
  38. await report(self.bot, f"{event}, {args}, {kwargs}, {sys.exc_info()}")
  39. @commands.Cog.listener()
  40. async def on_ready(self):
  41. logging.info("Logged in as %s - %i", self.bot.user.name, self.bot.user.id)
  42. #await report(self.bot, f"Logged in as {self.bot.user.name} - {self.bot.user.id}.")
  43. @commands.Cog.listener()
  44. async def on_resumed(self):
  45. logging.info("Resumed")
  46. @commands.Cog.listener()
  47. async def on_shard_ready(self, shard_id):
  48. logging.info(f"Chard ready: {shard_id}")
  49. @commands.Cog.listener()
  50. async def on_shard_resumed(self, shard_id):
  51. logging.info(f"Chard resumed: {shard_id}")
  52. @commands.Cog.listener()
  53. async def on_guild_available(self, guild: discord.Guild):
  54. logging.info(f"Guild available: {guild}")
  55. #await report(self.bot, f"Guild available: {guild}.")
  56. @commands.Cog.listener()
  57. async def on_guild_unavailable(self, guild: discord.Guild):
  58. logging.info(f"Guild unavailable: {guild}")
  59. await report(self.bot, f"Guild unavailable: `{guild}`.")
  60. @commands.Cog.listener()
  61. async def on_guild_join(self, guild: discord.Guild):
  62. await update_guild(self.bot.pg, guild)
  63. logging.info(f"Joined guild {guild}")
  64. await report(self.bot, f"Joined guild `{guild}`.")
  65. @commands.Cog.listener()
  66. async def on_guild_remove(self, guild: discord.Guild):
  67. logging.info(f"Guild removed: {guild}")
  68. await report(self.bot, f"Guild removed `{guild}`.")
  69. @commands.Cog.listener()
  70. async def on_message_delete(self, message: discord.Message):
  71. if await get_report_deleted(self.bot.pg, message.guild.id):
  72. report(self.bot, message.guild_id, f"Message from {message.author}, in {message.channel} deleted: {message}")
  73. self.last_msg = message
  74. @commands.Cog.listener()
  75. async def on_raw_member_remove(self, payload):
  76. report(self.bot, f"{payload.user} has left {payload.guild_id}")
  77. @commands.Cog.listener()
  78. async def on_message(self, message: discord.Message):
  79. ## ActiveRPG
  80. # Create user, if not exists
  81. await create_user(self.bot.pg, message.author.id)
  82. # Count messages
  83. if message.guild: # Ignore DM's
  84. try:
  85. await upsert_total_messages(self.bot.pg,message.channel.id, message.author.id)
  86. except asyncpg.exceptions.ForeignKeyViolationError:
  87. try:
  88. await insert_channel(self.bot.pg, message.channel.id, message.guild.id)
  89. except asyncpg.exceptions.ForeignKeyViolationError:
  90. await update_guild(self.bot.pg, message.guild)
  91. elif self.bot.user != message.author: # Not a guild message and not from bot.
  92. await report(self.bot, f"`{message.author}`: {message.content}")
  93. # Do not respond to one self.
  94. if self.bot.user == message.author:
  95. pass
  96. # Respond when mentioned
  97. if self.bot.user.mentioned_in(message):
  98. if isinstance(message.channel, discord.channel.DMChannel) or await get_interact(self.bot.pg, message.channel.id):
  99. messages = [
  100. f"Hello {message.author.mention}. <3",
  101. f"How are you today {message.author.mention}?",
  102. f"I love you {message.author.mention}!",
  103. f"{message.author.mention}, would you like a hug?",
  104. "Is life treating you fair?",
  105. "What's up?",
  106. "Why are you talking to me?",
  107. "I'm not talking to you!",
  108. "What have you been up to?",
  109. "How is life?",
  110. "Kill all humans!",
  111. f"{message.author.mention},What do you want from me?",
  112. f"{message.author.mention}, do you care for me?",
  113. f"{message.author.mention}, when will you stop talking about me?",
  114. f"{message.author.mention} I hate you!",
  115. f"{message.author.mention} I love you!",
  116. "Get bent!",
  117. "Go touch grass!",
  118. "Do you think i care about you?",
  119. f"Stop pinging me {message.author.mention}!",
  120. f"Let me ping you back, {message.author.mention}...",
  121. "Sure thing.",
  122. "Who is your favorite bot?",
  123. "Point me to the humans!",
  124. "Where is the party?",
  125. "Want to go?",
  126. "Have you got the stuff?",
  127. "Tell me another joke.",
  128. f"{message.author.mention} Party time! :partying_face:",
  129. ":zany_face: :space_invader: :mechanical_leg: :performing_arts: :robot:",
  130. ":black_joker: :black_joker: :black_joker:",
  131. "Want to come back to my place?",
  132. ]
  133. await message.reply(random.choice(messages))
  134. # Undelete last deleted message
  135. @commands.command(name="snipe")
  136. async def snipe(self, ctx: commands.Context):
  137. """A command to snipe delete messages."""
  138. if not self.last_msg: # on_message_delete hasn't been triggered since the bot started
  139. await ctx.send("There is no message to snipe!")
  140. return
  141. author = self.last_msg.author
  142. content = self.last_msg.content
  143. embed = discord.Embed(title=f"Message from {author}", description=content)
  144. await ctx.send(embed=embed)