1
0

general.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. # Do not respond to one self.
  47. if self.bot.user == message.author:
  48. pass
  49. # Respond when mentioned
  50. if self.bot.user.mentioned_in(message):
  51. print(f"Message channel ID: {message.channel.id}")
  52. if await get_interact(self.bot.pg, message.channel.id):
  53. messages = [
  54. f"Hello {message.author.mention}. <3",
  55. f"How are you today {message.author.mention}?",
  56. f"I love you {message.author.mention}!",
  57. f"{message.author.mention}, would you like a hug?",
  58. "Is life treating you fair?",
  59. "What's up?",
  60. "Why are you talking to me?",
  61. "I'm not talking to you!",
  62. "What have you been up to?",
  63. "How is life?",
  64. "Kill all humans!",
  65. f"{message.author.mention},What do you want from me?",
  66. f"{message.author.mention}, do you care for me?",
  67. f"{message.author.mention}, when will you stop talking about me?",
  68. f"{message.author.mention} I hate you!",
  69. f"{message.author.mention} I love you!",
  70. "Get bent!",
  71. "Go touch grass!",
  72. "Do you think i care about you?",
  73. f"Stop pinging me {message.author.mention}!",
  74. f"Let me ping you back, {message.author.mention}...",
  75. "Sure thing.",
  76. "Who is your favorite bot?",
  77. "Point me to the humans!",
  78. "Where is the party?",
  79. "Want to go?",
  80. "Have you got the stuff?",
  81. "Tell me another joke.",
  82. f"{message.author.mention} Party time! :partying_face:",
  83. ":zany_face: :space_invader: :mechanical_leg: :performing_arts: :robot:",
  84. ":black_joker: :black_joker: :black_joker:",
  85. "Want to come back to my place?",
  86. ]
  87. await message.reply(random.choice(messages))
  88. # Undelete last deleted message
  89. @commands.command(name="snipe")
  90. async def snipe(self, ctx: commands.Context):
  91. """A command to snipe delete messages."""
  92. if not self.last_msg: # on_message_delete hasn't been triggered since the bot started
  93. await ctx.send("There is no message to snipe!")
  94. return
  95. author = self.last_msg.author
  96. content = self.last_msg.content
  97. embed = discord.Embed(title=f"Message from {author}", description=content)
  98. await ctx.send(embed=embed)