Count the XP each member gains this week from the messages this creation sees, post the top 10 every Sunday at 8pm UTC in a channel I choose, give a role I choose to the winner and take it back from last week\'s winner, and /race shows the live standings
Weekly XP Race tracks how much XP each member earns from messages throughout the week.
Every Sunday at 8pm UTC, the creation posts the top 10 to your chosen leaderboard channel and awards the winner role to the champion. The role is automatically removed from last week's winner and given to the new one.
Members can check live standings anytime with /race.
Settings:
Made by MEE6. Remix it to get a copy of your own and change anything you like.
🥇 Alex — 20 XP 🥈 Sam — 10 XP 🥉 Jordan — 10 XP
🥇 Jordan — 40 XP 🥈 Sam — 30 XP 🥉 Alex — 20 XP
/**
* Weekly XP Race
* Tracks member XP gains from messages each week, posts Sunday leaderboards,
* and awards a champion role to the winner.
*/
mee6.settings({
leaderboard_channel: {
type: 'channel',
label: 'Leaderboard channel',
required: true,
sample: 'announcements',
},
winner_role: {
type: 'role',
label: 'Winner role',
required: true,
sample: 'XP Champion',
},
xp_per_message: {
type: 'integer',
label: 'XP per message',
default: 10,
sample: 10,
},
});
// The week's counters live under a key that carries the week's number, so
// the Sunday reset is one increment — never a walk over every member.
async function weekKey(): Promise<string> {
const week = await mee6.store.get<number>('race_week', 0);
return `week_xp_${week}`;
}
// Track XP gains from messages seen this week
mee6.on('message', async (ctx) => {
const xp = ctx.settings.xp_per_message ?? 10;
const m = mee6.store.member(ctx.author.id);
const key = await weekKey();
// A member's first message of the week clears their past weeks' counters: a
// key per week would fill their storage (64 keys) in a year or so.
if ((await m.incr(key, xp)) === xp) {
for (const old of Object.keys(await m.all())) {
if (old.startsWith('week_xp_') && old !== key) await m.delete(old);
}
}
});
// /race command shows live standings
mee6.command('race', { description: 'View the current weekly XP race standings' }, async (ctx) => {
const top = await mee6.store.memberTop(await weekKey(), 10);
if (top.length === 0) {
await ctx.reply('No activity this week yet — start chatting to join the race! 🏁');
return;
}
const lines = await Promise.all(
top.map(async (entry, i) => {
const member = await mee6.getMember(entry.user_id);
const name = member ? member.displayName : 'Unknown';
const medal = i === 0 ? '🥇' : i === 1 ? '🥈' : i === 2 ? '🥉' : `${i + 1}.`;
return `${medal} **${name}** — ${entry.value} XP`;
})
);
await ctx.reply({
embed: {
title: '🏁 Weekly XP Race',
description: lines.join('\n'),
color: '#f1c40f',
footer: { text: 'Race resets every Sunday at 8pm UTC' },
},
});
});
// Sunday 8pm UTC = cron '0 20 * * 0'
mee6.schedule.cron('weekly_reset', '0 20 * * 0');
mee6.on('scheduled', async (ctx) => {
if (ctx.scheduleKey !== 'weekly_reset') return;
const channelId = ctx.settings.leaderboard_channel;
const winnerRoleId = ctx.settings.winner_role;
if (!channelId) {
mee6.log('No leaderboard channel configured');
return;
}
if (!winnerRoleId) {
mee6.log('No winner role configured');
return;
}
// Get top 10, then a new week: fresh counters for everyone, before anything
// below can fail and keep the week from ending
const top = await mee6.store.memberTop(await weekKey(), 10);
await mee6.store.incr('race_week');
if (top.length === 0) {
await ctx.send(channelId, '🏁 No activity this week — the race will start fresh!');
await mee6.store.delete('last_winner');
return;
}
// The standings, among the members still in the server
const members = await Promise.all(top.map((entry) => mee6.getMember(entry.userId)));
const standing = top.flatMap((entry, i) => {
const member = members[i];
return member ? [{ entry, member }] : [];
});
if (standing.length === 0) {
await ctx.send(channelId, '🏁 Everyone in the race this week has left the server — the race will start fresh!');
return;
}
// Build leaderboard
const lines = standing.map(({ entry, member }, i) => {
const medal = i === 0 ? '🥇' : i === 1 ? '🥈' : i === 2 ? '🥉' : `${i + 1}.`;
return `${medal} **${member.displayName}** — ${entry.value} XP`;
});
const winner = standing[0].member;
const winnerId = standing[0].entry.userId;
// Post the leaderboard
await ctx.send(channelId, {
embed: {
title: '🏆 Weekly XP Race Results',
description: lines.join('\n'),
color: '#f1c40f',
footer: { text: "Congratulations to this week's champion!" },
},
});
// Manage winner role: remove from last week's winner, give to this week's
const lastWinner = await mee6.store.get<string>('last_winner');
if (lastWinner && lastWinner !== winnerId) {
const lastMember = await mee6.getMember(lastWinner);
if (lastMember && lastMember.roles.includes(winnerRoleId)) {
await mee6.removeRole(lastWinner, winnerRoleId);
}
}
await mee6.store.set('last_winner', winnerId);
if (!winner.roles.includes(winnerRoleId)) {
await mee6.addRole(winnerId, winnerRoleId);
}
});