Count messages per user, then announce the week's top sender on Sunday after 09:00 UTC using the daily-gate pattern. The winner mention renders without pinging.
from datetime import datetime, timezone
@plugin.on_event("message_create")
def count_week(ctx: Context, event: dict):
if event.get("author_bot"):
return
ctx.kv.increment(f"msgs_week:{event['author_id']}")
now = datetime.now(timezone.utc)
if now.weekday() != 6 or now.hour < 9: # Sunday, after 09:00 UTC
return
if not ctx.ephemeral.dedup(f"mvp:{now:%Y-%m-%d}", ttl_seconds=86400):
return
counts = ctx.kv.list_values(prefix="msgs_week:") or {} # {key: count}
if not counts:
return
top_key, top_count = max(counts.items(), key=lambda kv: int(kv[1] or 0))
uid = top_key.split(":", 1)[1]
channel_id = ctx.kv.get("announce_channel_id")
if channel_id:
ctx.discord.send_message(channel_id=str(channel_id),
content=f"🏆 Member of the week: <@{uid}> with {top_count} messages.")
for key in counts: # reset weekly counters
ctx.kv.delete(key)