general.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import logging, discord, asyncpg, random
  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_ready(self):
  17. logging.info("Logged in as %s - %i", self.bot.user.name, self.bot.user.id)
  18. #await report(self.bot, f"Logged in as {self.bot.user.name} - {self.bot.user.id}.")
  19. @commands.Cog.listener()
  20. async def on_guild_join(self, guild: discord.Guild):
  21. await update_guild(self.bot.pg, guild)
  22. logging.info(f"Joined guild {guild}")
  23. await report(self.bot, f"Joined guild {guild.name}.")
  24. @commands.Cog.listener()
  25. async def on_message_delete(self, message: discord.Message):
  26. if await get_report_deleted(self.bot.pg, message.guild.id):
  27. report(self.bot, message.guild_id, f"Message from {message.author}, in {message.channel} deleted: {message}")
  28. self.last_msg = message
  29. @commands.Cog.listener()
  30. async def on_raw_member_remove(self, payload):
  31. report(self.bot, f"{payload.user} has left {payload.guild_id}")
  32. @commands.Cog.listener()
  33. async def on_message(self, message: discord.Message):
  34. ## ActiveRPG
  35. # Create user, if not exists
  36. await create_user(self.bot.pg, message.author.id)
  37. # Count messages
  38. if message.guild: # Ignore DM's
  39. try:
  40. await upsert_total_messages(self.bot.pg,message.channel.id, message.author.id)
  41. except asyncpg.exceptions.ForeignKeyViolationError:
  42. try:
  43. await insert_channel(self.bot.pg, message.channel.id, message.guild.id)
  44. except asyncpg.exceptions.ForeignKeyViolationError:
  45. await update_guild(self.bot.pg, message.guild)
  46. elif self.bot.user != message.author: # Not a guild message and not from bot.
  47. await report(self.bot, f"`{message.author}`: {message.content}")
  48. # Do not respond to one self.
  49. if self.bot.user == message.author:
  50. pass
  51. # Respond when mentioned
  52. if self.bot.user.mentioned_in(message):
  53. if isinstance(message.channel, discord.channel.DMChannel) or await get_interact(self.bot.pg, message.channel.id):
  54. messages = [
  55. f"Hello {message.author.mention}. <3",
  56. f"How are you today {message.author.mention}?",
  57. f"I love you {message.author.mention}!",
  58. f"{message.author.mention}, would you like a hug?",
  59. "Is life treating you fair?",
  60. "What's up?",
  61. "Why are you talking to me?",
  62. "I'm not talking to you!",
  63. "What have you been up to?",
  64. "How is life?",
  65. "Kill all humans!",
  66. f"{message.author.mention},What do you want from me?",
  67. f"{message.author.mention}, do you care for me?",
  68. f"{message.author.mention}, when will you stop talking about me?",
  69. f"{message.author.mention} I hate you!",
  70. f"{message.author.mention} I love you!",
  71. "Get bent!",
  72. "Go touch grass!",
  73. "Do you think i care about you?",
  74. f"Stop pinging me {message.author.mention}!",
  75. f"Let me ping you back, {message.author.mention}...",
  76. "Sure thing.",
  77. "Who is your favorite bot?",
  78. "Point me to the humans!",
  79. "Where is the party?",
  80. "Want to go?",
  81. "Have you got the stuff?",
  82. "Tell me another joke.",
  83. f"{message.author.mention} Party time! :partying_face:",
  84. ":zany_face: :space_invader: :mechanical_leg: :performing_arts: :robot:",
  85. ":black_joker: :black_joker: :black_joker:",
  86. "Want to come back to my place?",
  87. ]
  88. await message.reply(random.choice(messages))
  89. # Undelete last deleted message
  90. @commands.command(name="snipe")
  91. async def snipe(self, ctx: commands.Context):
  92. """A command to snipe delete messages."""
  93. if not self.last_msg: # on_message_delete hasn't been triggered since the bot started
  94. await ctx.send("There is no message to snipe!")
  95. return
  96. author = self.last_msg.author
  97. content = self.last_msg.content
  98. embed = discord.Embed(title=f"Message from {author}", description=content)
  99. await ctx.send(embed=embed)