owobot/plugins/command.js

157 lines
6 KiB
JavaScript
Raw Normal View History

2024-07-06 11:02:11 -04:00
const fs=require("fs");
const Command=require("../util/Command.js");
const hashcheck=require("../util/hashcheck.js");
2024-07-18 01:49:55 -04:00
const settings = require("../settings.json");
const getMessage = require('../util/lang.js');
let cmds=Object.create(null);
2024-07-25 17:27:10 -04:00
const sortHelp=function sortHelp(c1, c2){
const level1 = cmds[c1.with[1]].level?cmds[c1.with[1]].level:0;
const level2 = cmds[c2.with[1]].level?cmds[c2.with[1]].level:0;
return level1 - level2
}
2024-07-06 11:02:11 -04:00
module.exports={
load:()=>{
module.exports.loadCMD();
},
loadBot:(b)=>{
2024-07-18 01:49:55 -04:00
b.prefix=settings.prefix;
2024-07-06 11:02:11 -04:00
b.lastCmd=0;
2024-07-16 11:53:32 -04:00
b.runCommand=(name, uuid, text, prefix)=>{
2024-07-16 17:22:42 -04:00
if(uuid=="00000000-0000-0000-0000-000000000000") return;
2024-07-23 16:38:57 -04:00
if(Date.now()-b.lastCmd<=1000) return;
b.lastCmd=Date.now();
2024-07-06 11:02:11 -04:00
const cmd=text.split(" ");
let lang=settings.defaultLang;
let verify=hashcheck(cmd);
2024-07-22 18:11:27 -04:00
if(verify>0){
text=cmd.slice(0,cmd.length-1).join(" ");
}
2024-07-06 11:02:11 -04:00
if(cmds[cmd[0].toLowerCase()]){
2024-07-22 18:11:27 -04:00
const command = cmds[cmd[0].toLowerCase()];
if(command.level!==undefined && command.level>verify){
b.tellraw(uuid,{
text:getMessage(lang,"command.disallowed.perms")
2024-07-22 18:11:27 -04:00
});
b.tellraw(uuid,{
text:getMessage(lang,"command.disallowed.perms.yourLevel",[verify+""])
2024-07-22 18:11:27 -04:00
});
b.tellraw(uuid,{
text:getMessage(lang,"command.disallowed.perms.cmdLevel",[command.level+""])
2024-07-22 18:11:27 -04:00
});
return;
}
2024-07-06 11:02:11 -04:00
try{
cmds[cmd[0].toLowerCase()].execute(new Command(uuid,name,"nick N/A",text,prefix,b,verify))
2024-07-22 18:11:27 -04:00
} catch(e) {
2024-07-25 17:27:10 -04:00
console.log(e);
b.tellraw(uuid,{
text:getMessage(lang,"command.error"),
color: "red",
hoverEvent:{
action: "show_text",
value:{
"text": e.stack
}
}
});
2024-07-22 18:11:27 -04:00
}
2024-07-06 11:02:11 -04:00
}
}
b.printHelp=(uuid,prefix,lang)=>{
2024-07-25 17:27:10 -04:00
let commandList=[];
2024-07-06 11:02:11 -04:00
for(const i in cmds){
if(cmds[i].hidden) continue;
2024-07-25 17:27:10 -04:00
let cmdColor;
switch (cmds[i].level){
case 0:
cmdColor = "green";
break;
case 1:
cmdColor = "red";
break;
case 2:
cmdColor = "dark_red";
break;
case 3:
cmdColor = "dark_gray";
break;
default:
cmdColor = "gray";
}
commandList.push(
{
translate: "%s%s ",
color: cmdColor,
with: [
prefix,
i
]
}
)
2024-07-06 11:02:11 -04:00
}
2024-07-25 17:27:10 -04:00
b.tellraw(uuid,{
translate: "%s: %s",
with: [
getMessage(lang,"command.help.cmdList"),
commandList.sort(sortHelp)
]
})
2024-07-06 11:02:11 -04:00
}
b.printCmdHelp=(uuid,cmd,lang)=>{
if(!cmds[cmd]){
b.tellraw(uuid,{text:getMessage(lang,"command.help.noCommand")});
return;
}
2024-07-23 17:18:48 -04:00
let usage=getMessage(lang,`command.${cmd}.usage`).split("||");
let desc=getMessage(lang,`command.${cmd}.desc`);
if(cmds[cmd].usage){
usage=cmds[cmd].usage.split("||");
}
if(cmds[cmd].desc){
desc=cmds[cmd].desc;
}
2024-07-23 15:15:15 -04:00
//b.tellraw(uuid,{"text":getMessage(lang,"command.help.commandInfo",[cmd,usage,desc])});
for(const i in usage){
b.tellraw(uuid,{text:getMessage(lang,"command.help.commandUsage",[cmd,usage[i]])});
}
b.tellraw(uuid,{text:getMessage(lang,"command.help.commandDesc",[desc])});
2024-07-23 15:15:15 -04:00
const permsN=getMessage(lang,"command.help.permsNormal");
const permsT=getMessage(lang,"command.help.permsTrusted");
const permsO=getMessage(lang,"command.help.permsOwner");
const permsC=getMessage(lang,"command.help.permsConsole");
const rPerms=cmds[cmd].level?cmds[cmd].level:0;
b.tellraw(uuid,{text:getMessage(lang,"command.help.commandPerms",[[permsN,permsT,permsO,permsC][rPerms]])});
2024-07-06 11:02:11 -04:00
}
},
loadCMD:()=>{
const botplug = []
const bpl = fs.readdirSync('./plugins/commands')
for (const i in bpl) {
if (!bpl[i].endsWith('.js')) {
continue
}
try {
commandName=bpl[i].split(".js")[0];
cmds[commandName]=require(`./commands/${bpl[i]}`);
2024-07-25 17:27:10 -04:00
if(cmds[commandName].level === undefined){
cmds[commandName].level = 0;
}
console.log("Loaded command "+commandName);
if(cmds[commandName].aliases){
for(const j in cmds[commandName].aliases){
cmds[cmds[commandName].aliases[j]]={
execute:cmds[commandName].execute,
desc:"Alias to "+commandName,
usage:cmds[commandName].usage,
2024-07-23 15:18:38 -04:00
level:cmds[commandName].level,
hidden:true,
consoleIndex:cmds[commandName].consoleIndex
};
}
}
2024-07-06 11:02:11 -04:00
} catch (e) { console.log(e); }
}
},
cmds
}