RockPaperScissors/index.js
2021-11-30 22:03:51 +01:00

136 lines
5.9 KiB
JavaScript

const dc = require("discord.js");
const client = new dc.Client({intents: ["GUILDS"]});
const fs = require("fs");
var shapes = ["rock", "paper", "scissors"];
function rps(shape){
shape = shape.toLowerCase();
var chosenShape = shapes[Math.floor(Math.random()*shapes.length)];
var beats = {
paper: "scissors",
scissors: "rock",
rock: "paper"
}
if(beats[chosenShape] == shape) return "Win";
if(beats[shape] == chosenShape) return "Loose";
return "Tie";
}
client.on("ready", () => {
console.log("Ready!");
client.guilds.cache.get("733451782063390790").commands.set([
{
name: "ping",
description: "Shows my latency xDDDDDDDD"
},
{
name: "rps",
description: "Rock paper scissors in a nutshell.",
options: [
{
name: "firstmove",
description: "First move, yk to start it all off",
type: 3,
required: true
}
]
}
]);
});
client.on("interactionCreate", (int) => {
if(int.isCommand()){
if(int.commandName == "ping"){
int.reply(`Pong! (${client.ws.ping} ms)`);
} else if(int.commandName == "rps"){
var shape = int.options.getString("firstmove").toLowerCase();
if(!shapes.includes(shape)) return int.reply("That isn't a valid shape.");
var msgRow = new dc.MessageActionRow();
var rockButton = new dc.MessageButton();
rockButton.setCustomId("rock");
rockButton.setLabel("Rock");
rockButton.setEmoji(int.guild.emojis.cache.get("<:rock:915328258126528582>"));
rockButton.setStyle("PRIMARY");
var paperButton = new dc.MessageButton();
paperButton.setCustomId("paper");
paperButton.setLabel("Paper");
paperButton.setEmoji(int.guild.emojis.cache.get("<:paper:915328929294864494>"));
paperButton.setStyle("PRIMARY");
var scissorsButton = new dc.MessageButton();
scissorsButton.setCustomId("scissors");
scissorsButton.setLabel("Scissors");
scissorsButton.setEmoji(int.guild.emojis.cache.get("<:scissors:915329041958043648>"));
scissorsButton.setStyle("PRIMARY");
msgRow.addComponents(rockButton);
msgRow.addComponents(paperButton);
msgRow.addComponents(scissorsButton);
var embed = new dc.MessageEmbed();
embed.setFooter("rps");
embed.addField("Moves", shape);
var status = rps(shape);
if(status == "Win"){
embed.setColor("#00FF00");
embed.setTitle("Win");
embed.setDescription("You insta-won the game!");
} else if(status == "Loose"){
embed.setColor("#FF0000");
embed.setTitle("Lose");
embed.setDescription("You insta-lost the game, better luck next time!");
} else if(status == "Tie"){
embed.setColor("LIGHT_GREY");
embed.setTitle("Tie");
embed.setDescription("And the games continue, this game is currently on tie, press a button to continue.");
}
int.reply({embeds: [embed], components: [msgRow]});
}
} else if(int.isButton()){
console.log("Button detected");
if(!int.message.embeds[0]) return console.log("Nu embed");
var msgEmbed = int.message.embeds[0];
if(msgEmbed.footer.text == "rps"){
var shape = int.customId;
if(msgEmbed.title !== "Tie") return int.reply("This game has already ended!");
var msgRow = new dc.MessageActionRow();
var rockButton = new dc.MessageButton();
rockButton.setCustomId("rock");
rockButton.setLabel("Rock");
rockButton.setEmoji(int.guild.emojis.cache.get("<:rock:915328258126528582>"));
rockButton.setStyle("PRIMARY");
var paperButton = new dc.MessageButton();
paperButton.setCustomId("paper");
paperButton.setLabel("Paper");
paperButton.setEmoji(int.guild.emojis.cache.get("<:paper:915328929294864494>"));
paperButton.setStyle("PRIMARY");
var scissorsButton = new dc.MessageButton();
scissorsButton.setCustomId("scissors");
scissorsButton.setLabel("Scissors");
scissorsButton.setEmoji(int.guild.emojis.cache.get("<:scissors:915329041958043648>"));
scissorsButton.setStyle("PRIMARY");
msgRow.addComponents(rockButton);
msgRow.addComponents(paperButton);
msgRow.addComponents(scissorsButton);
var embed = new dc.MessageEmbed();
embed.setFooter("rps");
var moves = msgEmbed.fields[0].value + " -> " + shape;
embed.addField("Moves", moves);
var status = rps(shape);
if(status == "Win"){
embed.setColor("#00FF00");
embed.setTitle("Win");
embed.setDescription("You win the game!");
} else if(status == "Loose"){
embed.setColor("#FF0000");
embed.setTitle("Lose");
embed.setDescription("You lost the game, better luck next time!");
} else if(status == "Tie"){
embed.setColor("LIGHT_GREY");
embed.setTitle("Tie");
embed.setDescription("And the games continue, this game is currently on tie, press a button to continue.");
}
int.message.edit({embeds: [embed], components: [msgRow]});
int.update(int);
} else console.log(msgEmbed.footer);
}
});
client.login(JSON.parse(fs.readFileSync(".token").toString()));