1
0

general.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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
  5. from query.channel_settings import get_interact
  6. from query.channel_user import upsert_total_messages
  7. from query.user import create_user
  8. from query.settings import get_crew_channel
  9. def setup(bot: commands.Bot):
  10. bot.add_cog(General(bot))
  11. class General(commands.Cog):
  12. """A couple of simple commands."""
  13. def __init__(self, bot: commands.Bot):
  14. self.bot = bot
  15. self.last_msg = None
  16. @commands.Cog.listener()
  17. async def on_ready(self):
  18. logging.info("Logged in as %s - %i", 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.id)
  22. @commands.Cog.listener()
  23. async def on_message_delete(self, message: discord.Message):
  24. self.last_msg = message
  25. @commands.Cog.listener()
  26. async def on_raw_member_remove(self, payload):
  27. channel = bot.get_channel(get_crew_channel(self.pg))
  28. if channel:
  29. await channel.send(f"{payload.user} has left {payload.guild_id}")
  30. @commands.Cog.listener()
  31. async def on_message(self, message: discord.Message):
  32. ## ActiveRPG
  33. # Create user, if not exists
  34. await create_user(self.bot.pg, message.author.id)
  35. # Count messages
  36. if message.guild: # Ignore DM's
  37. try:
  38. await upsert_total_messages(self.bot.pg,message.channel.id, message.author.id)
  39. except asyncpg.exceptions.ForeignKeyViolationError:
  40. try:
  41. print(message)
  42. print(message.channel)
  43. print(message.guild)
  44. await insert_channel(self.bot.pg, message.channel.id, message.guild.id)
  45. except asyncpg.exceptions.ForeignKeyViolationError:
  46. await update_guild(self.bot.pg, message.guild)
  47. # Do not respond to one self.
  48. if self.bot.user == message.author:
  49. pass
  50. # Respond when mentioned
  51. if self.bot.user.mentioned_in(message):
  52. print("mentioned in ")
  53. print(message.channel)
  54. #interact = get_interact()
  55. #print(interact)
  56. #if interact:
  57. if get_interact(self.bot.pg, message.channel.id):
  58. messages = [
  59. f"Hello {message.author.mention}. <3",
  60. f"How are you today {message.author.mention}?",
  61. f"I love you {message.author.mention}!",
  62. f"{message.author.mention}, would you like a hug?",
  63. "Is life treating you fair?",
  64. "What's up?",
  65. "Why are you talking to me?",
  66. "I'm not talking to you!",
  67. "What have you been up to?",
  68. "How is life?",
  69. "Kill all humans!",
  70. "What do you want from me?",
  71. f"{message.author.mention}, do you care for me?",
  72. f"{message.author.mention}, when will you stop talking about me?",
  73. f"{message.author.mention} I hate you!",
  74. f"{message.author.mention} I love you!",
  75. "Get bent!",
  76. "Go touch grass!",
  77. "Do you think i care about you?",
  78. f"Stop pinging me {message.author.mention}!",
  79. ]
  80. await message.reply(random.choice(messages))
  81. # Undelete last deleted message
  82. @commands.command(name="snipe")
  83. async def snipe(self, ctx: commands.Context):
  84. """A command to snipe delete messages."""
  85. if not self.last_msg: # on_message_delete hasn't been triggered since the bot started
  86. await ctx.send("There is no message to snipe!")
  87. return
  88. author = self.last_msg.author
  89. content = self.last_msg.content
  90. embed = discord.Embed(title=f"Message from {author}", description=content)
  91. await ctx.send(embed=embed)