Build Connect 4 between two members: a 6x7 board drawn with emoji in the message, seven column buttons under it laid out over two rows, each click drops a disc and edits the board in place, clicks from anyone but the two players are refused privately, and when four line up or the board fills the columns give way to a Play again button that only those two can press
Play Connect 4 against another member on a 6×7 board.
How to play:
/connect4 @opponent starts a gameMade by MEE6. Remix it to get a copy of your own and change anything you like.
⚪⚪⚪⚪⚪⚪⚪ ⚪⚪⚪⚪⚪⚪⚪ ⚪⚪⚪⚪⚪⚪⚪ ⚪⚪⚪⚪⚪⚪⚪ ⚪⚪⚪⚪⚪⚪⚪ ⚪⚪⚪⚪⚪⚪⚪ 1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣
🔴 @You's turn @You 🔴 vs 🟡 @Jordan
/**
* Connect 4 — two-player game on a 6x7 board
* The board is drawn with emoji, buttons let players drop discs
*/
const ROWS = 6;
const COLS = 7;
const EMPTY = '⚪';
const RED = '🔴';
const YELLOW = '🟡';
interface Game {
board: string[][];
players: [string, string]; // [red, yellow]
turn: 0 | 1; // whose turn
over: boolean;
winner?: 0 | 1 | 'draw';
}
function createBoard(): string[][] {
return Array.from({ length: ROWS }, () => Array(COLS).fill(EMPTY));
}
function dropDisc(game: Game, col: number): boolean {
// Find lowest empty row in this column
for (let row = ROWS - 1; row >= 0; row--) {
if (game.board[row][col] === EMPTY) {
game.board[row][col] = game.turn === 0 ? RED : YELLOW;
return true;
}
}
return false; // column full
}
function checkWin(board: string[][], row: number, col: number): boolean {
const disc = board[row][col];
if (disc === EMPTY) return false;
// Check horizontal
let count = 1;
for (let c = col - 1; c >= 0 && board[row][c] === disc; c--) count++;
for (let c = col + 1; c < COLS && board[row][c] === disc; c++) count++;
if (count >= 4) return true;
// Check vertical
count = 1;
for (let r = row - 1; r >= 0 && board[r][col] === disc; r--) count++;
for (let r = row + 1; r < ROWS && board[r][col] === disc; r++) count++;
if (count >= 4) return true;
// Check diagonal \
count = 1;
for (let i = 1; row - i >= 0 && col - i >= 0 && board[row - i][col - i] === disc; i++) count++;
for (let i = 1; row + i < ROWS && col + i < COLS && board[row + i][col + i] === disc; i++) count++;
if (count >= 4) return true;
// Check diagonal /
count = 1;
for (let i = 1; row - i >= 0 && col + i < COLS && board[row - i][col + i] === disc; i++) count++;
for (let i = 1; row + i < ROWS && col - i >= 0 && board[row + i][col - i] === disc; i++) count++;
if (count >= 4) return true;
return false;
}
function isBoardFull(board: string[][]): boolean {
return board[0].every((cell) => cell !== EMPTY);
}
function renderBoard(board: string[][]): string {
let output = '';
for (let row = 0; row < ROWS; row++) {
output += board[row].join('') + '\n';
}
output += '1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣'; // column numbers
return output;
}
function statusMessage(game: Game): string {
if (game.over) {
if (game.winner === 'draw') {
return `**Draw!** The board is full.\n<@${game.players[0]}> vs <@${game.players[1]}>`;
} else {
const winner = game.players[game.winner!];
const disc = game.winner === 0 ? RED : YELLOW;
return `**${disc} Wins!**\n<@${winner}> beat <@${game.players[1 - game.winner!]}>`;
}
}
const current = game.players[game.turn];
const disc = game.turn === 0 ? RED : YELLOW;
return `${disc} **<@${current}>'s turn**\n<@${game.players[0]}> ${RED} vs ${YELLOW} <@${game.players[1]}>`;
}
function columnButtons(game: Game) {
if (game.over) {
return [
[
{
label: 'Play again',
customId: 'play_again',
style: 'primary' as const,
},
],
];
}
// Check which columns are full
const buttons = [];
for (let col = 0; col < COLS; col++) {
const full = game.board[0][col] !== EMPTY;
buttons.push({
label: `${col + 1}`,
customId: `col_${col}`,
style: 'secondary' as const,
disabled: full,
});
}
// Split into two rows: 0-4, 5-6
return [buttons.slice(0, 5), buttons.slice(5)];
}
mee6.command(
'connect4',
{
description: 'Play Connect 4 against another member',
options: [{ name: 'opponent', type: 'user', description: 'Who to play against', required: true }],
},
async (ctx) => {
const opponentId = ctx.option<string>('opponent');
if (opponentId === ctx.author.id) {
await ctx.reply('You cannot play against yourself!');
return;
}
const opponent = await mee6.getMember(opponentId);
if (!opponent) {
await ctx.reply('That user is not in this server.');
return;
}
if (opponent.bot) {
await ctx.reply('You cannot play against a bot!');
return;
}
const game: Game = {
board: createBoard(),
players: [ctx.author.id, opponentId],
turn: 0,
over: false,
};
const sent = await ctx.reply({
content: `${renderBoard(game.board)}\n\n${statusMessage(game)}`,
buttons: columnButtons(game),
});
// Store game state keyed by message id, expires in 1 hour
await mee6.store.set(`game_${sent.messageId}`, game, { ttl: 3600 });
}
);
mee6.on('button', async (ctx) => {
const game = await mee6.store.get<Game>(`game_${ctx.message.id}`);
if (!game) return;
// Check if this person is one of the players
if (!game.players.includes(ctx.author.id)) {
await ctx.reply({ content: 'This is not your game.', ephemeral: true });
return;
}
if (ctx.customId === 'play_again') {
// Only players can restart
const newGame: Game = {
board: createBoard(),
players: game.players,
turn: 0,
over: false,
};
await ctx.editOriginal({
content: `${renderBoard(newGame.board)}\n\n${statusMessage(newGame)}`,
buttons: columnButtons(newGame),
});
await mee6.store.set(`game_${ctx.message.id}`, newGame, { ttl: 3600 });
return;
}
if (ctx.customId.startsWith('col_')) {
// 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;
}
const col = parseInt(ctx.customId.split('_')[1]);
const dropped = dropDisc(game, col);
if (!dropped) {
await ctx.reply({ content: 'That column is full!', ephemeral: true });
return;
}
// Find which row the disc landed in
let row = 0;
for (let r = 0; r < ROWS; r++) {
if (game.board[r][col] !== EMPTY) {
row = r;
break;
}
}
// Check for win
if (checkWin(game.board, row, col)) {
game.over = true;
game.winner = game.turn;
} else if (isBoardFull(game.board)) {
game.over = true;
game.winner = 'draw';
} else {
// Switch turns
game.turn = game.turn === 0 ? 1 : 0;
}
await ctx.editOriginal({
content: `${renderBoard(game.board)}\n\n${statusMessage(game)}`,
buttons: columnButtons(game),
});
await mee6.store.set(`game_${ctx.message.id}`, game, { ttl: 3600 });
}
});