Hilmany / cogs /reminders.py
HilmanBey's picture
Upload 64 files
cbb05ec verified
Raw
History Blame Contribute Delete
5.65 kB
import discord
from discord.ext import commands, tasks
from discord import app_commands
from datetime import datetime
from database import Database
import re
CYBER_BLUE = discord.Color.from_rgb(52, 152, 219)
CYBER_GREEN = discord.Color.from_rgb(46, 204, 113)
CYBER_ORANGE = discord.Color.from_rgb(230, 126, 34)
def parse_reminder_duration(duration_str: str):
"""Süre: '10s', '5m', '2h', '1d', '1w' -> datetime (şimdi + delta)"""
duration_str = duration_str.lower().strip()
multipliers = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400, 'w': 604800}
match = re.match(r"^(\d+)([smhdw])$", duration_str)
if not match:
return None
value = int(match.group(1))
unit = match.group(2)
from datetime import timedelta
return datetime.now() + timedelta(seconds=value * multipliers[unit])
class Reminders(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.reminder_loop.start()
def cog_unload(self):
self.reminder_loop.cancel()
@tasks.loop(seconds=5)
async def reminder_loop(self):
try:
now = datetime.now()
records = await Database.fetch_all(
"SELECT * FROM hatirlaticilar WHERE bitis_zamani <= ?",
(now.isoformat(),)
)
for r in records:
guild = self.bot.get_guild(r['guild_id'])
if not guild:
await Database.execute("DELETE FROM hatirlaticilar WHERE id = ?", (r['id'],))
continue
channel = guild.get_channel(r['kanal_id'])
if not channel:
await Database.execute("DELETE FROM hatirlaticilar WHERE id = ?", (r['id'],))
continue
member = guild.get_member(r['user_id'])
mention = member.mention if member else f"<@{r['user_id']}>"
embed = discord.Embed(
title="⏰ Hatırlatıcı!",
description=f"Merhaba {mention}! Hatırlatmanız geldi:\n\n**{r['mesaj']}**",
color=CYBER_ORANGE,
timestamp=datetime.now()
)
embed.set_footer(text="ArazAI Hatırlatıcı Sistemi")
try:
await channel.send(content=mention, embed=embed)
except:
pass
await Database.execute("DELETE FROM hatirlaticilar WHERE id = ?", (r['id'],))
except Exception as e:
print(f"Reminder loop hatası: {e}")
@reminder_loop.before_loop
async def before_reminder_loop(self):
await self.bot.wait_until_ready()
@commands.hybrid_command(name="hatırlat", description="Size belirli bir süre sonra hatırlatma gönderir.")
@app_commands.describe(
sure="Süre (örn: 10s, 5m, 2h, 1d, 1w)",
mesaj="Hatırlatıcı mesajı"
)
async def hatirlat(self, ctx: commands.Context, sure: str, *, mesaj: str):
if ctx.interaction:
await ctx.defer()
bitis = parse_reminder_duration(sure)
if not bitis:
await ctx.send("❌ Geçersiz süre. Örnek: `10s`, `5m`, `2h`, `1d`, `1w`", ephemeral=True)
return
await Database.execute(
"INSERT INTO hatirlaticilar (guild_id, kanal_id, user_id, mesaj, bitis_zamani) VALUES (?, ?, ?, ?, ?)",
(ctx.guild.id, ctx.channel.id, ctx.author.id, mesaj, bitis.isoformat())
)
embed = discord.Embed(
title="✅ Hatırlatıcı Kuruldu!",
description=(
f"**Mesaj:** {mesaj}\n"
f"**Süre:** {sure}\n"
f"**Bitiş:** <t:{int(bitis.timestamp())}:F> (<t:{int(bitis.timestamp())}:R>)"
),
color=CYBER_GREEN,
timestamp=datetime.now()
)
await ctx.send(embed=embed)
@commands.hybrid_command(name="hatırlatıcılar", description="Aktif hatırlatıcılardan ilk 10 tanesini listeler.")
async def hatirlaticilar(self, ctx: commands.Context):
if ctx.interaction:
await ctx.defer()
records = await Database.fetch_all(
"SELECT * FROM hatirlaticilar WHERE guild_id = ? AND user_id = ? ORDER BY bitis_zamani ASC LIMIT 10",
(ctx.guild.id, ctx.author.id)
)
if not records:
await ctx.send("ℹ️ Aktif hatırlatıcınız bulunmuyor.")
return
embed = discord.Embed(title="⏰ Aktif Hatırlatıcılar", color=CYBER_ORANGE, timestamp=datetime.now())
desc = ""
for r in records:
bitis = datetime.fromisoformat(r['bitis_zamani'])
desc += f"• **#{r['id']}** | <t:{int(bitis.timestamp())}:R> | {r['mesaj'][:50]}\n"
embed.description = desc
embed.set_footer(text="Silmek için: /hatırlatıcı-sil <id>")
await ctx.send(embed=embed)
@commands.hybrid_command(name="hatırlatıcı-sil", description="Bir hatırlatıcıyı ID ile siler.")
@app_commands.describe(id="Hatırlatıcı ID")
async def hatirlatici_sil(self, ctx: commands.Context, id: int):
if ctx.interaction:
await ctx.defer()
rows = await Database.execute(
"DELETE FROM hatirlaticilar WHERE id = ? AND guild_id = ? AND user_id = ?",
(id, ctx.guild.id, ctx.author.id)
)
if rows > 0:
await ctx.send(f"✅ #{id} hatırlatıcısı silindi.")
else:
await ctx.send("❌ Hatırlatıcı bulunamadı veya size ait değil.", ephemeral=True)
async def setup(bot):
await bot.add_cog(Reminders(bot))