-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpunishment_manager.py
More file actions
133 lines (113 loc) · 5.17 KB
/
Copy pathpunishment_manager.py
File metadata and controls
133 lines (113 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from datetime import datetime, timedelta
from typing import Optional, Dict
import discord
class PunishmentManager:
def __init__(self, db):
self.db = db
async def get_punishment(self, user_id: int, guild_id: int) -> Optional[Dict]:
"""Retorna informações de punição de um usuário"""
return await self.db.fetch_one(
"SELECT * FROM punishments WHERE user_id = ? AND guild_id = ?",
(user_id, guild_id)
)
async def add_warning(self, user_id: int, guild_id: int, warned_by: int,
max_warnings: int = 3, ban_duration_hours: int = 24) -> tuple[int, bool]:
"""Adiciona um aviso ao usuário e retorna (total_avisos, foi_banido)"""
punishment = await self.get_punishment(user_id, guild_id)
if not punishment:
await self.db.execute(
"""INSERT INTO punishments (guild_id, user_id, warnings, banned_by)
VALUES (?, ?, 1, ?)""",
(guild_id, user_id, warned_by)
)
return 1, False
new_warnings = punishment['warnings'] + 1
if new_warnings >= max_warnings:
ban_until = datetime.utcnow() + timedelta(hours=ban_duration_hours)
await self.db.execute(
"""UPDATE punishments
SET warnings = ?, banned_until = ?, reason = ?, banned_by = ?
WHERE user_id = ? AND guild_id = ?""",
(new_warnings, ban_until, "Excesso de avisos", warned_by, user_id, guild_id)
)
return new_warnings, True
else:
await self.db.execute(
"""UPDATE punishments SET warnings = ?, banned_by = ?
WHERE user_id = ? AND guild_id = ?""",
(new_warnings, warned_by, user_id, guild_id)
)
return new_warnings, False
async def ban_user(self, user_id: int, guild_id: int, duration_hours: int,
reason: str, banned_by: int):
"""Bane um usuário temporariamente"""
ban_until = datetime.utcnow() + timedelta(hours=duration_hours)
await self.db.execute(
"""INSERT INTO punishments
(guild_id, user_id, banned_until, reason, banned_by)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(user_id, guild_id) DO UPDATE SET
banned_until = ?, reason = ?, banned_by = ?""",
(guild_id, user_id, ban_until, reason, banned_by,
ban_until, reason, banned_by)
)
async def unban_user(self, user_id: int, guild_id: int):
"""Remove o banimento de um usuário"""
await self.db.execute(
"""UPDATE punishments
SET banned_until = NULL, reason = NULL
WHERE user_id = ? AND guild_id = ?""",
(user_id, guild_id)
)
async def is_banned(self, user_id: int, guild_id: int) -> tuple[bool, Optional[str]]:
"""Verifica se um usuário está banido e retorna (está_banido, motivo)"""
punishment = await self.get_punishment(user_id, guild_id)
if not punishment or not punishment['banned_until']:
return False, None
ban_until = datetime.fromisoformat(punishment['banned_until'])
if datetime.utcnow() < ban_until:
return True, punishment['reason']
else:
await self.unban_user(user_id, guild_id)
return False, None
async def clear_warnings(self, user_id: int, guild_id: int):
"""Limpa os avisos de um usuário"""
await self.db.execute(
"""UPDATE punishments SET warnings = 0
WHERE user_id = ? AND guild_id = ?""",
(user_id, guild_id)
)
def format_punishment_embed(self, user: discord.Member, punishment: Dict,
color: int) -> discord.Embed:
"""Formata um embed com informações de punição"""
embed = discord.Embed(
title=f"⚠️ Punições de {user.display_name}",
color=color,
timestamp=datetime.utcnow()
)
embed.set_thumbnail(url=user.display_avatar.url)
embed.add_field(
name="Avisos",
value=f"`{punishment['warnings']}`",
inline=True
)
if punishment['banned_until']:
ban_until = datetime.fromisoformat(punishment['banned_until'])
embed.add_field(
name="Banido até",
value=f"<t:{int(ban_until.timestamp())}:F>",
inline=True
)
if punishment['reason']:
embed.add_field(
name="Motivo",
value=punishment['reason'],
inline=False
)
else:
embed.add_field(
name="Status",
value="✅ Sem banimento ativo",
inline=True
)
return embed