Skip to content

Instantly share code, notes, and snippets.

@Hri7566
Created September 11, 2022 02:41
Show Gist options
  • Select an option

  • Save Hri7566/39c7240ecae7841fcfa68ca948f8b7d2 to your computer and use it in GitHub Desktop.

Select an option

Save Hri7566/39c7240ecae7841fcfa68ca948f8b7d2 to your computer and use it in GitHub Desktop.
var fs = require('fs');
fs.readFile("./ops", function(err, data) {
if(err) {
console.log(err);
data = "[]"
}
var ops = JSON.parse(data);
fs.readFile("./bans", function(err, data) {
if(err) {
console.log(err);
data = "[]";
}
var bans = JSON.parse(data);
runTheBot({ops: ops, bans: bans});
});
});
function runTheBot(params) {
var ops = params.ops;
var bans = params.bans;
var Client = require("./Client.js");
var client = new Client("ws://www.multiplayerpiano.com");
client.setChannel("totoro");
client.start();
var WAIT_MS = 3000;
var last_known_channel = undefined;
var wait_until = Infinity;
function receiveChat(msg) {
process.stdout.write(
//new Date(msg.t).toString()+"\n"+
msg.p.name+":\n"+
"\t"+msg.a+"\n");
};
client.on("c", function(msg) {
for(var i = 0; i < msg.c.length; i++) {
receiveChat(msg.c[i]);
}
});
var lvltmp = new Array();
var healthtmp = new Array();
function youwin(name1, name2, color, total) {
var saythis = name1 + " was slain by " + name2;
if (Math.random() < (total - 1.0 * lvltmp[color]) / (total + lvltmp[color] * 2)) {
lvltmp[color] = lvltmp[color] + 1;
//saythis += ", and " + name2 + " levelled up to level " + lvltmp[color];
}
client.sendArray([{m: "a", message: saythis}]);
}
var WAIT_MS = 3000;
var last_known_channel = undefined;
var wait_until = Infinity;
var welcTemp = false;
var disabled = true;
var banned = [];
client.on("ch", function (msg) {
if (msg.ch._id !== last_known_channel) {
last_known_channel = msg.ch._id;
wait_until = Date.now() + WAIT_MS;
}
});
client.on("participant added", function (part) {
if (Date.now() > wait_until) {
setTimeout(function () {
if (Date.now() > wait_until) {
if (welcTemp) {
client.sendArray([{m: "a", message: "Welcome " + part.name + ", Please use /help for a list of commands!"}]);
} else {
}
}
}, 100);
}
});
client.on("a", function (msg) {
receiveChat(msg);
var commands = ["poke", "gift", "grouphug", "dice (/roll)", "color (/colour)", "hug", "kiss", "cuddle", "online", "slap", "facepalm", "afk", "duel"];
var admincmds = ["rename", "welcome", "ban", "pardon", "op", "deop"];
var message = msg.a;
var name = msg.p.name;
var names = msg.p;
var speed = 6;
var color = msg.p.color;
var ids = msg.p._id
var matched = false;
for (i = 0; i < banned.length; i++) {
if (banned[i] == ids)
matched = true;
}
var opped = ops.indexOf(ids) !== -1;
if (message.substring(0,9).toLowerCase() == "/commands") {
if (opped) {
if (!disabled) {
disabled = !disabled;
client.sendArray([{m: "a", message: "Commands enabled!"}]);
} else {
disabled = !disabled;
client.sendArray([{m: "a", message: "Commands disabled!"}]);
}
} else if (disabled && !opped) {
client.sendArray([{m: "a", message: "You are not authorized to use this command!"}]);
}
} else if (disabled) {
if (message.substring(0,1) == "/" && matched) {
client.sendArray([{m: "a", message: "You may not use commands because you are banned!"}]);
} else if (message.substring(0,1) == "/" && name.substring(0, 1) == "/") {
client.sendArray([{m: "a", message: "You may not use commands because you have / at the beginning of your name!"}]);
} else if (message.substring(0,5).toLowerCase() == "/test") {
client.sendArray([{m: "a", message: "Ops: " + ops}]);
} else if (message.substring(0,5).toLowerCase() == "/help") {
var cmds = "/" + commands[0];
var adminstring = "/" + admincmds[0];
for (i = 1; i < commands.length; i++) {
cmds += ", /" + commands[i];
}
for (o = 1; o < admincmds.length; o++) {
adminstring += ", /" + admincmds[o];
}
if (opped) {
client.sendArray([{m: "a", message: "Commands are: " + cmds}]);
client.sendArray([{m: "a", message: "Command bot admin commands are: " + adminstring}]);
} else {
client.sendArray([{m: "a", message: "Commands are: " + cmds}]);
}
} else if (message.substring(0,9).toLowerCase() == "/grouphug") {
client.sendArray([{m: "a", message: name + " gave everyone a hug!"}])
} else if (message.substring(0,4).toLowerCase() == "/sov") {
client.sendArray([{m: "a", message: "Sov is AWESOME"}]);
} else if (message.substring(0,5).toLowerCase() == "/dice" || message.substring(0, 5).toLowerCase() == "/roll") {
if (message.substring(6,7) == "#") {
speed = message.substring(7).trim();
if (speed == "") {
speed = 0;
}
if (isNaN(speed)) {
client.sendArray([{m: "a", message: "Invalid number"}]);
} else {
client.sendArray([{m: "a", message: name + " rolled a " + Math.floor(Math.random() * speed + 1) + " out of " + speed}]);
}
} else {
client.sendArray([{m: "a", message: name + " rolled a " + Math.floor(Math.random() * speed + 1) + " out of " + speed}]);
speed = 6;
}
} else if (message.substring(0,3).toLowerCase() == "/me") {
if (message.substring(3).trim() == "") {
client.sendArray([{m: "a", message: "Usage: /me action"}])
} else {
client.sendArray([{m: "a", message: "*" + name + " " + message.substring(4).trim() + "*"}]);
}
} else if (message.substring(0,5).toLowerCase() == "/poke") {
var target_name_lowercase = msg.a.substring(5).trim().toLowerCase();
var target_participant = undefined;
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (message.substring(5).trim() == "") {
client.sendArray([{m: "a", message: "Usage: /poke name_here"}]);
} else if (message.substring(5).trim().toLowerCase() == name.toLowerCase()) {
client.sendArray([{m: "a", message: "You cannot poke yourself!"}]);
} else if (target_participant) {
client.sendArray([{m: "a", message: name + " poked " + target_participant.name}]);
} else {
client.sendArray([{m: "a", message: "Could not find anyone to poke by the name of " + msg.a.substring(5).trim()}]);
}
} else if (message.substring(0,5).toLowerCase() == "/kiss") {
var target_name_lowercase = msg.a.substring(5).trim().toLowerCase();
var target_participant = undefined;
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (message.substring(5).trim() == "") {
client.sendArray([{m: "a", message: "Usage: /kiss name_here"}]);
} else if (message.substring(5).trim().toLowerCase() == name.toLowerCase()) {
client.sendArray([{m: "a", message: "You cannot kiss yourself!"}]);
} else if (target_participant) {
client.sendArray([{m: "a", message: name + " gave " + target_participant.name + " a kiss!"}]);
} else {
client.sendArray([{m: "a", message: "Could not find anyone to kiss by the name of " + msg.a.substring(5).trim()}]);
}
} else if (message.substring(0,5).toLowerCase() == "/duel") {
var target_name_lowercase = msg.a.substring(5).trim().toLowerCase();
var target_participant = undefined;
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (message.substring(5).trim() == "") {
client.sendArray([{m: "a", message: "Usage: /duel name_here"}]);
} else if (message.substring(5).trim().toLowerCase() == name.toLowerCase()) {
client.sendArray([{m: "a", message: "You cannot duel yourself!"}]);
} else if (target_participant) {
if (lvltmp[target_participant.color] == undefined)
lvltmp[target_participant.color] = 1;
if (lvltmp[names.color] == undefined)
lvltmp[names.color] = 1;
if (healthtmp[target_participant.color] == undefined)
healthtmp[target_participant.color] = 100;
if (healthtmp[names.color] == undefined)
healthtmp[names.color] = 100;
var total = lvltmp[target_participant.color] + lvltmp[names.color];
client.sendArray([{m: "a", message: target_participant.name + " lvl: " + lvltmp[target_participant.color] + " was challenged to a duel by " + names.name + " lvl: " + lvltmp[names.color] + ""}]);
if (Math.random() < 1.0 * lvltmp[names.color] / total) {
setTimeout(function () {
youwin(target_participant.name, names.name, name.color, total);
}, 4000);
} else {
setTimeout(function () {
youwin(names.name, target_participant.name, target_participant.color, total);
}, 4000);
}
} else {
client.sendArray([{m: "a", message: "Could not find anyone to duel by the name of " + msg.a.substring(5).trim()}]);
}
} else if (message.substring(0,5).toLowerCase() == "/slap") {
var target_name_lowercase = msg.a.substring(5).trim().toLowerCase();
var target_participant = undefined;
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (message.substring(5).trim() == "") {
client.sendArray([{m: "a", message: "Usage: /slap name_here"}]);
} else if (message.substring(5).trim().toLowerCase() == name.toLowerCase()) {
client.sendArray([{m: "a", message: "You cannot slap yourself!"}]);
} else if (target_participant) {
client.sendArray([{m: "a", message: name + " slapped " + target_participant.name}]);
} else {
client.sendArray([{m: "a", message: "Could not find anyone to slap by the name of " + msg.a.substring(5).trim()}]);
}
} else if (message.substring(0,7).toLowerCase() == "/cuddle") {
var target_name_lowercase = msg.a.substring(7).trim().toLowerCase();
var target_participant = undefined;
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (message.substring(5).trim() == "") {
client.sendArray([{m: "a", message: "Usage: /cuddle name_here"}]);
} else if (message.substring(5).trim().toLowerCase() == name.toLowerCase()) {
client.sendArray([{m: "a", message: "You cannot cuddle yourself!"}]);
} else if (target_participant) {
client.sendArray([{m: "a", message: name + " cuddled with " + target_participant.name}]);
} else {
client.sendArray([{m: "a", message: "Could not find anyone to cuddle by the name of " + msg.a.substring(7).trim()}]);
}
} else if (message.substring(0,4).toLowerCase() == "/hug") {
var target_name_lowercase = msg.a.substring(4).trim().toLowerCase();
var target_participant = undefined;
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (message.substring(5).trim() == "") {
client.sendArray([{m: "a", message: "Usage: /hug name_here"}]);
} else if (message.substring(5).trim().toLowerCase() == name.toLowerCase()) {
client.sendArray([{m: "a", message: "You cannot hug yourself!"}]);
} else if (target_participant) {
client.sendArray([{m: "a", message: name + " gave " + target_participant.name + " a hug!"}]);
} else {
client.sendArray([{m: "a", message: "Could not find anyone to hug by the name of " + msg.a.substring(5).trim()}]);
}
} else if (message.substring(0,5).toLowerCase() == "/gift") {
var giftArray = [
'a cute kitten!',
'a cute puppy!',
'a potato!',
'a pikachu!',
'a brand new Xbox One!',
'a brand new PS4!',
'1 million cows!',
'a slice of bacon!',
'a boating license!',
'a bunch of money!',
'some cake!',
'a box inside of a... BOX!',
'a new phone!',
'a new piano!',
'some chicken nuggets!',
'a brand new Wii U!',
'an evil gerbil!',
'a captains ship!',
'some sfoof!',
'some pizza!',
'a new laptop!'
];
var randomNumber = Math.floor(Math.random() * giftArray.length);
var target_name_lowercase = msg.a.substring(5).trim().toLowerCase();
var target_participant = undefined;
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (message.substring(5).trim() == "") {
client.sendArray([{m: "a", message: "Usage: /gift name_here"}]);
} else if (message.substring(5).trim().toLowerCase() == name.toLowerCase()) {
client.sendArray([{m: "a", message: "You cannot gift yourself!"}]);
} else if (target_participant) {
client.sendArray([{m: "a", message: name + " gave " + target_participant.name + " " + giftArray[randomNumber]}]);
} else {
client.sendArray([{m: "a", message: "Could not find anyone to gift by the name of " + msg.a.substring(5).trim()}]);
}
} else if (message.substring(0,6).toLowerCase() == "/color" || message.substring(0, 7).toLowerCase() == "/colour") {
var target_name_lowercase = msg.a.substring(7).trim().toLowerCase();
var target_participant = undefined;
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (target_participant) {
client.sendArray([{m: target_participant.name + "'s color is: " + target_participant.color}]);
console.log(target_participant);
} else {
client.sendArray([{m: "a", message: "Your color is: " + color}]);
}
} else if (message.substring(0,4).toLowerCase() == "/afk") {
client.sendArray([{m: "a", message: name + " is now AFK!"}]);
} else if (message.substring(0,7).toLowerCase() == "/online") {
{
nick = $('#names .name');
var nickOnline = "";
for (i = 0; i < nick.length; i++) {
nickOnline += " | " + nick[i].textContent;
}
client.sendArray([{m: "a", message: "Online: " + nickOnline + " |"}]);
}
} else if (message.substring(0,9).toLowerCase() == "/facepalm") {
client.sendArray([{m: "a", message: name + " facepalmed"}]);
// op commands
} else if (message.substring(0,8).toLowerCase() == "/kaleigh") {
if (opped) {
client.sendArray([{m: "a", message: "Josh loves Kaleigh ❤❤❤"}]);
} else {
}
} else if (message.substring(0,7).toLowerCase() == "/rename") {
if (opped) {
client.sendArray([{
"m": "userset",
"set": {
"name": msg.a.substring(7).trim()
}
}]);
} else {
client.sendArray([{m: "a", message: "You are not authorized to use this command!"}]);
}
} else if (message.substring(0,8).toLowerCase() == "/welcome") {
if (opped) {
if (!welcTemp) {
welcTemp = !welcTemp;
client.sendArray([{m: "a", message: "Welcoming enabled!"}]);
} else {
welcTemp = !welcTemp;
client.sendArray([{m: "a", message: "Welcoming disabled!"}]);
}
} else {
client.sendArray([{m: "a", message: "You are not authorized to use this command!"}]);
}
} else if (message.substring(0,4).toLowerCase() == "/ban") {
var target_name_lowercase = msg.a.substring(5).trim().toLowerCase();
var target_participant = undefined;
if (opped) {
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (target_participant) {
banned.push(target_participant._id);
} else {
client.sendArray([{m: "a", message: "Could not find anyone to ban by the name of " + msg.a.substring(5).trim()}]);
}
} else {
client.sendArray([{m: "a", message: "You are not authorized to use this command!"}]);
}
} else if (message.substring(0,7).toLowerCase() == "/pardon") {
var target_name_lowercase = msg.a.substring(8).trim().toLowerCase();
var target_participant = undefined;
if (opped) {
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (target_participant) {
while (banned.indexOf(target_participant._id) !== -1) {
banned.splice(banned.indexOf(target_participant._id), 1);
}
while (banned.indexOf(target_participant._id) !== -1) {
delete banned[banned.indexOf(target_participant._id)];
}
} else {
client.sendArray([{m: "a", message: "Could not find anyone to pardon by the name of " + msg.a.substring(8).trim()}]);
}
} else {
client.sendArray([{m: "a", message: "You are not authorized to use this command!"}]);
}
} else if (message.substring(0,3).toLowerCase() == "/op") {
var target_name_lowercase = msg.a.substring(3).trim().toLowerCase();
var target_participant = undefined;
if (!opped) {
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (target_participant) {
ops.push(target_participant._id); fs.writeFile("./ops", JSON.stringify(ops));
} else {
client.sendArray([{m: "a", message: "Could not find anyone to op by the name of " + msg.a.substring(4).trim()}]);
}
} else {
client.sendArray([{m: "a", message: "You are not authorized to use this command!"}]);
}
} else if (message.substring(0,5).toLowerCase() == "/deop") {
var target_name_lowercase = msg.a.substring(6).trim().toLowerCase();
var target_participant = undefined;
if (opped) {
for (var id in client.ppl) {
if (!client.ppl.hasOwnProperty(id)) continue;
var part = client.ppl[id];
if (part.name.toLowerCase() === target_name_lowercase) {
target_participant = part;
break;
}
}
if (target_participant) {
if (target_participant._id == "59dcad53d48a5ecf2e28bc5c") {
client.sendArray([{m: "a", message: "You cannot deop electrashave!"}]);
} else {
while (OP.indexOf(target_participant._id) !== -1) {
OP.splice(banned.indexOf(target_participant._id), 1);
}
while (OP.indexOf(target_participant._id) !== -1) {
delete OP[OP.indexOf(target_participant._id)];
}
}
} else {
client.sendArray([{m: "a", message: "Could not find anyone to deop by the name of " + msg.a.substring(6).trim()}]);
}
} else {
client.sendArray([{m: "a", message: "You are not authorized to use this command!"}]);
}
} else if (message.substring(0,1) == "/") {
client.sendArray([{m: "a", message: "Invalid Command. Please use /help for a list of commands"}]);
}
}
});
var iface = require("readline").createInterface({input: process.stdin, output: process.stdout});
iface.on("line", function(line) {
client.sendArray([{m: "a", message: line}]);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment