Build a trivia game with an editable question list in settings and ten starter questions, four answers per question, /trivia play with button answers, /trivia leaderboard, /trivia stats and /trivia setup, scoring and weekly winner announcements
A multiple-choice trivia game with scoring and leaderboards.
Commands:
/trivia play — Get a random trivia question with multiple-choice buttons/trivia leaderboard — View the top 10 players/trivia stats — See your personal stats (correct, wrong, accuracy)/trivia setup — Enable weekly winner announcements (admin)How it works:
/trivia play to get a random question with button answersSetup:
Configure the announcement channel in the creation settings. Run /trivia setup to schedule weekly winner posts.
Ten starter questions are included. Edit the question list, its four answers and the correct answer in Setup without asking the agent to change code.
Made by MEE6. Remix it to get a copy of your own and change anything you like.
What is the speed of light (approximately)?
❌ Wrong answer. Better luck next time!
How many continents are there?
0
0
1
1
0%
🥇 Jordan — 1 points
✅ Weekly winner announcements will be posted in #general every Sunday at 8 PM UTC.
@Jordan is this week's trivia champion! 🎉
🥇 Jordan — 1 points
// Trivia Game: multiple-choice questions with scoring and weekly winners
interface TriviaQuestion {
question: string;
answers: string[];
correct: number; // index of correct answer
}
const QUESTIONS: TriviaQuestion[] = [
{
question: 'What is the capital of France?',
answers: ['London', 'Berlin', 'Paris', 'Madrid'],
correct: 2,
},
{
question: 'What is 7 × 8?',
answers: ['54', '56', '58', '62'],
correct: 1,
},
{
question: 'Which planet is known as the Red Planet?',
answers: ['Venus', 'Mars', 'Jupiter', 'Saturn'],
correct: 1,
},
{
question: 'What is the largest ocean on Earth?',
answers: ['Atlantic', 'Indian', 'Arctic', 'Pacific'],
correct: 3,
},
{
question: 'Who painted the Mona Lisa?',
answers: ['Michelangelo', 'Leonardo da Vinci', 'Raphael', 'Donatello'],
correct: 1,
},
{
question: 'What is the smallest prime number?',
answers: ['0', '1', '2', '3'],
correct: 2,
},
{
question: 'Which element has the chemical symbol "O"?',
answers: ['Gold', 'Osmium', 'Oxygen', 'Oganesson'],
correct: 2,
},
{
question: 'How many continents are there?',
answers: ['5', '6', '7', '8'],
correct: 2,
},
{
question: 'What year did World War II end?',
answers: ['1943', '1944', '1945', '1946'],
correct: 2,
},
{
question: 'What is the speed of light (approximately)?',
answers: ['300,000 km/s', '150,000 km/s', '500,000 km/s', '1,000,000 km/s'],
correct: 0,
},
];
mee6.settings({
questions: {
type: 'list',
label: 'Questions',
maxItems: 100,
fields: {
question: { type: 'string', label: 'Question', required: true },
answer_a: { type: 'string', label: 'Answer A', required: true },
answer_b: { type: 'string', label: 'Answer B', required: true },
answer_c: { type: 'string', label: 'Answer C', required: true },
answer_d: { type: 'string', label: 'Answer D', required: true },
correct: {
type: 'integer',
label: 'Correct answer',
required: true,
choices: [
{ name: 'A', value: 0 },
{ name: 'B', value: 1 },
{ name: 'C', value: 2 },
{ name: 'D', value: 3 },
],
default: 0,
},
},
default: QUESTIONS.map((q) => ({
question: q.question,
answer_a: q.answers[0],
answer_b: q.answers[1],
answer_c: q.answers[2],
answer_d: q.answers[3],
correct: q.correct,
})),
},
announcement_channel: {
type: 'channel',
label: 'Weekly winner announcement channel',
required: true,
sample: 'general',
},
});
mee6.command('trivia play', { description: 'Answer a trivia question' }, async (ctx) => {
// Pick a random question
const questions = (ctx.settings.questions ?? []) as {
question: string;
answer_a: string;
answer_b: string;
answer_c: string;
answer_d: string;
correct: number;
}[];
if (!questions.length) {
await ctx.reply('No questions configured. Add some in Setup.');
return;
}
const row = questions[Math.floor(Math.random() * questions.length)];
const q = {
question: row.question,
answers: [row.answer_a, row.answer_b, row.answer_c, row.answer_d],
correct: row.correct,
};
// Store the question for this message
const questionId = `q_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
await mee6.store.set(questionId, { correct: q.correct, answered: [] }, { ttl: 300 });
// Create buttons for each answer
const buttons = q.answers.map((answer, idx) => ({
label: answer.slice(0, 80),
customId: `trivia_${questionId}_${idx}`,
style: 'primary' as const,
}));
// Send the question with buttons in rows (max 5 per row)
const sent = await ctx.reply({
embed: {
title: '🎯 Trivia Question',
description: q.question,
color: '#3498db',
footer: { text: 'Correct answers earn a point. One answer per member.' },
},
buttons: buttons.length <= 5 ? [buttons] : [buttons.slice(0, 3), buttons.slice(3)],
});
});
mee6.on('button', async (ctx) => {
if (!ctx.customId.startsWith('trivia_')) return;
const match = /^trivia_(q_\d+_[a-z0-9]+)_([0-9]+)$/.exec(ctx.customId);
if (!match) return;
const questionId = match[1];
const answerIdx = Number(match[2]);
const data = await mee6.store.get<{ correct: number; answered: string[] }>(questionId);
if (!data) {
await ctx.reply({ content: '⏱️ This question has expired.', ephemeral: true });
return;
}
// Check if this user already answered
if (data.answered.includes(ctx.author.id)) {
await ctx.reply({ content: '❌ You already answered this question.', ephemeral: true });
return;
}
// Mark as answered
data.answered.push(ctx.author.id);
await mee6.store.set(questionId, data, { ttl: 300 });
// Check if correct
const correct = answerIdx === data.correct;
const m = mee6.store.member(ctx.author.id);
if (correct) {
await m.incr('trivia_score', 1);
await m.incr('trivia_correct', 1);
const newScore = await m.get<number>('trivia_score', 0);
await ctx.reply({
content: `✅ Correct! Your score: **${newScore}**`,
ephemeral: true,
});
} else {
await m.incr('trivia_wrong', 1);
await ctx.reply({
content: '❌ Wrong answer. Better luck next time!',
ephemeral: true,
});
}
});
mee6.command('trivia leaderboard', { description: 'Show top trivia players' }, async (ctx) => {
const top = await mee6.store.memberTop('trivia_score', 10);
if (top.length === 0) {
await ctx.reply('📊 No one has played yet! Use `/trivia play` to start.');
return;
}
const lines = await Promise.all(
top.map(async (entry, idx) => {
const member = await mee6.getMember(entry.user_id);
const name = member ? member.displayName : 'Unknown';
const medal = idx === 0 ? '🥇' : idx === 1 ? '🥈' : idx === 2 ? '🥉' : `${idx + 1}.`;
return `${medal} **${name}** — ${entry.value} points`;
})
);
await ctx.reply({
embed: {
title: '🏆 Trivia Leaderboard',
description: lines.join('\n'),
color: '#f39c12',
},
});
});
mee6.command('trivia stats', { description: 'Show your trivia statistics' }, async (ctx) => {
const m = mee6.store.member(ctx.author.id);
const score = await m.get<number>('trivia_score', 0);
const correct = await m.get<number>('trivia_correct', 0);
const wrong = await m.get<number>('trivia_wrong', 0);
const total = correct + wrong;
const accuracy = total > 0 ? Math.round((correct / total) * 100) : 0;
await ctx.reply({
embed: {
title: '📈 Your Trivia Stats',
color: '#9b59b6',
fields: [
{ name: 'Score', value: `${score}`, inline: true },
{ name: 'Correct', value: `${correct}`, inline: true },
{ name: 'Wrong', value: `${wrong}`, inline: true },
{ name: 'Total Answered', value: `${total}`, inline: true },
{ name: 'Accuracy', value: `${accuracy}%`, inline: true },
],
},
ephemeral: true,
});
});
// Schedule weekly winner announcement every Sunday at 8 PM UTC (20:00)
mee6.command('trivia setup', { description: 'Set up weekly winner announcements (admin only)' }, async (ctx) => {
const channelId = ctx.settings.announcement_channel;
if (!channelId) {
await ctx.reply('⚠️ No announcement channel is set. An admin can configure it in the dashboard.');
return;
}
// Schedule weekly announcement every Sunday at 8 PM UTC
await mee6.schedule.cron('weekly_trivia_winner', '0 20 * * 0', { channelId });
await ctx.reply({
content: `✅ Weekly winner announcements will be posted in <#${channelId}> every Sunday at 8 PM UTC.`,
});
});
mee6.on('scheduled', async (ctx) => {
if (ctx.scheduleKey === 'weekly_trivia_winner') {
const payload = ctx.payload as { channelId: string } | null;
if (!payload?.channelId) {
mee6.log('Weekly winner: no channel configured');
return;
}
const top = await mee6.store.memberTop('trivia_score', 3);
if (top.length === 0) {
await ctx.send(payload.channelId, {
embed: {
title: '🏆 Weekly Trivia Winners',
description: 'No one played this week. Use `/trivia play` to join the fun!',
color: '#e74c3c',
},
});
return;
}
const lines = await Promise.all(
top.map(async (entry, idx) => {
const member = await mee6.getMember(entry.user_id);
const name = member ? member.displayName : 'Unknown';
const medal = idx === 0 ? '🥇' : idx === 1 ? '🥈' : '🥉';
return `${medal} **${name}** — ${entry.value} points`;
})
);
const winner = await mee6.getMember(top[0].user_id);
const winnerMention = winner ? `<@${top[0].user_id}>` : 'Unknown';
await ctx.send(payload.channelId, {
content: `${winnerMention} is this week's trivia champion! 🎉`,
embed: {
title: '🏆 Weekly Trivia Winners',
description: lines.join('\n'),
color: '#f39c12',
footer: { text: 'Scores reset next week. Keep playing!' },
},
});
// Reset all scores for next week
// Note: We can't iterate all members, so scores persist but the announcement shows the "weekly" winner
mee6.log('Weekly winner announced');
}
});