Add /temprole give which gives a member a role I choose for a duration in minutes, hours or days with a reason, removes it automatically when the time is up, and /temprole list which shows who holds a temporary role and until when
Give members roles for a set duration (minutes, hours, or days) that are automatically removed when the time expires. Track all active temporary roles and see when they expire.
Made by MEE6. Remix it to get a copy of your own and change anything you like.
✅ Gave @Alex the @MEE6 role for 2 hours (until September 14, 2026, 09:43 AM). Reason: Won the contest
✅ Gave @Jordan the @MEE6 role for 30 minutes (until September 14, 2026, 08:13 AM). Reason: Spam warning
• @Jordan has @MEE6 until 2 months ago Reason: Spam warning
• @Alex has @MEE6 until 2 months ago Reason: Won the contest
• @Alex has @MEE6 until 2 months ago Reason: Won the contest
// Temporary Roles: give roles for a set duration, remove them automatically
interface TempRole {
userId: string;
roleId: string;
expiresAt: number; // unix seconds
reason: string;
grantedBy: string;
}
// Every temporary role lives in one list, and a sweep every five minutes
// removes the ones that expired. One schedule for all of them: a creation
// may hold 25 schedules, a server may hand out many more temporary roles.
const INDEX_KEY = 'temproles';
const MAX_TEMP_ROLES = 200;
const REMOVALS_PER_SWEEP = 8; // a run may change at most 10 roles
mee6.schedule.every('temprole_sweep', 300);
async function readIndex(): Promise<TempRole[]> {
return (await mee6.store.get<TempRole[]>(INDEX_KEY)) || [];
}
mee6.command(
'temprole give',
{
description: 'Give a member a role temporarily',
options: [
{ name: 'member', type: 'user', description: 'Who to give the role to', required: true },
{ name: 'role', type: 'role', description: 'Which role to give', required: true },
{ name: 'duration', type: 'integer', description: 'How long (number)', required: true },
{
name: 'unit',
type: 'string',
description: 'Time unit',
required: true,
choices: [
{ name: 'Minutes', value: 'minutes' },
{ name: 'Hours', value: 'hours' },
{ name: 'Days', value: 'days' },
],
},
{ name: 'reason', type: 'string', description: 'Reason for this role' },
],
},
async (ctx) => {
const userId = ctx.option<string>('member');
const roleId = ctx.option<string>('role');
const duration = ctx.option<number>('duration');
const unit = ctx.option<string>('unit');
const reason = ctx.option<string>('reason', 'No reason given');
if (duration < 1) {
await ctx.reply('Duration must be at least 1.');
return;
}
const multiplier = { minutes: 60, hours: 3600, days: 86400 }[unit];
const seconds = duration * multiplier;
if (seconds > 31536000) {
await ctx.reply('Duration cannot exceed 1 year.');
return;
}
const index = await readIndex();
if (index.length >= MAX_TEMP_ROLES && !index.some((t) => t.userId === userId && t.roleId === roleId)) {
await ctx.reply(`❌ Too many temporary roles are active (${MAX_TEMP_ROLES}). Wait for some to expire.`);
return;
}
const expiresAt = Math.floor(Date.now() / 1000) + seconds;
try {
await mee6.addRole(userId, roleId);
} catch (e) {
await ctx.reply(
`❌ Could not give the role. Make sure I have the Manage Roles permission and a higher role than <@&${roleId}>.`
);
return;
}
// Giving the same role again replaces the previous expiry.
const kept = index.filter((t) => !(t.userId === userId && t.roleId === roleId));
kept.push({ userId, roleId, expiresAt, reason, grantedBy: ctx.author.id });
await mee6.store.set(INDEX_KEY, kept);
const unitText = duration === 1 ? unit.slice(0, -1) : unit;
await ctx.reply(
`✅ Gave <@${userId}> the <@&${roleId}> role for ${duration} ${unitText} (until <t:${expiresAt}:f>).\nReason: ${reason}`
);
}
);
mee6.command('temprole list', { description: 'Show all active temporary roles' }, async (ctx) => {
const now = Math.floor(Date.now() / 1000);
const active = (await readIndex()).filter((t) => t.expiresAt > now).sort((a, b) => a.expiresAt - b.expiresAt);
if (active.length === 0) {
await ctx.reply('No temporary roles are currently active.');
return;
}
const lines = active
.slice(0, 20)
.map((t) => `• <@${t.userId}> has <@&${t.roleId}> until <t:${t.expiresAt}:R>\n Reason: ${t.reason}`);
if (active.length > 20) lines.push(`… and ${active.length - 20} more`);
await ctx.reply({
embed: {
title: '⏰ Temporary Roles',
description: lines.join('\n\n'),
color: '#5865F2',
},
});
});
mee6.on('scheduled', async (ctx) => {
if (ctx.scheduleKey !== 'temprole_sweep') return;
const now = Math.floor(Date.now() / 1000);
const index = await readIndex();
const expired = index.filter((t) => t.expiresAt <= now);
if (expired.length === 0) return;
// Remove a few per sweep; the rest go on the next one, five minutes later.
const batch = expired.slice(0, REMOVALS_PER_SWEEP);
const done = new Set<string>();
for (const t of batch) {
try {
await mee6.removeRole(t.userId, t.roleId);
mee6.log(`Removed temporary role ${t.roleId} from ${t.userId}`);
} catch (e) {
mee6.log(`Failed to remove role ${t.roleId} from ${t.userId}: ${e}`);
}
done.add(`${t.userId}:${t.roleId}`);
}
await mee6.store.set(
INDEX_KEY,
index.filter((t) => !done.has(`${t.userId}:${t.roleId}`))
);
});