When a member leaves, remove all their XP and post in a channel I choose what level they were; when they come back, DM them that they start from zero
Tracks members who leave the server with XP progress. When someone leaves, their level is recorded, their XP is reset to zero, and a log is posted to your chosen channel. When they return, they receive a DM explaining they're starting fresh, along with their previous level for reference.
Made by MEE6. Remix it to get a copy of your own and change anything you like.
Alex left the server at level 7 (2600 XP).
// XP Reset Tracker: records departing members' levels, removes their XP,
// and notifies returning members they start from zero.
mee6.settings({
log_channel: {
type: 'channel',
label: 'Channel to log departures',
required: true,
sample: 'mod-logs',
},
});
// A call removes at most 100,000 XP: a bigger total goes in parts, forty in a
// run (a run makes 50 SDK calls at most), and what is left after that in a
// run scheduled half a minute later.
const MAX_PER_CALL = 100000;
const PARTS_PER_RUN = 40;
/** Remove `amount` XP in parts, and schedule the rest when one run is not enough. */
async function removeXp(userId: string, amount: number) {
let left = amount;
for (let part = 0; left > 0 && part < PARTS_PER_RUN; part++) {
const step = Math.min(left, MAX_PER_CALL);
await mee6.xp.remove(userId, step);
left -= step;
}
if (left > 0) {
await mee6.schedule.once(`xp_reset_${userId}`, 30, { userId, left });
}
}
mee6.on('scheduled', async (ctx) => {
if (!ctx.scheduleKey.startsWith('xp_reset_')) return;
const p = ctx.payload as { userId: string; left: number } | null;
if (p) await removeXp(p.userId, p.left);
});
// When a member leaves, record their level and reset their XP
mee6.on('member_leave', async (ctx) => {
const channelId = ctx.settings.log_channel;
if (!channelId) {
mee6.log('No log channel configured');
return;
}
// Get their current XP data
const xpData = await mee6.xp.get(ctx.author.id);
// Store their level for when they return
const m = mee6.store.member(ctx.author.id);
await m.set('previous_level', xpData.level, { ttl: 31536000 }); // 1 year
// Remove all their XP
if (xpData.xp > 0) {
await removeXp(ctx.author.id, xpData.xp);
}
// Log to the channel
await ctx.send(channelId, {
embed: {
title: '📉 Member Left',
description: `**${ctx.author.displayName}** left the server at level **${xpData.level}** (${xpData.xp} XP).`,
color: '#E74C3C',
footer: { text: `User ID: ${ctx.author.id}` },
},
});
});
// When a member (re)joins, check if they had a previous level
mee6.on('member_join', async (ctx) => {
const m = mee6.store.member(ctx.author.id);
const previousLevel = await m.get<number>('previous_level');
if (previousLevel) {
// DM them about starting over
const sent = await mee6.dm(ctx.author.id, {
embed: {
title: '👋 Welcome back!',
description: `You're rejoining the server. Your previous level was **${previousLevel}**, but you're starting fresh from **level 0**.`,
color: '#3498DB',
},
});
if (!sent) {
mee6.log(`Could not DM ${ctx.author.id} about level reset (DMs closed)`);
}
// Clear the stored level now that we've notified them
await m.delete('previous_level');
}
});