Give members 5 XP for every ten minutes they spend in a voice channel, paid as the time goes by, and add a /voicetime command that shows how long someone has spent in voice in total
Voice XP pays members 5 XP for every ten minutes they spend in a voice channel, while they are there: a sweep every ten minutes pays everyone still in voice, and leaving settles what is left.
/voicetime — How long you (or a member you name) have spent in voice in total, the current stay includedvoice_join — starts the clockvoice_leave — pays the last blocks and adds the stay to the totalscheduled — the ten-minute sweepMade by MEE6. Remix it to get a copy of your own and change anything you like.
You are level 7 with 2600 XP.
You are level 7 with 2630 XP.
@Alex has spent 1h 0m in voice channels (in voice right now).
@Sam has spent 25m in voice channels.
// Voice XP — 5 XP for every ten minutes in a voice channel, paid as the
// minutes go by: a sweep every ten minutes pays everyone still in voice,
// and leaving settles what is left.
const XP_PER_BLOCK = 5;
const BLOCK_MS = 10 * 60 * 1000;
interface Stay {
/** when they joined, ms */
since: number;
/** how far their stay has been paid, ms */
paidUntil: number;
}
// Everyone in voice right now, in one key: the sweep reads it once and pays
// each of them, instead of a read per member.
async function stays(): Promise<Record<string, Stay>> {
return (await mee6.store.get<Record<string, Stay>>('in_voice')) || {};
}
/** Pay the full ten-minute blocks since the last payment; how much XP that was. */
async function payUpTo(userId: string, stay: Stay, now: number): Promise<number> {
const blocks = Math.floor((now - stay.paidUntil) / BLOCK_MS);
if (blocks <= 0) return 0;
const xp = blocks * XP_PER_BLOCK;
await mee6.xp.give(userId, xp);
stay.paidUntil += blocks * BLOCK_MS;
return xp;
}
/** Whether a call was refused because the member has left the server. */
function hasLeft(err: unknown): boolean {
return String(err).includes('is not in this server');
}
mee6.schedule.every('voice_sweep', 600);
mee6.on('voice_join', async (ctx) => {
const now = Date.now();
const all = await stays();
all[ctx.author.id] = { since: now, paidUntil: now };
await mee6.store.set('in_voice', all);
});
mee6.on('scheduled', async (ctx) => {
if (ctx.scheduleKey !== 'voice_sweep') return;
const now = Date.now();
const all = await stays();
let changed = false;
// A run may make 50 SDK calls: forty members a sweep is the ceiling.
for (const [userId, stay] of Object.entries(all).slice(0, 40)) {
// One member's failure must stop neither the others nor the save below:
// without it, the members already paid would be paid again next sweep.
try {
const xp = await payUpTo(userId, stay, now);
if (xp > 0) {
changed = true;
mee6.log(`${userId} earned ${xp} XP in voice`);
}
} catch (err) {
if (!hasLeft(err)) {
mee6.log(`could not pay ${userId}, next sweep tries again: ${err}`);
continue;
}
// Gone from the server, so gone from voice: no voice_leave will close
// their stay, the sweep does.
delete all[userId];
changed = true;
}
}
if (changed) await mee6.store.set('in_voice', all);
});
mee6.on('voice_leave', async (ctx) => {
const now = Date.now();
const all = await stays();
const stay = all[ctx.author.id];
if (!stay) return;
// The blocks the sweep has not paid yet, then the stay goes on the total.
// The stay ends even when they cannot be paid (they left the server):
// kept, the sweep would go on paying it.
try {
await payUpTo(ctx.author.id, stay, now);
} catch (err) {
if (!hasLeft(err)) mee6.log(`could not pay ${ctx.author.id} on leaving: ${err}`);
}
const m = mee6.store.member(ctx.author.id);
await m.incr('total_voice_time', Math.floor((now - stay.since) / 1000));
delete all[ctx.author.id];
await mee6.store.set('in_voice', all);
});
mee6.command(
'voicetime',
{
description: 'Check how long you or someone else has spent in voice',
options: [{ name: 'user', type: 'user', description: 'Member to check (leave blank for yourself)' }],
},
async (ctx) => {
const targetId = ctx.option<string>('user') || ctx.author.id;
const target = await mee6.getMember(targetId);
if (!target) {
await ctx.reply('Member not found.');
return;
}
const m = mee6.store.member(targetId);
const totalSeconds = await m.get<number>('total_voice_time', 0);
// A stay in progress counts too.
const stay = (await stays())[targetId];
const activeSeconds = stay ? Math.floor((Date.now() - stay.since) / 1000) : 0;
const grandTotal = totalSeconds + activeSeconds;
const hours = Math.floor(grandTotal / 3600);
const minutes = Math.floor((grandTotal % 3600) / 60);
const timeStr = hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
const inVoiceNote = stay ? ' (in voice right now)' : '';
await ctx.reply({
embed: {
title: '🎤 Voice Time',
description: `${target.mention} has spent **${timeStr}** in voice channels${inVoiceNote}.`,
color: '#5865F2',
footer: { text: 'Earn 5 XP per 10 minutes in voice, paid every ten minutes' },
},
});
}
);