-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcounting.py
More file actions
769 lines (659 loc) · 30.5 KB
/
counting.py
File metadata and controls
769 lines (659 loc) · 30.5 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
import discord
from discord.ext import commands
from discord import app_commands
import aiosqlite
from utils.codebuddy_database import DB_PATH
from utils.codebuddy_database import (
add_guild_save_units,
get_guild_save_units,
get_user_save_units,
increment_quest_counting_count,
try_use_guild_save,
try_use_user_save,
)
import ast
import operator
import asyncio
import time
from typing import Optional
class Counting(commands.Cog):
def __init__(self, bot):
self.bot = bot
# Cache for counting channels: guild_id -> channel_id
self.counting_channels = {}
# Protect against occasional duplicate MESSAGE_CREATE dispatches or accidental double-processing.
# Key: message_id, Value: monotonic timestamp
self._recent_message_ids: dict[int, float] = {}
# Throttle reaction API calls to avoid Discord rate limits in fast counting channels.
self._reaction_queue: asyncio.Queue[tuple[discord.Message, str]] = asyncio.Queue()
self._pending_reactions: set[tuple[int, str]] = set()
self._reaction_worker_task: Optional[asyncio.Task[None]] = None
async def cog_unload(self) -> None:
if self._reaction_worker_task and not self._reaction_worker_task.done():
self._reaction_worker_task.cancel()
async def _reaction_worker(self) -> None:
# A small delay between reaction requests keeps us under the common reaction route limits.
# Reactions may appear slightly delayed, but they will still be added.
while True:
message, emoji = await self._reaction_queue.get()
try:
try:
await message.add_reaction(emoji)
except Exception:
pass
await asyncio.sleep(0.35)
finally:
self._pending_reactions.discard((message.id, emoji))
self._reaction_queue.task_done()
def _enqueue_reaction(self, message: discord.Message, emoji: str) -> None:
key = (message.id, emoji)
if key in self._pending_reactions:
return
self._pending_reactions.add(key)
try:
self._reaction_queue.put_nowait((message, emoji))
except Exception:
self._pending_reactions.discard(key)
async def cog_load(self):
"""Load counting channels into memory on startup"""
if self._reaction_worker_task is None or self._reaction_worker_task.done():
self._reaction_worker_task = asyncio.create_task(self._reaction_worker())
try:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
# Ensure auxiliary tables exist
await db.execute("""
CREATE TABLE IF NOT EXISTS counting_warnings (
guild_id INTEGER,
user_id INTEGER,
warnings INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (guild_id, user_id)
)
""")
await db.execute("""
CREATE TABLE IF NOT EXISTS counting_active_highscore (
guild_id INTEGER PRIMARY KEY,
message_id INTEGER
)
""")
await db.execute("""
CREATE TABLE IF NOT EXISTS counting_highscore_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id INTEGER,
score INTEGER NOT NULL,
user_id INTEGER,
message_id INTEGER,
timestamp INTEGER
)
""")
await db.commit()
try:
async with db.execute("SELECT guild_id, channel_id FROM counting_config") as cursor:
rows = await cursor.fetchall()
for guild_id, channel_id in rows:
self.counting_channels[guild_id] = channel_id
print(f"Loaded {len(self.counting_channels)} counting channels")
except aiosqlite.OperationalError:
print("counting_config table not found during cog load (likely first run)")
except Exception as e:
print(f"Error loading counting channels: {e}")
async def _get_warning_count(self, guild_id: int, user_id: int) -> int:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
async with db.execute(
"SELECT warnings FROM counting_warnings WHERE guild_id = ? AND user_id = ?",
(guild_id, user_id),
) as cursor:
row = await cursor.fetchone()
return int(row[0]) if row else 0
async def _set_warning_count(self, guild_id: int, user_id: int, warnings: int) -> None:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
if warnings <= 0:
await db.execute(
"DELETE FROM counting_warnings WHERE guild_id = ? AND user_id = ?",
(guild_id, user_id),
)
else:
await db.execute(
"""
INSERT INTO counting_warnings (guild_id, user_id, warnings)
VALUES (?, ?, ?)
ON CONFLICT(guild_id, user_id) DO UPDATE SET warnings = excluded.warnings
""",
(guild_id, user_id, warnings),
)
await db.commit()
async def _clear_all_warnings(self, guild_id: int) -> None:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
await db.execute("DELETE FROM counting_warnings WHERE guild_id = ?", (guild_id,))
await db.commit()
async def _get_active_highscore_message_id(self, guild_id: int) -> Optional[int]:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
async with db.execute(
"SELECT message_id FROM counting_active_highscore WHERE guild_id = ?",
(guild_id,),
) as cursor:
row = await cursor.fetchone()
return int(row[0]) if row and row[0] is not None else None
async def _set_active_highscore_message_id(self, guild_id: int, message_id: Optional[int]) -> None:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
if message_id is None:
await db.execute("DELETE FROM counting_active_highscore WHERE guild_id = ?", (guild_id,))
else:
await db.execute(
"""
INSERT INTO counting_active_highscore (guild_id, message_id)
VALUES (?, ?)
ON CONFLICT(guild_id) DO UPDATE SET message_id = excluded.message_id
""",
(guild_id, message_id),
)
await db.commit()
async def _remove_bot_reactions(self, channel: discord.TextChannel, message_id: int) -> None:
try:
msg = await channel.fetch_message(message_id)
except Exception:
return
if not self.bot.user:
return
try:
await msg.remove_reaction("✅", self.bot.user)
except Exception:
pass
try:
await msg.remove_reaction("🏆", self.bot.user)
except Exception:
pass
async def _mark_highscore_message(
self,
message: discord.Message,
new_count: int,
previous_high_score: int,
) -> None:
"""Add ✅+🏆 to the message.
Note: Reactions, once added by the bot, should never be removed.
"""
if not message.guild or not isinstance(message.channel, discord.TextChannel):
return
guild_id = message.guild.id
channel = message.channel
# Only add the trophy here.
# The ✅ reaction is added for all valid counts in the main handler;
# adding it again here causes extra API calls and rate limits.
self._enqueue_reaction(message, "🏆")
# Track the latest highscore/tie message ID for bookkeeping.
# (We no longer remove reactions from older messages.)
await self._set_active_highscore_message_id(guild_id, message.id)
# Record history only if it is a NEW record
if new_count > previous_high_score:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
await db.execute(
"""
INSERT INTO counting_highscore_history (guild_id, score, user_id, message_id, timestamp)
VALUES (?, ?, ?, ?, ?)
""",
(guild_id, new_count, message.author.id, message.id, int(time.time())),
)
await db.commit()
async def _clear_highscore_marker_if_any(self, guild_id: int, channel: discord.TextChannel) -> None:
marker_id = await self._get_active_highscore_message_id(guild_id)
if marker_id:
await self._set_active_highscore_message_id(guild_id, None)
@app_commands.command(name="setcountingchannel", description="Set the channel for the counting game")
@app_commands.checks.has_permissions(administrator=True)
async def setcountingchannel(self, interaction: discord.Interaction, channel: discord.TextChannel):
# Slash command interactions must be acknowledged quickly.
# DB operations can take >3s (locks, slow disks), so defer immediately.
if interaction.response.is_done():
# Extremely defensive; normally false here.
pass
else:
await interaction.response.defer(ephemeral=True)
if interaction.guild_id is None:
await interaction.followup.send("This command can only be used in a server.", ephemeral=True)
return
retries = 3
while retries > 0:
try:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
await db.execute(
"""
INSERT INTO counting_config (guild_id, channel_id)
VALUES (?, ?)
ON CONFLICT(guild_id) DO UPDATE SET channel_id = excluded.channel_id
""",
(interaction.guild_id, channel.id),
)
await db.commit()
break
except aiosqlite.OperationalError as e:
if "locked" in str(e).lower():
retries -= 1
await asyncio.sleep(0.5)
continue
raise
# Update cache
self.counting_channels[interaction.guild_id] = channel.id
await interaction.followup.send(f"Counting channel set to {channel.mention}", ephemeral=True)
def safe_eval(self, expr):
operators = {
ast.Add: operator.add,
ast.Sub: operator.sub,
ast.Mult: operator.mul,
ast.Div: operator.truediv,
ast.Pow: operator.pow,
ast.BitXor: operator.pow, # Allow ^ for power
ast.USub: operator.neg
}
def eval_node(node):
if isinstance(node, ast.Constant):
if isinstance(node.value, (int, float)):
return node.value
raise TypeError("Not a number")
elif isinstance(node, ast.BinOp):
op = type(node.op)
if op in operators:
left = eval_node(node.left)
right = eval_node(node.right)
if op in (ast.Pow, ast.BitXor):
if right > 100: # Limit exponent
raise ValueError("Exponent too large")
return operators[op](left, right)
elif isinstance(node, ast.UnaryOp):
op = type(node.op)
if op in operators:
return operators[op](eval_node(node.operand))
raise TypeError("Unsupported type")
try:
tree = ast.parse(expr, mode='eval')
return eval_node(tree.body)
except Exception:
return None
def _extract_leading_expression(self, text: str) -> Optional[str]:
"""Extract a safe-eval compatible expression from the start of a message.
This allows messages like "43 is next" or "6*7 nice" to count as 43/42.
Only a leading run of digits/operators/parentheses is considered.
"""
if not text:
return None
# Normalize whitespace/newlines.
s = text.replace("\n", " ").strip()
if not s:
return None
# Allow common markdown wrappers before the number (e.g. `43`, **43**).
s = s.lstrip()
while s and s[0] in "`*_~":
s = s[1:].lstrip()
allowed = set("0123456789+-*/^(). ")
expr_chars: list[str] = []
for ch in s:
if ch in allowed:
expr_chars.append(ch)
else:
break
expr = "".join(expr_chars).strip()
return expr or None
def _parse_count_number(self, message_content: str) -> Optional[int]:
expr = self._extract_leading_expression(message_content)
if not expr:
return None
number = self.safe_eval(expr)
if number is None:
return None
if isinstance(number, float):
if number.is_integer():
return int(number)
return None
if isinstance(number, int):
return number
return None
@commands.Cog.listener()
async def on_message(self, message):
if message.author.bot or not message.guild:
return
# 1. OPTIMIZATION: Check cache first before touching DB
if message.guild.id not in self.counting_channels:
return
if message.channel.id != self.counting_channels[message.guild.id]:
return
# Deduplicate processing of the same message ID within this process.
# This prevents duplicate warnings/messages if Discord or the bot dispatches the event twice.
now = time.monotonic()
last_seen = self._recent_message_ids.get(message.id)
if last_seen is not None and (now - last_seen) < 30:
return
self._recent_message_ids[message.id] = now
if len(self._recent_message_ids) > 5000:
cutoff = now - 120
self._recent_message_ids = {mid: ts for mid, ts in self._recent_message_ids.items() if ts >= cutoff}
# 2. Process the message logic
# Wrap DB operations in retry loop for robustness
retries = 3
while retries > 0:
try:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
async with db.execute("SELECT current_count, last_user_id, high_score FROM counting_config WHERE guild_id = ?", (message.guild.id,)) as cursor:
config = await cursor.fetchone()
if not config:
# Should not happen if in cache, but possible if DB was manually cleared
return
current_count, last_user_id, high_score = config
# Try to parse the number
content = message.content.strip()
if not content:
return
# Evaluate math expression (supports extra trailing text like "43 is next")
number = self._parse_count_number(content)
if number is None:
return # Not a valid number/expression at the start
# Check rules
next_count = current_count + 1
if number != next_count:
await self.fail_count(message, current_count, "Wrong number!")
return
if message.author.id == last_user_id:
# Warn instead of instant ruin. 3 warnings ruins the count.
# Use an atomic increment in the SAME connection to avoid races and DB-lock retries.
await db.execute(
"""
INSERT INTO counting_warnings (guild_id, user_id, warnings)
VALUES (?, ?, 1)
ON CONFLICT(guild_id, user_id) DO UPDATE SET warnings = warnings + 1
""",
(message.guild.id, message.author.id),
)
async with db.execute(
"SELECT warnings FROM counting_warnings WHERE guild_id = ? AND user_id = ?",
(message.guild.id, message.author.id),
) as cursor:
row = await cursor.fetchone()
warnings = int(row[0]) if row else 1
await db.commit()
if warnings >= 3:
await self.fail_count(message, current_count, "Too many warnings (counted twice in a row 3 times)!")
return
self._enqueue_reaction(message, "⚠️")
await message.channel.send(
f"You can't count twice in a row, {message.author.mention}. "
f"You have **{warnings}/3** warnings.",
delete_after=12,
)
return
# Valid count - Update DB
new_high_score = max(high_score, next_count)
# Update configuration tables
await db.execute("""
UPDATE counting_config
SET current_count = ?, last_user_id = ?, high_score = ?
WHERE guild_id = ?
""", (next_count, message.author.id, new_high_score, message.guild.id))
# Update user stats
await db.execute("""
INSERT INTO counting_stats (user_id, guild_id, total_counts, ruined_counts)
VALUES (?, ?, 1, 0)
ON CONFLICT(user_id, guild_id) DO UPDATE SET total_counts = total_counts + 1
""", (message.author.id, message.guild.id))
# Reset warnings for this user on a valid count (in the same transaction).
await db.execute(
"DELETE FROM counting_warnings WHERE guild_id = ? AND user_id = ?",
(message.guild.id, message.author.id),
)
await db.commit()
# Side effects after commit to avoid duplicate reactions on retries.
self._enqueue_reaction(message, "✅")
# Daily quest progress: count 5 numbers (best-effort).
try:
quest_completed = await increment_quest_counting_count(message.author.id)
if quest_completed:
await message.channel.send(
f"Daily quest completed, {message.author.mention}! "
"You earned **0.2** Streak Freeze and **0.5** Save. "
"Use `?inventory` to check your items.",
delete_after=15,
)
except Exception:
pass
# Highscore marker: react ✅+🏆 when reaching/topping the record
if next_count >= high_score:
await self._mark_highscore_message(message, next_count, high_score)
return # Success
except aiosqlite.OperationalError as e:
# If specifically locked, retry
if "locked" in str(e):
retries -= 1
if retries == 0:
print(f"Database locked repeatedly in counting for msg {message.id}")
# Don't crash bot, just ignore or log
return
await asyncio.sleep(0.1 * (4 - retries)) # backoff
else:
raise # Re-raise other operational errors
@commands.Cog.listener()
async def on_message_delete(self, message: discord.Message):
"""Announce when someone deletes a counting number message."""
if message.author.bot or not message.guild:
return
if message.guild.id not in self.counting_channels:
return
if message.channel.id != self.counting_channels[message.guild.id]:
return
content = (message.content or "").strip()
if not content:
return
number = self._parse_count_number(content)
if number is None:
return
await message.channel.send(
f"{message.author.mention} deleted a number **{number}**.",
allowed_mentions=discord.AllowedMentions(users=True),
)
async def fail_count(self, message, current_count, reason):
# Replace dice mechanic with save mechanic:
# 1) Use a personal save if available.
# 2) Else use a guild save if available.
# 3) Else the count is ruined (reset to 0).
try:
await message.add_reaction("❌")
except Exception:
pass
if not message.guild:
return
guild_id = message.guild.id
user_id = message.author.id
# Clear this user's warnings so a saved mistake doesn't soft-lock them.
try:
await self._set_warning_count(guild_id, user_id, 0)
except Exception:
pass
used_personal = False
used_guild = False
try:
used_personal = await try_use_user_save(user_id)
except Exception:
used_personal = False
if not used_personal:
try:
used_guild = await try_use_guild_save(guild_id)
except Exception:
used_guild = False
if used_personal or used_guild:
try:
remaining_user_units = await get_user_save_units(user_id)
except Exception:
remaining_user_units = 0
try:
remaining_guild_units = await get_guild_save_units(guild_id)
except Exception:
remaining_guild_units = 0
source = "your" if used_personal else "the server's"
await message.channel.send(
f"{reason} {message.author.mention} messed up at **{current_count}**, "
f"but {source} save was used — the count is **saved**.\n"
f"Next number is **{current_count + 1}**.\n"
f"Your saves: **{remaining_user_units/10:.1f}** • Server saves: **{remaining_guild_units/10:.1f}**"
)
return
# No saves: ruin the count (reset to 0)
db_ops = [
(
"""
UPDATE counting_config
SET current_count = 0, last_user_id = NULL
WHERE guild_id = ?
""",
(guild_id,),
),
(
"""
INSERT INTO counting_stats (user_id, guild_id, total_counts, ruined_counts)
VALUES (?, ?, 0, 1)
ON CONFLICT(user_id, guild_id) DO UPDATE SET ruined_counts = ruined_counts + 1
""",
(user_id, guild_id),
),
]
retries = 3
while retries > 0:
try:
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
for sql, args in db_ops:
await db.execute(sql, args)
await db.commit()
break
except aiosqlite.OperationalError as e:
if "locked" in str(e).lower():
retries -= 1
await asyncio.sleep(0.3)
continue
break
await message.channel.send(
f"{reason} {message.author.mention} messed up at **{current_count}**. "
"No saves were available — the count is **ruined** and has been reset to **0**.\n"
"Next number is **1**."
)
if isinstance(message.channel, discord.TextChannel):
await self._clear_all_warnings(guild_id)
await self._clear_highscore_marker_if_any(guild_id, message.channel)
@commands.command(name="donateguild", aliases=["dg"])
async def donate_guild(self, ctx: commands.Context):
"""Donate 1 personal save to the guild pool (guild receives 0.5 save)."""
if not ctx.guild:
return await ctx.send("Server only command.")
user_id = ctx.author.id
guild_id = ctx.guild.id
# Need at least 1.0 save (10 units) to donate.
user_units = await get_user_save_units(user_id)
if user_units < 10:
return await ctx.send(
f"You need **1.0** save to donate. Your saves: **{user_units/10:.1f}**"
)
# Consume 1.0 personal save
used = await try_use_user_save(user_id)
if not used:
return await ctx.send("Couldn't donate right now (try again).")
# Guild receives 0.5 save (5 units)
await add_guild_save_units(guild_id, 5)
new_user_units = await get_user_save_units(user_id)
new_guild_units = await get_guild_save_units(guild_id)
await ctx.send(
f"Donated **1.0** save to the server pool. Server gained **0.5** save.\n"
f"Your saves: **{new_user_units/10:.1f}** • Server saves: **{new_guild_units/10:.1f}**"
)
@commands.command(name="guildsaves", aliases=["gsaves", "serversaves", "ssaves"])
async def guild_saves(self, ctx: commands.Context):
"""Show the server save pool used to protect counting mistakes."""
if not ctx.guild:
return await ctx.send("Server only command.")
units = await get_guild_save_units(ctx.guild.id)
await ctx.send(
f"Server saves: **{units/10:.1f}**\n"
"(Needs **1.0** server save to protect a ruined count.)"
)
@commands.hybrid_command(name="highscoretable", aliases=["highscores"], help="Show recent counting highscores")
async def highscore_table(self, ctx: commands.Context):
if not ctx.guild:
return await ctx.send("Server only command.")
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
async with db.execute(
"""
SELECT score, user_id, timestamp
FROM counting_highscore_history
WHERE guild_id = ?
ORDER BY score DESC
LIMIT 10
""",
(ctx.guild.id,),
) as cursor:
rows = await cursor.fetchall()
async with db.execute(
"SELECT current_count, high_score FROM counting_config WHERE guild_id = ?",
(ctx.guild.id,),
) as cursor:
config = await cursor.fetchone()
current = config[0] if config else 0
high = config[1] if config else 0
embed = discord.Embed(title="Counting Highscores", color=discord.Color.gold())
embed.add_field(name="Current Count", value=str(current), inline=True)
embed.add_field(name="All-Time High", value=str(high), inline=True)
if not rows:
embed.description = "No highscore history yet."
return await ctx.send(embed=embed)
lines = []
for i, (score, user_id, ts) in enumerate(rows, 1):
when = f"<t:{int(ts)}:R>" if ts else ""
lines.append(f"{i}. **{score}** by <@{user_id}> {when}")
embed.description = "\n".join(lines)
await ctx.send(embed=embed)
@commands.command(name="mcl", aliases=["tc"])
async def most_count_leaderboard(self, ctx):
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
async with db.execute("""
SELECT user_id, total_counts
FROM counting_stats
WHERE guild_id = ?
ORDER BY total_counts DESC
LIMIT 10
""", (ctx.guild.id,)) as cursor:
rows = await cursor.fetchall()
if not rows:
await ctx.send("No counting stats yet.")
return
embed = discord.Embed(title="Most Count Leaderboard", color=discord.Color.blue())
description = ""
for i, (user_id, count) in enumerate(rows, 1):
description += f"{i}. <@{user_id}>: {count}\n"
embed.description = description
await ctx.send(embed=embed)
@commands.command(name="mrl")
async def most_ruined_leaderboard(self, ctx):
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
async with db.execute("""
SELECT user_id, ruined_counts
FROM counting_stats
WHERE guild_id = ?
ORDER BY ruined_counts DESC
LIMIT 10
""", (ctx.guild.id,)) as cursor:
rows = await cursor.fetchall()
if not rows:
await ctx.send("No ruined stats yet.")
return
embed = discord.Embed(title="Most Ruined Leaderboard", color=discord.Color.red())
description = ""
for i, (user_id, count) in enumerate(rows, 1):
description += f"{i}. <@{user_id}>: {count}\n"
embed.description = description
await ctx.send(embed=embed)
@commands.command(name="scs")
async def server_count_stats(self, ctx):
async with aiosqlite.connect(DB_PATH, timeout=30.0) as db:
async with db.execute("SELECT current_count, high_score FROM counting_config WHERE guild_id = ?", (ctx.guild.id,)) as cursor:
row = await cursor.fetchone()
if not row:
await ctx.send("Counting channel not set up or no data.")
return
current, high = row
embed = discord.Embed(title="Server Count Stats", color=discord.Color.green())
embed.add_field(name="Current Count", value=str(current))
embed.add_field(name="High Score", value=str(high))
await ctx.send(embed=embed)
async def setup(bot):
await bot.add_cog(Counting(bot))