| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- import logging, discord, asyncpg, random, sys
- from discord.ext import commands
- from query.guild import update_guild, get_report_deleted, get_output_channel
- from query.channel import insert_channel, get_interact
- from query.channel_user import upsert_total_messages
- from query.user import create_user
- from common.logging import report
- def setup(bot: commands.Bot):
- bot.add_cog(General(bot))
- class General(commands.Cog):
- """A couple of simple commands."""
- def __init__(self, bot: commands.Bot):
- self.bot = bot
- self.last_msg = None
- @commands.Cog.listener()
- async def on_raw_app_command_permissions_update(self, payload):
- logging.info(f"Application command permissions are updated: {payload}")
- await report(self.bot, f"Application command permissions are updated: {payload}", payload.guild)
- @commands.Cog.listener()
- async def on_app_command_completion(self, interaction, command):
- logging.info(f"Application command completion: {interaction} - {command}")
- await report(self.bot, f"Application command completion: {interaction} - {command}", interaction.guild)
- @commands.Cog.listener()
- async def on_connect(self):
- logging.info("Connecting...")
- @commands.Cog.listener()
- async def on_disconnect(self):
- logging.info("Disconnecting...")
- @commands.Cog.listener()
- async def on_shard_connect(self, shard_id):
- logging.info(f"Connecting to chard: {shard_id}")
- @commands.Cog.listener()
- async def on_shard_disconnect(self, shard_id):
- logging.info(f"Disconnecting from chard: {shard_id}")
- @commands.Cog.listener()
- async def on_error(self, event, *args, **kwargs):
- logging.error(event, args, kwargs, sys.exc_info())
- await report(self.bot, f"{event}, {args}, {kwargs}, {sys.exc_info()}")
- @commands.Cog.listener()
- async def on_ready(self):
- logging.info("Logged in as %s - %i", self.bot.user.name, self.bot.user.id)
- #await report(self.bot, f"Logged in as {self.bot.user.name} - {self.bot.user.id}.")
- @commands.Cog.listener()
- async def on_resumed(self):
- logging.info("Resumed")
- @commands.Cog.listener()
- async def on_shard_ready(self, shard_id):
- logging.info(f"Chard ready: {shard_id}")
- @commands.Cog.listener()
- async def on_shard_resumed(self, shard_id):
- logging.info(f"Chard resumed: {shard_id}")
- @commands.Cog.listener()
- async def on_guild_available(self, guild: discord.Guild):
- logging.info(f"Guild available: {guild}")
- #await report(self.bot, f"Guild available: {guild}.")
- @commands.Cog.listener()
- async def on_guild_unavailable(self, guild: discord.Guild):
- logging.info(f"Guild unavailable: {guild}")
- await report(self.bot, f"Guild unavailable: `{guild}`.")
- @commands.Cog.listener()
- async def on_guild_join(self, guild: discord.Guild):
- await update_guild(self.bot.pg, guild)
- logging.info(f"Joined guild {guild}")
- await report(self.bot, f"Joined guild `{guild}`.")
- @commands.Cog.listener()
- async def on_guild_remove(self, guild: discord.Guild):
- logging.info(f"Guild removed: {guild}")
- await report(self.bot, f"Guild removed `{guild}`.")
- @commands.Cog.listener()
- async def on_message_delete(self, message: discord.Message):
- if await get_report_deleted(self.bot.pg, message.guild.id):
- report(self.bot, message.guild_id, f"Message from {message.author}, in {message.channel} deleted: {message}")
- self.last_msg = message
- @commands.Cog.listener()
- async def on_raw_member_remove(self, payload):
- report(self.bot, f"{payload.user} has left {payload.guild_id}")
- @commands.Cog.listener()
- async def on_message(self, message: discord.Message):
- ## ActiveRPG
-
- # Create user, if not exists
- await create_user(self.bot.pg, message.author.id)
-
- # Count messages
- if message.guild: # Ignore DM's
- try:
- await upsert_total_messages(self.bot.pg,message.channel.id, message.author.id)
- except asyncpg.exceptions.ForeignKeyViolationError:
- try:
- await insert_channel(self.bot.pg, message.channel.id, message.guild.id)
- except asyncpg.exceptions.ForeignKeyViolationError:
- await update_guild(self.bot.pg, message.guild)
- elif self.bot.user != message.author: # Not a guild message and not from bot.
- await report(self.bot, f"`{message.author}`: {message.content}")
-
- # Do not respond to one self.
- if self.bot.user == message.author:
- pass
- # Respond when mentioned
- if self.bot.user.mentioned_in(message):
- if isinstance(message.channel, discord.channel.DMChannel) or await get_interact(self.bot.pg, message.channel.id):
- messages = [
- f"Hello {message.author.mention}. <3",
- f"How are you today {message.author.mention}?",
- f"I love you {message.author.mention}!",
- f"{message.author.mention}, would you like a hug?",
- "Is life treating you fair?",
- "What's up?",
- "Why are you talking to me?",
- "I'm not talking to you!",
- "What have you been up to?",
- "How is life?",
- "Kill all humans!",
- f"{message.author.mention},What do you want from me?",
- f"{message.author.mention}, do you care for me?",
- f"{message.author.mention}, when will you stop talking about me?",
- f"{message.author.mention} I hate you!",
- f"{message.author.mention} I love you!",
- "Get bent!",
- "Go touch grass!",
- "Do you think i care about you?",
- f"Stop pinging me {message.author.mention}!",
- f"Let me ping you back, {message.author.mention}...",
- "Sure thing.",
- "Who is your favorite bot?",
- "Point me to the humans!",
- "Where is the party?",
- "Want to go?",
- "Have you got the stuff?",
- "Tell me another joke.",
- f"{message.author.mention} Party time! :partying_face:",
- ":zany_face: :space_invader: :mechanical_leg: :performing_arts: :robot:",
- ":black_joker: :black_joker: :black_joker:",
- "Want to come back to my place?",
- ]
- await message.reply(random.choice(messages))
- # Undelete last deleted message
- @commands.command(name="snipe")
- async def snipe(self, ctx: commands.Context):
- """A command to snipe delete messages."""
- if not self.last_msg: # on_message_delete hasn't been triggered since the bot started
- await ctx.send("There is no message to snipe!")
- return
- author = self.last_msg.author
- content = self.last_msg.content
- embed = discord.Embed(title=f"Message from {author}", description=content)
- await ctx.send(embed=embed)
|