general.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import logging, discord, asyncpg, random
  2. from discord.ext import commands
  3. from query.guild import update_guild
  4. def setup(bot: commands.Bot):
  5. bot.add_cog(General(bot))
  6. class General(commands.Cog):
  7. """A couple of simple commands."""
  8. def __init__(self, bot: commands.Bot):
  9. self.bot = bot
  10. self.last_msg = None
  11. @commands.Cog.listener()
  12. async def on_ready(self):
  13. logging.info("Logged in as %s - %i", self.bot.user.name, self.bot.user.id)
  14. @commands.Cog.listener()
  15. async def on_guild_join(self, guild: discord.Guild):
  16. update_guild(guild)
  17. @commands.Cog.listener()
  18. async def on_message_delete(self, message: discord.Message):
  19. self.last_msg = message
  20. @commands.Cog.listener()
  21. async def on_message(self, message: discord.Message):
  22. # ActiveRPG
  23. await self.bot.pg.execute("INSERT INTO \"user\"(user_id) VALUES($1) ON CONFLICT DO NOTHING", message.author.id)
  24. try:
  25. 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)
  26. except asyncpg.exceptions.ForeignKeyViolationError:
  27. try:
  28. await self.bot.pg.execute("INSERT INTO channel(channel_id, guild) VALUES($1, $2)", message.channel.id, message.guild.id)
  29. except asyncpg.exceptions.ForeignKeyViolationError:
  30. update_guild(message.guild.id)
  31. # Do not respond to one self.
  32. if self.bot.user == message.author:
  33. pass
  34. # Respond when mentioned
  35. if self.bot.user.mentioned_in(message):
  36. print("mentioned in ")
  37. print(message.channel)
  38. interact = await self.bot.pg.fetchrow("SELECT interact FROM channel_settings WHERE channel_id=$1::bigint", message.channel.id)
  39. print(interact)
  40. if interact:
  41. messages = [
  42. f"Hello {message.author.mention}. <3",
  43. f"How are you today {message.author.mention}?",
  44. f"I love you {message.author.mention}!",
  45. f"{message.author.mention}, would you like a hug?",
  46. "Is life treating you fair?",
  47. "What's up?",
  48. "Why are you talking to me?",
  49. "I'm not talking to you!",
  50. "What have you been up to?",
  51. "How is life?",
  52. "Kill all humans!",
  53. "What do you want from me?",
  54. f"{message.author.mention}, do you care for me?",
  55. f"{message.author.mention}, when will you stop talking about me?",
  56. f"{message.author.mention} I hate you!",
  57. f"{message.author.mention} I love you!",
  58. "Get bent!",
  59. "Go touch grass!",
  60. "Do you think i care about you?",
  61. f"Stop pinging me {message.author.mention}!",
  62. ]
  63. await message.reply(random.choice(messages))
  64. # Undelete last deleted message
  65. @commands.command(name="snipe")
  66. async def snipe(self, ctx: commands.Context):
  67. """A command to snipe delete messages."""
  68. if not self.last_msg: # on_message_delete hasn't been triggered since the bot started
  69. await ctx.send("There is no message to snipe!")
  70. return
  71. author = self.last_msg.author
  72. content = self.last_msg.content
  73. embed = discord.Embed(title=f"Message from {author}", description=content)
  74. await ctx.send(embed=embed)