mpp-server-dev2/oldsrc/InternalBot/Command.js

302 lines
8.0 KiB
JavaScript
Raw Normal View History

2023-01-15 01:48:04 +01:00
const Logger = require("../Logger");
2023-03-31 16:43:24 +02:00
const Color = require("../Color");
const { Cow } = require("../Cow");
2023-04-01 07:05:12 +02:00
const Database = require("../Database");
2023-01-15 01:48:04 +01:00
2022-08-16 15:42:09 +02:00
class Command {
2023-03-31 16:43:24 +02:00
static commands = [];
static logger = new Logger("Command Handler");
static handleCommand(cl, ch, c, usedPrefix, args, argcat, p, isAdmin) {
for (let cmd of this.commands) {
let aliasCheck = false;
aliasLoop: for (let alias of cmd.aliases) {
if (c.toLowerCase() == alias.toLowerCase()) {
aliasCheck = true;
break aliasLoop;
}
}
if (!aliasCheck) continue;
if (!isAdmin && cmd.permLevel == "admin")
return ch.adminChat(
`You don't have permission to use this command.`
);
if (args.length - 1 < cmd.minargs)
return ch.adminChat(
`Not enough arguments. Usage: ${this.getUsage(
cmd.usage,
usedPrefix
)}`
);
try {
const out = cmd.func(cl, ch, {
c,
args,
argcat,
p,
isAdmin,
a: args.join(" ")
});
2023-04-01 07:05:12 +02:00
// console.log(out);
2023-03-31 16:43:24 +02:00
if (!out) return;
if (out !== "") {
ch.adminChat(out);
}
} catch (err) {
this.logger.error(err);
ch.adminChat(
`An error has occurred whilst performing this command.`
);
}
}
}
static addCommand(cmd) {
this.commands.push(cmd);
}
static getUsage(usa, pre) {
return usa.split("%P").join(pre);
}
2023-01-15 01:48:04 +01:00
constructor(id, aliases, desc, usage, minargs, func, permLevel) {
2022-08-16 15:42:09 +02:00
this.id = id;
2023-01-15 01:48:04 +01:00
this.aliases = aliases || [id];
2023-03-31 16:43:24 +02:00
this.desc = desc || "no description"; // brandon-like words
this.usage = usage || "no usage";
this.minargs = minargs;
2022-08-16 15:42:09 +02:00
this.func = func;
2023-03-31 16:43:24 +02:00
this.permLevel = permLevel || "admin"; // user / admin?
2022-08-16 15:42:09 +02:00
}
}
2023-03-31 16:43:24 +02:00
Command.addCommand(
new Command(
"ping",
["ping"],
undefined,
`%Pping`,
0,
(cl, ch, msg) => {
return `pong`;
},
"user"
)
);
Command.addCommand(
new Command(
"color",
["color", "setcolor", "colorset"],
undefined,
`%Pcolor [color] [userid]`,
0,
(cl, ch, msg) => {
if (!msg.isAdmin) {
ch.adminChat("You do not have permission to use this command.");
return;
}
let color = ch.verifyColor(msg.args[1]);
if (color) {
let c = new Color(color);
if (!msg.args[2]) {
cl.emit(
"color",
2023-04-17 01:34:32 +02:00
{ color: c.toHexa(), _id: cl.user._id },
2023-03-31 16:43:24 +02:00
true
);
ch.adminChat(
`Your color is now ${c
.getName()
.replace("A", "a")} [${c.toHexa()}]`
);
} else {
let winner = ch.server.getAllClientsByUserID(
msg.args[2]
)[0];
if (winner) {
cl.emit(
"color",
2023-04-17 01:34:32 +02:00
{ color: c.toHexa(), _id: winner.user._id },
2023-03-31 16:43:24 +02:00
true
);
ch.adminChat(
`Friend ${winner.user.name}'s color is now ${c
.getName()
.replace("A", "a")}.`
);
} else {
ch.adminChat(
"The friend you are looking for (" +
msg.args[2] +
") is not around."
);
}
}
} else {
ch.adminChat("Invalid color.");
}
ch.updateCh();
},
"user"
)
);
Command.addCommand(
new Command(
"chlist",
["chlist"],
undefined,
`%Pchlist`,
0,
(cl, ch, msg) => {
if (!msg.isAdmin) {
ch.adminChat("You do not have permission to use this command.");
return;
}
ch.adminChat("Channels:");
for (const cc of cl.server.channels.values()) {
ch.adminChat(`- ${cc._id}\n`);
}
},
"admin"
)
);
Command.addCommand(
new Command(
"cow",
["cow"],
undefined,
`%Pcow`,
0,
(cl, ch, msg) => {
const cow = new Cow();
return `Cow: ${cow.emoji}${cow.display_name}`;
},
"user"
)
);
2023-01-15 01:48:04 +01:00
2023-04-01 07:05:12 +02:00
Command.addCommand(
new Command(
"inventory",
["inventory", "inv"],
undefined,
`%Pinventory`,
0,
(cl, ch, msg) => {
if (cl.user.inventory) {
const items = Object.values(cl.user.inventory)
.map(
it =>
`${it.emoji ? it.emoji : ""}${it.display_name} (x${
it.count
})`
)
.join(", ")
.trim();
ch.adminChat(`Inventory: ${items == "" ? "(none)" : items}`);
}
},
"user"
)
);
Command.addCommand(
new Command(
"js",
["js"],
undefined,
`%Pjs`,
0,
(cl, ch, msg) => {
return cl.server.ev(msg.argcat);
},
"admin"
)
);
2023-04-17 01:34:32 +02:00
/*
Command.addCommand(
new Command(
"ip",
["ip"],
undefined,
"%Pip",
0,
(cl, ch, msg) => {
if (msg.args[1]) {
const winner = new Array(cl.server.connections.values()).find(
cl => {
if (!cl.user) return false;
console.log(cl.user._id);
return cl.user ? cl.user._id == msg.args[1] : false;
}
);
if (winner) {
cl.sendArray([
{
m: "a",
a: "IP: " + winner.ip,
p: {
name: "mpp",
color: "#ffffff",
_id: "0",
id: "0"
}
}
]);
} else {
cl.sendArray([
{
m: "a",
a: "No IP found.",
p: {
name: "mpp",
color: "#ffffff",
_id: "0",
id: "0"
}
}
]);
}
} else {
cl.sendArray([
{
m: "a",
a: "ip: " + cl.ip,
p: { name: "mpp", color: "#ffffff", _id: "0", id: "0" }
}
]);
}
},
"admin"
)
);
*/
2023-04-20 10:22:28 +02:00
Command.addCommand(
new Command(
"time",
["time"],
undefined,
`%Ptime`,
0,
(cl, ch, msg) => {
return `It is ${cl.server.cycle.getCurrentGenericTime()}.`;
},
"admin"
)
);
2023-04-17 01:34:32 +02:00
module.exports = { Command };