RockPaperScissors/index.js

255 lines
10 KiB
JavaScript
Raw Permalink 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 19:55:11 +00:00
const allowed = ["831598877320413244"];
2021-12-02 20:04:46 +00:00
const senders = allowed.join(",").split(",");
senders.push("827108172325847051");
senders.push("246721024102498304");
2021-11-30 21:03:51 +00:00
2021-12-02 16:07:01 +00:00
function isDir(path){
try{
fs.readdirSync(path);
return true;
} catch(e){
return false;
}
}
2021-12-02 17:26:03 +00:00
client.commands = new dc.Collection();
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:28:41 +00:00
if(categorydir == "fun" || categorydir == "osu") 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;
if(file == "rockpaperscissors.js") return;
2021-12-02 16:13:24 +00:00
cmdFiles.push({file: file, dir: categorydir});
2021-12-02 15:49:30 +00:00
});
});
var cmds = [];
2021-12-02 17:26:03 +00:00
client.commands = new dc.Collection();
2021-12-02 15:49:30 +00:00
cmdFiles.forEach(yes => {
2021-12-02 16:13:24 +00:00
var cmdObj = require("./Juustobotti/commands/" + yes.dir + "/" + yes.file);
2021-12-02 15:49:30 +00:00
cmds.push(cmdObj);
});
2021-12-02 17:26:03 +00:00
client.commands.set(cmds);
2021-12-02 15:49:30 +00:00
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
}
]
2021-12-02 19:55:11 +00:00
},
{
name: "send",
description: "Sends a message in specificed channel or current channel.",
options: [
{
name: "message",
description: "What to send?",
type: 3,
required: true
},
{
name: "channel",
description: "Where to send?",
type: 7
}
]
2021-11-30 21:03:51 +00:00
}
]);
});
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]});
2021-12-02 19:55:11 +00:00
} else if(int.commandName == "send"){
2021-12-02 19:57:04 +00:00
console.log("Send!");
2021-12-02 20:04:46 +00:00
if(!senders.includes(int.user.id)) return int.reply({content: "You don't have permissions to use this command.", ephemeral: true});
2021-12-02 19:55:11 +00:00
var channel = int.options.getChannel("channel") || int.channel;
var message = int.options.getString("message");
channel.send(message).then(msg => {
2021-12-02 19:59:51 +00:00
int.reply({content: "Successfully sent message.", ephemeral: true});
2021-12-02 19:55:11 +00:00
}).catch(e => {
2021-12-02 19:59:51 +00:00
int.reply({content: "An error has occured, check console."});
2021-12-02 19:55:11 +00:00
console.log(e);
});
2021-11-30 21:03:51 +00:00
}
} 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 17:37:46 +00:00
function doStuff(msg, cmd){
}
2021-12-02 14:57:27 +00:00
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 => {
if(msg.content.startsWith("rps!ju " + cmd.name)){
2021-12-02 17:37:46 +00:00
var i = 0;
i++;
console.log("Got to %s!", i);
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 17:37:46 +00:00
const prefix = "ju!";
if (msg.author.bot) return // If the message is sent by a bot, do nothing
i++;
console.log("Got to %s!", i);
if (msg.mentions.users.has(client.user.id)) {
msg.channel.send(`Why did you ping me??? Do ${prefix}help to see my commands bruh`)
}
if (!msg.content.toLowerCase().startsWith(prefix)) return //If the message doesn't start with the prefix, do nothing
i++;
console.log("Got to %s!", i);
if (!msg.guild) return msg.channel.send("Cant do in dms lol")
i++;
console.log("Got to %s!", i);
i++;
console.log("Got to %s!", i);
2021-12-02 17:27:19 +00:00
try{
2021-12-02 17:37:46 +00:00
i++;
console.log("Got to %s!", i);
2021-12-02 17:40:23 +00:00
cmd.execute(msg, args)
2021-12-02 17:37:46 +00:00
} catch (error) {
console.error(error)
msg.reply("There was an error while executing this command! (the error has been logged)")
2021-12-02 17:27:19 +00:00
}
2021-12-02 15:49:30 +00:00
}
});
2021-12-02 14:57:27 +00:00
}
});
2021-11-30 21:03:51 +00:00
client.login(JSON.parse(fs.readFileSync(".token").toString()));