general.py 3.7 KB

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