const { Client, GatewayIntentBits, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, AttachmentBuilder } = require('discord.js'); const { Board } = require('2048-webcomponent'); const { createCanvas, loadImage } = require('canvas'); const fs = require('fs'); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); client.once('ready', () => { console.log('Bot is online!'); }); client.on('messageCreate', async (message) => { if (message.content === '!2048') { let board = new Board(); board.setupNewGame(); const attachment = await renderBoard(board); const embed = new EmbedBuilder() .setColor('#F08080') .setTitle('2048 Oyunu') .setDescription('Yön tuşlarıyla oynayın') .setImage('attachment://board.png'); const row = new ActionRowBuilder() .addComponents( new ButtonBuilder() .setCustomId('up') .setLabel('⬆️') .setStyle(ButtonStyle.Primary), new ButtonBuilder() .setCustomId('left') .setLabel('⬅️') .setStyle(ButtonStyle.Primary), new ButtonBuilder() .setCustomId('right') .setLabel('➡️') .setStyle(ButtonStyle.Primary), new ButtonBuilder() .setCustomId('down') .setLabel('⬇️') .setStyle(ButtonStyle.Primary) ); const gameMessage = await message.channel.send({ embeds: [embed], files: [attachment], components: [row] }); const filter = interaction => ['up', 'down', 'left', 'right'].includes(interaction.customId) && interaction.user.id === message.author.id; const collector = gameMessage.createMessageComponentCollector({ filter, time: 60000 }); collector.on('collect', async interaction => { const direction = interaction.customId; switch (direction) { case 'up': board.moveUp(); break; case 'down': board.moveDown(); break; case 'left': board.moveLeft(); break; case 'right': board.moveRight(); break; } if (board.hasWon()) { embed.setTitle('2048 Oyunu - Kazandınız!'); collector.stop(); } else if (board.isGameOver()) { embed.setTitle('2048 Oyunu - Kaybettiniz!'); collector.stop(); } const attachment = await renderBoard(board); embed.setImage('attachment://board.png'); await interaction.update({ embeds: [embed], files: [attachment] }); }); collector.on('end', collected => { gameMessage.edit({ components: [] }); }); } }); async function renderBoard(board) { const canvas = createCanvas(400, 400); const ctx = canvas.getContext('2d'); // Tahtayı ve hücreleri çizin ctx.fillStyle = '#bbada0'; ctx.fillRect(0, 0, 400, 400); const colors = { 0: '#cdc1b4', 2: '#eee4da', 4: '#ede0c8', 8: '#f2b179', 16: '#f59563', 32: '#f67c5f', 64: '#f65e3b', 128: '#edcf72', 256: '#edcc61', 512: '#edc850', 1024: '#edc53f', 2048: '#edc22e' }; for (let i = 0; i < 4; i++) { for (let j = 0; j < 4; j++) { const value = board.grid.cells[i][j] ? board.grid.cells[i][j].value : 0; ctx.fillStyle = colors[value]; ctx.fillRect(j * 100 + 10, i * 100 + 10, 90, 90); if (value) { ctx.fillStyle = value > 4 ? '#f9f6f2' : '#776e65'; ctx.font = 'bold 45px Arial'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(value, j * 100 + 55, i * 100 + 55); } } } const buffer = canvas.toBuffer('image/png'); fs.writeFileSync('./board.png', buffer); return new AttachmentBuilder('./board.png', { name: 'board.png' }); } client.login('YOUR_DISCORD_BOT_TOKEN');