Skip to content

Commit

Permalink
Update package.json and Fix 2048 game
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr5ecret committed Jan 14, 2024
1 parent 386e539 commit 344bd95
Show file tree
Hide file tree
Showing 17 changed files with 122 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
package-lock.json
test/test.js
node_modules/
86 changes: 51 additions & 35 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
{
"name": "flex-gamecord",
"version": "4.2.1-dev",
"description": "Discord Gamecord is a powerful npm package with a collection of minigames for your discord bot",
"main": "index.js",
"scripts": {
"test": "cd test && node ."
},
"author": "Aniket#0729",
"contributor": "Mr5ecret",
"license": "MIT",
"dependencies": {
"discord.js": "^14.6.0",
"html-entities": "^2.3.3",
"node-fetch": "^2.6.7"
},
"repository": {
"type": "git",
"url": "git+git@github.com:Mr5ecret/Flex-Gamecord.git"
},
"homepage": "https://discord-gamecord.js.org/",
"keywords": [
"discord-gamecord",
"discord-games",
"discord.js",
"minigames",
"paeonic"
],
"bugs": {
"url": "https://github.com/Mr5ecret/Flex-Gamecord/issues"
},
"directories": {
"src": "src"
}
}
{
"name": "flex-gamecord",
"version": "4.2.2",
"description": "Discord Gamecord is a powerful npm package with a collection of minigames for your discord bot",
"main": "index.js",
"scripts": {
"test": "cd test && node ."
},
"authors": [
{
"name": "Paeonic-Development",
"url": "https://github.com/Paeonic-Development"
},
{
"name": "Mr5ecret",
"url": "https://github.com/Mr5ecret"
}
],
"contributors": [
{
"name": "aniket091",
"url": "https://github.com/aniket091"
}
],
"contributor": "Mr5ecret",
"license": "MIT",
"dependencies": {
"canvas": "^2.11.2",
"discord.js": "^14.14.1",
"html-entities": "^2.3.3",
"node-fetch": "^2.6.7"
},
"repository": {
"type": "git",
"url": "git+git@github.com:Paeonic-Development/Flex-Gamecord.git"
},
"homepage": "https://github.com/Paeonic-Development/Flex-Gamecord",
"keywords": [
"discord-gamecord",
"discord-games",
"discord.js",
"minigames",
"paeonic"
],
"bugs": {
"url": "https://github.com/Paeonic-Development/Flex-Gamecord/issues"
},
"directories": {
"src": "src"
}
}
77 changes: 49 additions & 28 deletions src/2048.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { EmbedBuilder, ActionRowBuilder, AttachmentBuilder } = require('discord.js');
const { disableButtons, formatMessage, move, oppDirection, ButtonBuilder } = require('../utils/utils');
const chars = '0123456789abcdefghijklmnopqrstuvwxyz';
const events = require('events');

const { createCanvas, loadImage } = require('canvas');
const path = require('path');

module.exports = class TwoZeroFourEight extends events {
constructor(options = {}) {
Expand All @@ -12,7 +12,6 @@ module.exports = class TwoZeroFourEight extends events {
if (typeof options.message !== 'object') throw new TypeError('INVALID_MESSAGE: message option must be an object.');
if (typeof options.isSlashGame !== 'boolean') throw new TypeError('INVALID_COMMAND_TYPE: isSlashGame option must be a boolean.');


if (!options.embed) options.embed = {};
if (!options.embed.title) options.embed.title = '2048';
if (!options.embed.color) options.embed.color = '#5865F2';
Expand All @@ -22,11 +21,10 @@ module.exports = class TwoZeroFourEight extends events {
if (!options.emojis.down) options.emojis.down = '⬇️';
if (!options.emojis.left) options.emojis.left = '⬅️';
if (!options.emojis.right) options.emojis.right = '➡️';

if (!options.timeoutTime) options.timeoutTime = 60000;
if (!options.buttonStyle) options.buttonStyle = 'PRIMARY';


if (typeof options.embed !== 'object') throw new TypeError('INVALID_EMBED: embed option must be an object.');
if (typeof options.embed.title !== 'string') throw new TypeError('INVALID_EMBED: embed title must be a string.');
if (typeof options.embed.color !== 'string') throw new TypeError('INVALID_EMBED: embed color must be a string.');
Expand All @@ -42,7 +40,6 @@ module.exports = class TwoZeroFourEight extends events {
if (typeof options.playerOnlyMessage !== 'string') throw new TypeError('INVALID_MESSAGE: playerOnlyMessage option must be a string.');
}


super();
this.options = options;
this.message = options.message;
Expand All @@ -58,21 +55,45 @@ module.exports = class TwoZeroFourEight extends events {
}
}


async sendMessage(content) {
if (this.options.isSlashGame) return await this.message.editReply(content);
else return await this.message.channel.send(content);
}

async getBoardImage() {
const url = 'https://api.aniket091.xyz/2048?board=' + this.gameBoard.map(c => chars[c]).join('');
return await new AttachmentBuilder(url, { name: 'gameboard.png' });
}
const canvas = createCanvas(this.length * 100, this.length * 100);
const ctx = canvas.getContext('2d');
const imageFilenames = [
'0.png', '2.png', '4.png', '8.png', '16.png', '32.png', '64.png', '128.png', '256.png', '512.png', '1024.png', '2048.png',
];

const images = {};
for (let i = 0; i < imageFilenames.length; i++) {
const imagePath = path.join(__dirname, 'boardImages', imageFilenames[i]);
images[i] = await loadImage(imagePath).catch(err => {
console.error(`Error loading image: ${imagePath}`);
throw err;
});
}

for (let y = 0; y < this.length; y++) {
for (let x = 0; x < this.length; x++) {
const tileValue = this.gameBoard[y * this.length + x];
const tileImage = images[tileValue];

if (tileImage) {
ctx.drawImage(tileImage, x * 100, y * 100, 100, 100);
}
}
}

const buffer = canvas.toBuffer();
return new AttachmentBuilder(buffer, { name: 'gameboard.png' });
}

async startGame() {
if (this.options.isSlashGame || !this.message.author) {
if (!this.message.deferred) await this.message.deferReply().catch(e => {});
if (!this.message.deferred) await this.message.deferReply().catch(e => { });
this.message.author = this.message.user;
this.options.isSlashGame = true;
}
Expand All @@ -81,11 +102,11 @@ module.exports = class TwoZeroFourEight extends events {


const embed = new EmbedBuilder()
.setTitle(this.options.embed.title)
.setColor(this.options.embed.color)
.setImage('attachment://gameboard.png')
.addFields({ name: 'Current Score', value: this.score.toString() })
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });
.setTitle(this.options.embed.title)
.setColor(this.options.embed.color)
.setImage('attachment://gameboard.png')
.addFields({ name: 'Current Score', value: this.score.toString() })
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });


