mirror of
https://github.com/Lilac-Rose/Lacie.git
synced 2026-08-24 09:44:53 -05:00
fixed lock command and added ping protect for me cause i can
This commit is contained in:
62
events/ping_protect.py
Normal file
62
events/ping_protect.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import sqlite3
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from pathlib import Path
|
||||
|
||||
# The user ID to protect from pings
|
||||
PROTECTED_USER_ID = 252130669919076352
|
||||
|
||||
# Users allowed to ping the protected user
|
||||
ALLOWED_PINGERS = {
|
||||
505390548232699906,
|
||||
771709136051372032,
|
||||
692030310644187206,
|
||||
1153235432813895730,
|
||||
}
|
||||
|
||||
DB_PATH = Path(__file__).parent.parent / "data" / "ping_protect.db"
|
||||
|
||||
def get_db():
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS ping_counts (
|
||||
user_id INTEGER PRIMARY KEY,
|
||||
count INTEGER DEFAULT 0
|
||||
)
|
||||
""")
|
||||
conn.commit()
|
||||
return conn
|
||||
|
||||
def record_ping(user_id: int):
|
||||
conn = get_db()
|
||||
conn.execute("""
|
||||
INSERT INTO ping_counts (user_id, count) VALUES (?, 1)
|
||||
ON CONFLICT(user_id) DO UPDATE SET count = count + 1
|
||||
""", (user_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
class PingProtectListener(commands.Cog):
|
||||
"""Responds when the protected user is pinged by someone not on the allowlist."""
|
||||
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_message(self, message: discord.Message):
|
||||
if message.author.bot:
|
||||
return
|
||||
|
||||
# Check if the protected user is mentioned
|
||||
if not any(user.id == PROTECTED_USER_ID for user in message.mentions):
|
||||
return
|
||||
|
||||
# Allow users on the allowlist (don't track them)
|
||||
if message.author.id in ALLOWED_PINGERS:
|
||||
return
|
||||
|
||||
record_ping(message.author.id)
|
||||
await message.reply("Please don't ping faer", mention_author=False)
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(PingProtectListener(bot))
|
||||
@@ -261,23 +261,13 @@ class LockCog(commands.Cog):
|
||||
await ctx.message.add_reaction("❌")
|
||||
return await ctx.send("This channel wasn't locked with !lock, so I can't restore its permissions.", allowed_mentions=discord.AllowedMentions.none())
|
||||
|
||||
# Clear all current overwrites
|
||||
# Restore all original overwrites in a single API call
|
||||
failed_targets = []
|
||||
for target in list(channel.overwrites.keys()):
|
||||
try:
|
||||
await channel.set_permissions(target, overwrite=None)
|
||||
except discord.Forbidden:
|
||||
failed_targets.append(target.name)
|
||||
|
||||
# Restore all original overwrites
|
||||
restored_count = 0
|
||||
for target, overwrite in stored_overwrites.items():
|
||||
try:
|
||||
await channel.set_permissions(target, overwrite=overwrite, reason=f"Channel unlocked by {ctx.author}")
|
||||
restored_count += 1
|
||||
except discord.Forbidden:
|
||||
failed_targets.append(target.name if hasattr(target, 'name') else str(target.id))
|
||||
|
||||
try:
|
||||
await channel.edit(overwrites=stored_overwrites, reason=f"Channel unlocked by {ctx.author}")
|
||||
except discord.Forbidden:
|
||||
failed_targets.append("(permission denied restoring overwrites)")
|
||||
|
||||
# Remove from database (runs in thread pool)
|
||||
await self.remove_stored_permissions(channel.id)
|
||||
|
||||
@@ -288,9 +278,7 @@ class LockCog(commands.Cog):
|
||||
try:
|
||||
msg = f"#{channel.name} has been unlocked!"
|
||||
if failed_targets:
|
||||
msg += f"\nNote: Could not restore permissions for: {', '.join(failed_targets[:5])}"
|
||||
if len(failed_targets) > 5:
|
||||
msg += f" and {len(failed_targets) - 5} more"
|
||||
msg += f"\nNote: Could not restore some permissions (missing permissions)."
|
||||
await ctx.send(msg, allowed_mentions=discord.AllowedMentions.none())
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user