Build a duel: /duel challenges another member, both start at 20 health, Attack and Heal buttons alternate turns in one embed that updates in place, and the winner takes 100 coins from the loser
A turn-based duel system where members can challenge each other to combat for coins.
/duel @member — Challenge another member to a duel for 100 coinsMade by MEE6. Remix it to get a copy of your own and change anything you like.
@Alex wins and takes 100 coins from @You!
0 HP
VS
1 HP
You have 510 coins.
// Duel: challenge another member to turn-based combat for coins
interface DuelState {
players: [string, string]; // [challenger, challenged]
health: [number, number];
turn: 0 | 1; // whose turn it is
started: number; // timestamp
}
mee6.command(
'duel',
{
description: 'Challenge another member to a duel for 100 coins',
options: [{ name: 'opponent', type: 'user', description: 'Who to challenge', required: true }],
},
async (ctx) => {
const opponentId = ctx.option<string>('opponent');
// Validation
if (opponentId === ctx.author.id) {
await ctx.reply('You cannot duel yourself!');
return;
}
const opponent = await mee6.getMember(opponentId);
if (!opponent) {
await ctx.reply('That member was not found.');
return;
}
if (opponent.bot) {
await ctx.reply('You cannot duel a bot!');
return;
}
// Check both members have enough coins
const challengerBal = await mee6.economy.balance(ctx.author.id);
const opponentBal = await mee6.economy.balance(opponentId);
if (challengerBal.coins < 100) {
await ctx.reply('You need at least 100 coins to duel!');
return;
}
if (opponentBal.coins < 100) {
await ctx.reply(`${opponent.displayName} does not have enough coins to accept a duel.`);
return;
}
// Create the duel
const state: DuelState = {
players: [ctx.author.id, opponentId],
health: [20, 20],
turn: 0,
started: Date.now(),
};
const sent = await ctx.reply({
embed: renderDuel(state),
buttons: renderButtons(state),
});
await mee6.store.set(`duel_${sent.messageId}`, state, { ttl: 1800 }); // 30 min
}
);
mee6.on('button', async (ctx) => {
const state = await mee6.store.get<DuelState>(`duel_${ctx.message.id}`);
if (!state) return;
// Only the two players can interact
if (!state.players.includes(ctx.author.id)) {
await ctx.reply({ content: 'This is not your duel!', ephemeral: true });
return;
}
// Only the current turn's player can act
if (ctx.author.id !== state.players[state.turn]) {
await ctx.reply({ content: 'It is not your turn!', ephemeral: true });
return;
}
const opponent = state.turn === 0 ? 1 : 0;
if (ctx.customId === 'attack') {
// Attack deals 3-7 damage
const damage = Math.floor(Math.random() * 5) + 3;
state.health[opponent] = Math.max(0, state.health[opponent] - damage);
// Check for win
if (state.health[opponent] <= 0) {
await finishDuel(ctx, state);
return;
}
// Next turn
state.turn = opponent;
} else if (ctx.customId === 'heal') {
// Heal restores 2-5 health, max 20
const healing = Math.floor(Math.random() * 4) + 2;
state.health[state.turn] = Math.min(20, state.health[state.turn] + healing);
// Next turn
state.turn = opponent;
}
// Update the board
await ctx.editOriginal({
embed: renderDuel(state),
buttons: renderButtons(state),
});
await mee6.store.set(`duel_${ctx.message.id}`, state, { ttl: 1800 });
});
async function finishDuel(ctx: any, state: DuelState) {
const winner = state.health[0] > 0 ? 0 : 1;
const loser = winner === 0 ? 1 : 0;
const winnerId = state.players[winner];
const loserId = state.players[loser];
// Transfer 100 coins
await mee6.economy.remove(loserId, 100);
await mee6.economy.add(winnerId, 100);
await ctx.editOriginal({
embed: {
title: '⚔️ Duel Complete',
description: `<@${winnerId}> wins and takes 100 coins from <@${loserId}>!`,
color: '#e74c3c',
fields: [
{ name: `<@${state.players[0]}>`, value: `${state.health[0]} HP`, inline: true },
{ name: '⚔️', value: 'VS', inline: true },
{ name: `<@${state.players[1]}>`, value: `${state.health[1]} HP`, inline: true },
],
},
buttons: [],
});
await mee6.store.delete(`duel_${ctx.message.id}`);
}
function renderDuel(state: DuelState) {
const [p1, p2] = state.players;
const [h1, h2] = state.health;
const currentPlayer = state.players[state.turn];
return {
title: '⚔️ Duel in Progress',
description: `<@${currentPlayer}>'s turn`,
color: '#e74c3c',
fields: [
{ name: `<@${p1}>`, value: `❤️ ${h1} HP`, inline: true },
{ name: '⚔️', value: 'VS', inline: true },
{ name: `<@${p2}>`, value: `❤️ ${h2} HP`, inline: true },
],
footer: { text: 'Attack: 3-7 damage | Heal: 2-5 HP (max 20)' },
};
}
function renderButtons(state: DuelState) {
return [
{ label: 'Attack', customId: 'attack', style: 'danger' as const },
{ label: 'Heal', customId: 'heal', style: 'success' as const },
];
}