const up = new ButtonBuilder().setEmoji(this.options.emojis.up).setStyle(this.options.buttonStyle).setCustomId('2048_up');
Expand Down Expand Up @@ -115,7 +136,7 @@ module.exports = class TwoZeroFourEight extends events {


collector.on('collect', async btn => {
await btn.deferUpdate().catch(e => {});
await btn.deferUpdate().catch(e => { });
if (btn.user.id !== this.message.author.id) {
if (this.options.playerOnlyMessage) btn.followUp({ content: formatMessage(this.options, 'playerOnlyMessage'), ephemeral: true });
return;
Expand All @@ -132,11 +153,11 @@ module.exports = class TwoZeroFourEight extends events {


const embed = new EmbedBuilder()
.setTitle(this.options.embed.title)
.setColor(this.options.embed.color)
.setImage('attachment://gameboard.png')
.addFields({ name: 'Current Score', value: this.score.toString() })
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });
.setTitle(this.options.embed.title)
.setColor(this.options.embed.color)
.setImage('attachment://gameboard.png')
.addFields({ name: 'Current Score', value: this.score.toString() })
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });

return msg.edit({ embeds: [embed], files: [await this.getBoardImage()], attachments: [] });
})
Expand All @@ -154,11 +175,11 @@ module.exports = class TwoZeroFourEight extends events {
this.emit('gameOver', { result: (result ? 'win' : 'lose'), ...TwoZeroFourEightGame });

const embed = new EmbedBuilder()
.setTitle(this.options.embed.title)
.setColor(this.options.embed.color)
.setImage('attachment://gameboard.png')
.addFields({ name: 'Total Score', value: this.score.toString() })
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });
.setTitle(this.options.embed.title)
.setColor(this.options.embed.color)
.setImage('attachment://gameboard.png')
.addFields({ name: 'Total Score', value: this.score.toString() })
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });

