general.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 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_message(self, message: discord.Message):
  26. # ActiveRPG
  27. #await self.bot.pg.execute("INSERT INTO \"user\"(user_id) VALUES($1) ON CONFLICT DO NOTHING", message.author.id)
  28. await create_user(self.bot.pg, message.author.id)
  29. try:
  30. #await self.bot.pg.execute("INSERT INTO channel_user(channel, \"user\") VALUES($1, $2) ON CONFLICT ON CONSTRAINT channel_user_channel_user_key DO UPDATE SET total_messages=channel_user.total_messages+1", message.channel.id, message.author.id)
  31. await upsert_total_messages(self.bot.pg,message.channel.id, message.author.id)
  32. except asyncpg.exceptions.ForeignKeyViolationError:
  33. try:
  34. #await self.bot.pg.execute("INSERT INTO channel(channel_id, guild) VALUES($1, $2)", message.channel.id, message.guild.id)
  35. await insert_channel(self.bot.pg, message.channel.id, message.guild.id)
  36. except asyncpg.exceptions.ForeignKeyViolationError:
  37. await update_guild(self.bot.pg, message.guild)
  38. # Do not respond to one self.
  39. if self.bot.user == message.author:
  40. pass
  41. # Respond when mentioned
  42. if self.bot.user.mentioned_in(message):
  43. print("mentioned in ")
  44. print(message.channel)
  45. #interact = get_interact
  46. print(interact)
  47. #if interact:
  48. if 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. "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. ]
  71. await message.reply(random.choice(messages))
  72. # Undelete last deleted message
  73. @commands.command(name="snipe")
  74. async def snipe(self, ctx: commands.Context):
  75. """A command to snipe delete messages."""
  76. if not self.last_msg: # on_message_delete hasn't been triggered since the bot started
  77. await ctx.send("There is no message to snipe!")
  78. return
  79. author = self.last_msg.author
  80. content = self.last_msg.content
  81. embed = discord.Embed(title=f"Message from {author}", description=content)
  82. await ctx.send(embed=embed)