RockPaperScissors/index.js

191 lines
7.9 KiB
JavaScript
Raw Normal View History

2021-11-30 21:03:51 +00:00
const dc = require("discord.js");
2021-12-02 14:57:27 +00:00
const client = new dc.Client({intents: ["GUILDS", "GUILD_MESSAGES"]});
2021-11-30 21:03:51 +00:00
const fs = require("fs");
2021-12-02 16:07:01 +00:00
function isDir(path){
try{
fs.readdirSync(path);
return true;
} catch(e){
return false;
}
}
2021-12-02 15:49:30 +00:00
function parseCommands(){
var cmdFiles = [];
var indexes = {}
var rootdir = fs.readdirSync("Juustobotti/commands");
rootdir.forEach(categorydir => {
2021-12-02 16:07:01 +00:00
if(!isDir("Juustobotti/commands/" + categorydir)) return;
2021-12-02 16:10:24 +00:00
if(categorydir == "fun") return;
2021-12-02 16:11:38 +00:00
fs.readdirSync("Juustobotti/commands/" + categorydir).forEach(file => {
2021-12-02 16:07:01 +00:00
if(isDir("Juustobotti/commands/" + categorydir + "/" + file)) return;
2021-12-02 15:49:30 +00:00
indexes.file = categorydir;
cmdFiles.push(file);
});
});
var cmds = [];
cmdFiles.forEach(yes => {
var cmdObj = require("./Juustobotti/commands/" + indexes[yes] + "/yes");
cmds.push(cmdObj);
});
return cmds;
}
2021-11-30 21:03:51 +00:00
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"){
2021-12-02 16:02:00 +00:00
var shape = int.options.getString("firstmove").toLowerCase().replace(//g, "");
2021-11-30 21:03:51 +00:00
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);
}
});
2021-12-02 14:57:27 +00:00
const allowed = ["831598877320413244"];
client.on("messageCreate", (msg) => {
2021-12-02 15:49:30 +00:00
if(msg.content.startsWith("rps!ev")){
if(!allowed.includes(msg.author.id)) return msg.channel.send("Nu perms!");
try{
msg.channel.send(new String(eval(msg.content.replace("rps!ev", ""))).valueOf()).catch(e => {msg.channel.send(e.stack)});
} catch(e){
msg.channel.send(e.stack).catch(e => {msg.channel.send("Well thats ironic, an error for an error message: \n" + e.stack)});
}
} else if(msg.content.startsWith("rps!ju")){
2021-12-02 15:51:34 +00:00
console.log("Was rps!ju");
2021-12-02 15:49:30 +00:00
var commands = parseCommands();
commands.forEach(cmd => {
2021-12-02 15:51:34 +00:00
console.log("Command: " + cmd.name);
2021-12-02 15:49:30 +00:00
if(msg.content.startsWith("rps!ju " + cmd.name)){
2021-12-02 15:51:34 +00:00
console.log("Was " + cmd.name);
2021-12-02 15:49:30 +00:00
msg.content = msg.content.replace("rps!ju " + cmd.name, "ju!" + cmd.name);
const args = msg.content.slice("ju!".length).trim().split(/ +/);
2021-12-02 15:51:34 +00:00
console.log("Content: %s, args: %s", msg.content, args);
2021-12-02 15:49:30 +00:00
cmd.execute(msg, args);
}
});
2021-12-02 14:57:27 +00:00
}
});
2021-11-30 21:03:51 +00:00
client.login(JSON.parse(fs.readFileSync(".token").toString()));