Build tic-tac-toe: /tictactoe challenges another member, posts a 3x3 grid of buttons, updates the board in place on every click, refuses clicks from anyone but the two players, and announces the winner or a draw
Play tic-tac-toe against another member!
How to use:
/tictactoe @opponent — Challenge someone to a gameOnly the two players can click the buttons, and only on their turn. Games expire after 1 hour if unfinished.
Made by MEE6. Remix it to get a copy of your own and change anything you like.
🎮 Game Over! X wins! @You is victorious!
// Tic-Tac-Toe game for two players
type Player = 'X' | 'O';
type Cell = Player | '';
type Board = [Cell, Cell, Cell, Cell, Cell, Cell, Cell, Cell, Cell];
interface Game {
players: [string, string]; // [X player id, O player id]
board: Board;
turn: 0 | 1; // index into players array
winner?: Player | 'draw';
}
// Check if there's a winner
function checkWinner(board: Board): Player | 'draw' | null {
const lines = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8], // rows
[0, 3, 6],
[1, 4, 7],
[2, 5, 8], // columns
[0, 4, 8],
[2, 4, 6], // diagonals
];
for (const [a, b, c] of lines) {
if (board[a] && board[a] === board[b] && board[a] === board[c]) {
return board[a] as Player;
}
}
// Check for draw
if (board.every((cell) => cell !== '')) {
return 'draw';
}
return null;
}
// Generate status message
function statusMessage(game: Game): string {
if (game.winner === 'draw') {
return `🎮 **Game Over!** It's a draw!\n<@${game.players[0]}> vs <@${game.players[1]}>`;
}
if (game.winner) {
const winnerIndex = game.winner === 'X' ? 0 : 1;
return `🎮 **Game Over!** ${game.winner} wins!\n<@${game.players[winnerIndex]}> is victorious!`;
}
const currentPlayer = game.turn === 0 ? 'X' : 'O';
return `🎮 **Tic-Tac-Toe**\n<@${game.players[0]}> (X) vs <@${game.players[1]}> (O)\n\n${currentPlayer}'s turn: <@${
game.players[game.turn]
}>`;
}
// Generate button grid
function createButtons(game: Game) {
const rows = [];
for (let row = 0; row < 3; row++) {
const buttons = [];
for (let col = 0; col < 3; col++) {
const index = row * 3 + col;
const cell = game.board[index];
buttons.push({
label: cell || '·',
customId: `ttt_${index}`,
style: cell === 'X' ? 'primary' : cell === 'O' ? 'danger' : 'secondary',
disabled: !!cell || !!game.winner,
});
}
rows.push(buttons);
}
return rows;
}
mee6.command(
'tictactoe',
{
description: 'Challenge someone to tic-tac-toe',
options: [{ name: 'opponent', type: 'user', description: 'Who to challenge', required: true }],
},
async (ctx) => {
const opponentId = ctx.option<string>('opponent');
// Prevent self-challenge
if (opponentId === ctx.author.id) {
await ctx.reply({ content: 'You cannot challenge yourself!', ephemeral: true });
return;
}
// Check if opponent is a bot
const opponent = await mee6.getMember(opponentId);
if (opponent?.bot) {
await ctx.reply({ content: 'You cannot challenge a bot!', ephemeral: true });
return;
}
// Initialize game
const game: Game = {
players: [ctx.author.id, opponentId],
board: ['', '', '', '', '', '', '', '', ''],
turn: 0,
};
const sent = await ctx.reply({
content: statusMessage(game),
buttons: createButtons(game),
});
// Store game state with 1 hour expiry
await mee6.store.set(`game_${sent.messageId}`, game, { ttl: 3600 });
}
);
mee6.on('button', async (ctx) => {
// Only handle tic-tac-toe buttons
if (!ctx.customId.startsWith('ttt_')) return;
// Load game state
const game = await mee6.store.get<Game>(`game_${ctx.message.id}`);
if (!game) {
await ctx.reply({ content: 'This game has expired.', ephemeral: true });
return;
}
// Check if game is already over
if (game.winner) {
await ctx.reply({ content: 'This game is already over!', ephemeral: true });
return;
}
// Check if clicker is one of the players
if (!game.players.includes(ctx.author.id)) {
await ctx.reply({ content: 'This is not your game.', ephemeral: true });
return;
}
// Check if it's their turn
if (ctx.author.id !== game.players[game.turn]) {
await ctx.reply({ content: 'It is not your turn.', ephemeral: true });
return;
}
// Parse cell index
const cellIndex = parseInt(ctx.customId.split('_')[1]);
// Check if cell is already taken
if (game.board[cellIndex]) {
await ctx.reply({ content: 'That cell is already taken.', ephemeral: true });
return;
}
// Make the move
const currentPlayer: Player = game.turn === 0 ? 'X' : 'O';
game.board[cellIndex] = currentPlayer;
// Check for winner
const result = checkWinner(game.board);
if (result) {
game.winner = result;
} else {
// Switch turns
game.turn = game.turn === 0 ? 1 : 0;
}
// Update the board
await ctx.editOriginal({
content: statusMessage(game),
buttons: createButtons(game),
});
// Save updated state
await mee6.store.set(`game_${ctx.message.id}`, game, { ttl: 3600 });
});