return msg.edit({ embeds: [embed], components: disableButtons(msg.components), files: [await this.getBoardImage()], attachments: [] });
}
Expand All @@ -174,7 +195,7 @@ module.exports = class TwoZeroFourEight extends events {
const posNum = this.gameBoard[y * this.length + x];

['down', 'left', 'right', 'up'].forEach(dir => {
const newPos = move({x, y}, dir);
const newPos = move({ x, y }, dir);
if (this.isInsideBlock(newPos) && (this.gameBoard[newPos.y * this.length + newPos.x] === 0 || this.gameBoard[newPos.y * this.length + newPos.x] === posNum)) numMoves++;
})
}
Expand Down
34 changes: 17 additions & 17 deletions src/MatchPairs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ module.exports = class MatchPairs extends events {
if (!options.timeoutTime) options.timeoutTime = 60000;
if (!options.emojis) options.emojis = ['🍉', '🍇', '🍊', '🍋', '🥭', '🍎', '🍏', '🥝', '🥥', '🍓', '🍒', '🫐', '🍍', '🍅', '🍐', '🥔', '🌽', '🥕', '🥬', '🥦'];
if (!options.winMessage) options.winMessage = '**You won the Game! You turned a total of `{tilesTurned}` tiles.**';
if (!options.loseMessage) options.loseMessage = '**You lost the Game! You turned a total of `{tilesTurned}` tiles.**';
if (!options.loseMessage) options.loseMessage = '**You lost the Game! You turned a total of `{tilesTurned}` tiles.**';


if (typeof options.embed !== 'object') throw new TypeError('INVALID_EMBED: embed option must be an object.');
if (typeof options.embed.title !== 'string') throw new TypeError('INVALID_EMBED: embed title must be a string.');
Expand All @@ -37,7 +37,7 @@ module.exports = class MatchPairs extends events {
if (typeof options.playerOnlyMessage !== 'string') throw new TypeError('INVALID_MESSAGE: playerOnly Message option must be a string.');
}


super();
this.options = options;
this.message = options.message;
Expand All @@ -58,22 +58,22 @@ module.exports = class MatchPairs extends events {

async startGame() {
if (this.options.isSlashGame || !this.message.author) {
if (!this.message.deferred) await this.message.deferReply().catch(e => {});
if (!this.message.deferred) await this.message.deferReply().catch(e => { });
this.message.author = this.message.user;
this.options.isSlashGame = true;
}

this.emojis = shuffleArray(this.emojis).slice(0, 12);
this.emojis.push(...this.emojis, '🃏');
this.emojis = shuffleArray(this.emojis);
this.components = this.getComponents();


const embed = new EmbedBuilder()
.setColor(this.options.embed.color)
.setTitle(this.options.embed.title)
.setDescription(this.options.embed.description)
.setAuthor({ name: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });
.setColor(this.options.embed.color)
.setTitle(this.options.embed.title)
.setDescription(this.options.embed.description)
.setAuthor({ name: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });

const msg = await this.sendMessage({ embeds: [embed], components: this.components });
return this.handleButtons(msg);
Expand Down Expand Up @@ -113,10 +113,10 @@ module.exports = class MatchPairs extends events {


const embed = new EmbedBuilder()
.setColor(this.options.embed.color)
.setTitle(this.options.embed.title)
.setDescription(GameOverMessage.replace('{tilesTurned}', this.tilesTurned))
.setAuthor({ name: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });
.setColor(this.options.embed.color)
.setTitle(this.options.embed.title)
.setDescription(GameOverMessage.replace('{tilesTurned}', this.tilesTurned))
.setAuthor({ name: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });

return msg.edit({ embeds: [embed], components: disableButtons(this.components) });
}
Expand All @@ -126,7 +126,7 @@ module.exports = class MatchPairs extends events {
const collector = msg.createMessageComponentCollector({ idle: this.options.time });

collector.on('collect', async btn => {
await btn.deferUpdate().catch(e => {});
await btn.deferUpdate().catch(e => { });
if (btn.user.id !== this.message.author.id) {
if (this.options.playerOnlyMessage) btn.followUp({ content: formatMessage(this.options, 'playerOnlyMessage'), ephemeral: true });
return;
Expand All @@ -140,7 +140,7 @@ module.exports = class MatchPairs extends events {
const emojiBtn = this.components[y].components[x];
this.tilesTurned += 1;


if (!this.selected) {
this.selected = { x: x, y: y, id: id };
emojiBtn.setEmoji(emoji).setStyle('PRIMARY').removeLabel();
Expand Down Expand Up @@ -171,7 +171,7 @@ module.exports = class MatchPairs extends events {
await msg.edit({ components: this.components });
emojiBtn.removeEmoji().setStyle('SECONDARY').setLabel('\u200b');
selectedBtn.removeEmoji().setStyle('SECONDARY').setLabel('\u200b');
return this.selected = null;;
return this.selected = null;
}

this.remainingPairs -= 1;
Expand All @@ -182,7 +182,7 @@ module.exports = class MatchPairs extends events {
if (this.remainingPairs === 0) return collector.stop();
return await msg.edit({ components: this.components });
})


collector.on('end', async (_, reason) => {
if (reason === 'idle') return this.gameOver(msg, false);
Expand Down
Binary file added src/boardImages/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/1024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/2048.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/boardImages/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Discord = require('discord.js');
const client = new Discord.Client({ intents: [ 1, 512, 4096, 32768 ] });
const client = new Discord.Client({ intents: [1, 512, 4096, 32768] });
const { Snake } = require('../index');


client.on('messageCreate', async (message) => {
if(message.content === '!snake') {
if (message.content === '!snake') {
const Game = new Snake({
message: message,
isSlashGame: false,
Expand All @@ -16,7 +16,7 @@ client.on('messageCreate', async (message) => {
emojis: {
board: '⬛',
food: '🍎',
up: '⬆️',
up: '⬆️',
down: '⬇️',
left: '⬅️',
right: '➡️',
Expand All @@ -27,7 +27,7 @@ client.on('messageCreate', async (message) => {
foods: ['🍎', '🍇', '🍊', '🫐', '🥕', '🥝', '🌽'],
playerOnlyMessage: 'Only {player} can use these buttons.'
});

Game.startGame();
Game.on('gameOver', result => {
console.log(result);
Expand Down

0 comments on commit 344bd95

Please sign in to comment.