Build an hourly lottery: /lottery buy takes 50 coins per ticket, every hour the pot is drawn among the ticket holders and the winner is announced in a channel I choose, and /lottery pot shows the current pot and ticket count
An hourly lottery where members buy tickets for 50 coins each. Every hour, a random ticket is drawn and the winner receives the entire pot. The more tickets you buy, the better your chances of winning!
Commands:
/lottery buy — Purchase a ticket for 50 coins/lottery pot — View the current pot and ticket countSetup: Configure the announcement channel in settings where draw results will be posted.
Made by MEE6. Remix it to get a copy of your own and change anything you like.
🎫 Ticket purchased! You spent 50 coins. Current pot: 50 coins • Tickets: 1
🎫 Ticket purchased! You spent 50 coins. Current pot: 100 coins • Tickets: 2
🎫 Ticket purchased! You spent 50 coins. Current pot: 150 coins • Tickets: 3
The pot will be drawn within the next hour!
150 coins
3
3
🎉 @Sam wins 150 coins!
3
150 coins
// Hourly lottery: buy tickets for 50 coins, winner drawn every hour
//
// Storage:
// - global 'lottery_pot': total coins in current pot
// - global 'lottery_tickets': array of user IDs (one entry per ticket)
const TICKET_PRICE = 50;
const DRAW_INTERVAL = 3600; // 1 hour in seconds
/** 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.settings({
announcement_channel: {
type: 'channel',
label: 'Lottery announcement channel',
required: true,
sample: 'general',
},
});
// Buy a lottery ticket
mee6.command('lottery buy', { description: 'Buy a lottery ticket for 50 coins' }, async (ctx) => {
// Check balance
const balance = await mee6.economy.balance(ctx.author.id);
if (balance.coins < TICKET_PRICE) {
await ctx.reply(`❌ You need ${TICKET_PRICE} coins to buy a ticket. You have ${balance.coins} coins.`);
return;
}
// Deduct coins
await mee6.economy.remove(ctx.author.id, TICKET_PRICE);
// Add ticket to pot
const pot = (await mee6.store.get<number>('lottery_pot', 0)) + TICKET_PRICE;
const tickets = await mee6.store.get<string[]>('lottery_tickets', []);
tickets.push(ctx.author.id);
await mee6.store.set('lottery_pot', pot);
await mee6.store.set('lottery_tickets', tickets);
// Schedule the draw if none is coming. The schedule itself says so: a
// flag would stay set when a draw fails, and no draw would come again.
const scheduled = (await mee6.schedule.list()).some((s) => s.key === 'lottery_draw');
if (!scheduled) {
await mee6.schedule.once('lottery_draw', DRAW_INTERVAL, {
channelId: ctx.settings.announcement_channel,
});
}
await ctx.reply(
`🎫 Ticket purchased! You spent ${TICKET_PRICE} coins.\n` +
`Current pot: **${pot} coins** • Tickets: **${tickets.length}**`
);
});
// Show current pot and ticket count
mee6.command('lottery pot', { description: 'View the current lottery pot and ticket count' }, async (ctx) => {
const pot = await mee6.store.get<number>('lottery_pot', 0);
const tickets = await mee6.store.get<string[]>('lottery_tickets', []);
if (tickets.length === 0) {
await ctx.reply('🎰 No active lottery right now. Use `/lottery buy` to start one!');
return;
}
// Count tickets per player
const ticketCount: Record<string, number> = {};
for (const userId of tickets) {
ticketCount[userId] = (ticketCount[userId] || 0) + 1;
}
const players = Object.keys(ticketCount).length;
await ctx.reply({
embed: {
title: '🎰 Hourly Lottery',
description: `The pot will be drawn within the next hour!`,
color: '#f1c40f',
fields: [
{ name: '💰 Current Pot', value: `${pot} coins`, inline: true },
{ name: '🎫 Total Tickets', value: `${tickets.length}`, inline: true },
{ name: '👥 Players', value: `${players}`, inline: true },
],
footer: { text: 'Buy a ticket with /lottery buy for 50 coins' },
},
});
});
// Scheduled draw event
mee6.on('scheduled', async (ctx) => {
if (ctx.scheduleKey !== 'lottery_draw') return;
const payload = ctx.payload as { channelId: string } | null;
if (!payload?.channelId) return;
const pot = await mee6.store.get<number>('lottery_pot', 0);
const tickets = await mee6.store.get<string[]>('lottery_tickets', []);
// If no tickets, don't draw
if (tickets.length === 0) return;
// Pick a random winner among the members still in the server: the tickets
// of someone who left are set aside, and another one is drawn.
let drawn = [...tickets];
let winnerId = '';
while (drawn.length > 0 && !winnerId) {
const userId = drawn[Math.floor(Math.random() * drawn.length)];
try {
await mee6.economy.add(userId, pot);
winnerId = userId;
} catch (err) {
if (!hasLeft(err)) throw err;
drawn = drawn.filter((id) => id !== userId);
}
}
// Reset the lottery before announcing: a failed announcement must not
// keep the next lottery from starting.
await mee6.store.delete('lottery_tickets');
if (!winnerId) {
// Everyone who bought a ticket has left: the pot waits for the next draw.
await ctx.send(
payload.channelId,
`🎰 Nobody who bought a ticket is still here: the ${pot} coins roll over to the next draw.`
);
return;
}
await mee6.store.delete('lottery_pot');
// Announce the winner
await ctx.send(payload.channelId, {
embed: {
title: '🎰 Lottery Draw!',
description: `🎉 **<@${winnerId}> wins ${pot} coins!**`,
color: '#2ecc71',
fields: [
{ name: '🎫 Total Tickets', value: `${tickets.length}`, inline: true },
{ name: '💰 Prize', value: `${pot} coins`, inline: true },
],
footer: { text: 'Buy a ticket for the next draw with /lottery buy' },
},
});
});