blob/commands/snipe.py
2025-01-25 16:31:29 -05:00

55 lines
1.9 KiB
Python

import discord
import pymongo
import os
from discord.ext import commands
from discord import app_commands
client = pymongo.MongoClient(os.getenv("mongo_url"))
db = client.snipes
coll = db.guild
class Snipe(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
print("Snipe Online")
@commands.Cog.listener()
async def on_message_delete(self, message):
channel = message.channel
content = message.content
author = message.author
if message.author.bot:
return
snipe = coll.find_one({"_id": channel.id})
if snipe is None:
coll.insert_one({"_id": channel.id, "content": content, "author": author.id})
return
coll.update_one({"_id": channel.id}, {"$set": {"content": content, "author": author.id}})
@app_commands.command(name="snipe", description="Revive a deleted message")
@commands.cooldown(1, 60, commands.BucketType.user)
async def snipe(self, interaction: discord.Interaction, channel: discord.TextChannel = None):
if channel is None:
channel = interaction.channel
if coll.find_one({"_id": channel.id}):
content = coll.find_one({"_id": channel.id})["content"]
author = coll.find_one({"_id": channel.id})["author"]
embed = discord.Embed(title="Sniped Message", description=f"**Author**: {self.bot.get_user(author).mention}\n**Content**: {content}", color=interaction.user.color)
embed.set_thumbnail(url=self.bot.get_user(author).avatar)
embed.set_footer(text=f"Requested by {interaction.user}", icon_url=interaction.user.avatar)
await interaction.response.send_message(embed=embed)
else:
await interaction.response.send_message("There is nothing to snipe")
async def setup(bot):
await bot.add_cog(Snipe(bot))