limit by UID and server (#1251)
This commit is contained in:
+25
-2
@@ -222,8 +222,12 @@ def higher_tier(a: Optional[str], b: Optional[str]) -> Optional[str]:
|
||||
if ra < 0 and rb < 0:
|
||||
return None
|
||||
return a if ra >= rb else b
|
||||
# Free-tier /comp cap per timeslot.
|
||||
COMP_LIMIT_PER_TIMESLOT: int = 15
|
||||
# Free-tier /comp caps per timeslot.
|
||||
# Server-wide cap counts every invocation in the guild during the window.
|
||||
# Per-user cap counts each user's invocations and is enforced in addition to
|
||||
# the server cap, so one user maxing out can't drain the rest of the server.
|
||||
COMP_LIMIT_PER_TIMESLOT: int = 25
|
||||
COMP_LIMIT_PER_USER_PER_TIMESLOT: int = 10
|
||||
|
||||
# ── SQB schedule (UTC, DST-immune) ───────────────────────────────────────────
|
||||
# Edit SQB_SLOTS_POSTED and the margin constants when Gaijin changes the
|
||||
@@ -1080,6 +1084,25 @@ async def get_comp_usage_in_timeslot(guild_id: int, since_ts: int) -> int:
|
||||
return 0
|
||||
|
||||
|
||||
async def get_comp_usage_in_timeslot_by_user(
|
||||
guild_id: int, user_id: int, since_ts: int
|
||||
) -> int:
|
||||
"""Count /comp invocations for a specific user in a guild since a timestamp."""
|
||||
try:
|
||||
async with aiosqlite.connect(COMMAND_DATA_DB_PATH, timeout=5.0) as db:
|
||||
await db.execute("PRAGMA busy_timeout=5000;")
|
||||
cur = await db.execute(
|
||||
"SELECT COUNT(*) FROM command_usage "
|
||||
"WHERE command_name='comp' AND guild_id=? AND user_id=? AND timestamp >= ?",
|
||||
(str(guild_id), str(user_id), since_ts),
|
||||
)
|
||||
row = await cur.fetchone()
|
||||
return row[0] if row else 0
|
||||
except Exception:
|
||||
logging.debug("get_comp_usage_in_timeslot_by_user query failed", exc_info=True)
|
||||
return 0
|
||||
|
||||
|
||||
def minutes_ago(unix_timestamp: int) -> str:
|
||||
"""Convert a unix timestamp to a human-readable 'X minutes ago' string."""
|
||||
if not unix_timestamp:
|
||||
|
||||
Reference in New Issue
Block a user