Last active
May 11, 2021 14:47
-
-
Save AykutSarac/ab7a47807cb9a7850597f319b7dabb51 to your computer and use it in GitHub Desktop.
Discord 3x3 Grid Guess Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
- NOTE TO THE READER - | |
This is the Discord bot command for making 3x3 grid guessing game. | |
□ □ □ | |
□ □ □ | |
□ □ □ | |
User guessing a position and responding in following structure "y x" | |
which y is row and x is column (e.g: 2 3 aka 2nd row 3th column) and it | |
converts white_square emoji into red_square or green_square depending | |
on the answer from randomly generated position. | |
*/ | |
const { MessageEmbed } = require("discord.js-light"); | |
module.exports = { | |
name: 'guessthebox', | |
category: 'game', | |
cooldown: '600', | |
usage: "```geiger``` Choose a position from grid to catch!", | |
execute(message) { | |
const rng = () => Math.floor(Math.random() * 3) + 1; | |
const randomPos = [rng(), rng()]; | |
const filter = response => { | |
return response.content && (response.author.id === message.author.id); | |
}; | |
// We're creating our table here | |
let table = [ | |
[1, 1], [1, 2], [1, 3], | |
[2, 1], [2, 2], [2, 3], | |
[3, 1], [3, 2], [3, 3] | |
] | |
const embed = new MessageEmbed() | |
.setDescription(table.map((el, i) => (i % 3 === 0 && i !== 0 ? '\n' : '') + ':white_medium_square:').join(' ')) | |
.setFooter('Pick a box and respond in format: "3 2"'); | |
message.channel.send(embed).then(mes => { | |
mes.channel.awaitMessages(filter, { max: 1, time: 10000, errors: ['time'] }) | |
.then(collected => { | |
let answer = collected.first().content.split(' ').map(el => parseInt(el)); | |
if (answer[0] === randomPos[0] && answer[1] === randomPos[1]) { | |
// Random coin prizes | |
const rewards = [2000, 1500, 1200, 3800, 4600, 5700, 3000, 5000, 8000, 12000, 666, 16000, 14500, 22000, 4200, 4800, 7900, 14000, 27000, 13500]; | |
const randomReward = rewards[Math.floor(Math.random() * rewards.length)]; | |
embed.setDescription(table.map((el, i) => { | |
// Generate new lines | |
const blank = (i % 3 === 0 && i !== 0 ? '\n' : ''); | |
if (el[0] === randomPos[0] && el[1] === randomPos[1]) return blank + ':green_square:'; | |
return blank + ':white_medium_square:'; | |
}).join(' ')); | |
mes.edit(embed); | |
message.reply('wooho, good job you did it! I reward you with **' + randomReward.toLocaleString() + ' Gems**!'); | |
} else { | |
throw answer; | |
} | |
}) | |
.catch((answer) => { | |
embed.setDescription(table.map((el, i) => { | |
const blank = (i % 3 === 0 && i !== 0 ? '\n' : ''); | |
if (el[0] === answer[0] && el[1] === answer[1]) return blank + ':red_square:'; | |
if (el[0] === randomPos[0] && el[1] === randomPos[1]) return blank + ':green_square:'; | |
return blank + ':white_medium_square:'; | |
}).join(' ')); | |
mes.edit(embed); | |
let randomMsg = ['Next time maybe?', 'You couldn\'t hit the jackpot this time, captain!', 'What do you call, bad luck?', 'That was close!', 'One day mate, one day...']; | |
message.channel.send(randomMsg[Math.floor(Math.random() * randomMsg.length)]); | |
}) | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment