Files
NitoriBot/src/modules/command_manager.js
T

313 lines
11 KiB
JavaScript
Raw Normal View History

2025-05-07 07:27:41 -04:00
const fs = require("fs");
const path = require("path");
const CommandError = require("../command_util/command_error.js");
const CommandSource = require("../command_util/command_source");
const fixansi = require("../util/ansi");
const sleep = require("../util/sleep.js");
2025-02-03 09:15:29 -06:00
function unknownCommand(commandName, bot) {
2025-08-22 11:52:57 -05:00
return {
translate: "%s%s%s %s",
color: "red",
with: [
{ translate: "command.unknown.command", color: "red" },
{ text: "\n"},
{ text: `${commandName}`, color: "gray" },
{ translate: "command.context.here", color: "red" }
]
}
}
2025-08-12 10:39:50 -05:00
class command_manager {
2025-05-07 07:27:41 -04:00
constructor(context) {
const bot = context.bot;
const config = context.config;
const discordClient = context.discordClient;
const options = context.options;
2025-08-22 11:52:57 -05:00
2025-05-07 07:27:41 -04:00
bot.commandManager = {
commands: {},
commandlist: [],
execute(source, commandName, args) {
const command = this.getCommand(commandName.toLowerCase());
2025-08-22 11:52:57 -05:00
const authed = bot.auth.list;
2025-05-07 07:27:41 -04:00
try {
if (!command) {
2025-08-22 11:52:57 -05:00
if (source?.sources?.console)
bot.console.command(
unknownCommand(commandName, bot),
);
else
source.sendFeedback(
unknownCommand(commandName, bot),
);
2024-12-16 13:48:50 -05:00
}
2025-08-22 11:52:57 -05:00
2025-05-07 07:27:41 -04:00
const event = bot.discord.message;
const roles = event?.member?.roles?.cache;
2025-08-12 10:39:50 -05:00
2025-05-07 07:27:41 -04:00
switch (command?.data?.trustLevel) {
case 0:
// do nothing since trust level 0 is public
break;
case 1:
if (source?.sources?.discord) {
const hasRole = roles?.some(
(role) =>
role.name === `${config.discord.roles.trusted}` ||
role.name === `${config.discord.roles.admin}` ||
role.name === `${config.discord.roles.fullAccess}` ||
role.name === `${config.discord.roles.owner}`,
);
2025-08-22 11:52:57 -05:00
if (!hasRole)
throw new CommandError({
text: "You done have the trusted, admin, or owner roles!",
color: "red"
});
2025-05-07 07:27:41 -04:00
} else if (!source?.sources.console) {
if (source.ChatType === "extras:message")
throw new CommandError({
text: "Trusted Commands can not be ran from extras:message",
color: "red"
})
2025-08-22 11:52:57 -05:00
if (
args.length === 0 &&
authed.find((e) => e?.player === source.player.uuid)
?.trustLevel !== "trusted" &&
authed.find((e) => e?.player === source.player.uuid)
?.trustLevel !== "admin" &&
authed.find((e) => e?.player === source.player.uuid)
?.trustLevel !== "owner"
)
2025-08-22 11:52:57 -05:00
throw new CommandError({
text: "Please provide a trusted, admin, or owner hash",
color: "red"
});
2025-05-07 07:27:41 -04:00
if (
2025-08-22 11:52:57 -05:00
args[0] !== bot.validation.trusted &&
2025-05-07 07:27:41 -04:00
args[0] !== bot.validation.admin &&
2025-08-22 11:52:57 -05:00
args[0] !== bot.validation.owner &&
authed.find((e) => e.player === source.player.uuid)
?.trustLevel !== "trusted" &&
authed.find((e) => e.player === source.player.uuid)
?.trustLevel !== "admin" &&
authed.find((e) => e.player === source.player.uuid)
?.trustLevel !== "owner"
2025-05-07 07:27:41 -04:00
)
2025-08-22 11:52:57 -05:00
throw new CommandError({
text: "Invalid Hash",
color: "red"
});
2025-08-18 16:54:25 -05:00
if (source.player.authed) {
2025-08-22 11:52:57 -05:00
args = args;
2025-08-18 16:54:25 -05:00
} else {
2025-08-22 11:52:57 -05:00
args = args.slice(1);
2025-08-18 16:54:25 -05:00
}
2025-05-07 07:27:41 -04:00
}
break;
case 2:
if (source?.sources?.discord) {
const hasRole = roles?.some(
(role) =>
role.name === `${config.discord.roles.admin}` ||
role.name === `${config.discord.roles.fullAccess}` ||
role.name === `${config.discord.roles.owner}`,
);
2025-08-22 11:52:57 -05:00
if (!hasRole)
throw new CommandError({
text: "You dont have the admin, or owner roles!",
color: "red"
});
2025-05-07 07:27:41 -04:00
} else if (!source?.sources?.console) {
if (source.ChatType === "extras:message")
throw new CommandError({
text: "Admin Commands can not be ran from extras:message",
color: "red"
})
2025-05-07 07:27:41 -04:00
if (
2025-08-22 11:52:57 -05:00
args.length === 0 &&
authed.find((e) => e?.player === source.player.uuid)
?.trustLevel !== "admin" &&
authed.find((e) => e?.player === source.player.uuid)
?.trustLevel !== "owner"
)
throw new CommandError({
text: "Please provide a hash",
color: "red"
});
if (
args[0] !== bot.validation.admin &&
args[0] !== bot.validation.owner &&
authed.find((e) => e?.player === source.player.uuid)
?.trustLevel !== "admin" &&
authed.find((e) => e?.player === source.player.uuid)
?.trustLevel !== "owner"
2025-05-07 07:27:41 -04:00
)
2025-08-22 11:52:57 -05:00
throw new CommandError({
text: "Invalid hash",
color: "red"
});
2025-08-18 16:54:25 -05:00
if (source.player.authed) {
2025-08-22 11:52:57 -05:00
args = args;
2025-08-18 16:54:25 -05:00
} else {
2025-08-22 11:52:57 -05:00
args = args.slice(1);
2025-08-18 16:54:25 -05:00
}
2025-05-07 07:27:41 -04:00
}
break;
case 3:
if (source?.sources?.discord) {
const hasRole = roles?.some(
(role) =>
role.name === `${config.discord.roles.owner}` ||
role.name === `${config.discord.roles.fullAccess}`,
);
if (!hasRole)
2025-08-22 11:52:57 -05:00
throw new CommandError({
text: "You dont have the owner role!",
color: "red"
});
2025-05-07 07:27:41 -04:00
} else if (!source?.sources?.console) {
if (source.ChatType === "extras:message")
throw new CommandError({
text: "Owner Commands can not be ran from extras:message",
color: "red"
})
2025-08-22 11:52:57 -05:00
if (
args.length === 0 &&
bot.validation.owner &&
authed.find((e) => e?.player === source.player.uuid)
?.trustLevel !== "owner"
)
throw new CommandError({
text: "Please provide a hash",
color: "red"
});
if (
args[0] !== bot.validation.owner &&
authed.find((e) => e?.player === source.player.uuid)
?.trustLevel !== "owner"
)
throw new CommandError({
text: "Invalid hash",
color: "red"
});
2025-08-18 16:54:25 -05:00
if (source.player.authed) {
args = args;
} else {
2025-08-22 11:52:57 -05:00
args = args.slice(1);
2025-08-18 16:54:25 -05:00
}
2025-05-07 07:27:41 -04:00
}
break;
case 4:
if (!source?.sources?.console) {
2025-08-22 11:52:57 -05:00
throw new CommandError({
text: "This Command can only be ran via console",
color: "red"
});
2025-05-07 07:27:41 -04:00
}
break;
2025-08-12 10:39:50 -05:00
case 5:
2025-08-22 11:52:57 -05:00
throw new CommandError({
text: "This Command has been disabled!",
color: "red"
});
break;
2025-05-07 07:27:41 -04:00
}
2024-10-23 10:33:14 -04:00
2025-08-22 11:52:57 -05:00
/* if (source.sources.discord) {
2025-05-07 07:27:41 -04:00
discordClient.channels.cache
.get("1361739286113685534")
.send(
`User: ${source.player.profile.name}, Command: ${command?.data?.name}, Args: ${args.join(" ")}, Time/Date: ${new Date().toLocaleString("en-us", { TimeZone: "America/Chicago" })}, Source: Discord`,
2025-05-07 07:27:41 -04:00
);
} else if (!source.sources.console) {
discordClient.channels.cache
.get("1361739286113685534")
.send(
`User: ${source.player.profile.name}, Command: ${command?.data?.name}, Args: ${args.join(" ")}, Time/Date: ${new Date().toLocaleString("en-us", { TimeZone: "America/Chicago" })}, Source: Minecraft, Server: ${bot.options.host}:${bot.options.port}`,
2025-05-07 07:27:41 -04:00
);
2025-08-14 10:13:54 -05:00
}*/
2024-07-07 15:44:16 -04:00
2025-08-22 11:52:57 -05:00
// if (command.playerOnly === true && source.ChatType.find((e) => e)) return;
2025-05-07 07:27:41 -04:00
return command?.execute({
bot,
source,
arguments: args,
config,
discordClient,
});
} catch (error) {
if (error instanceof CommandError) {
source.sendFeedback(error._message);
2025-05-07 07:27:41 -04:00
} else {
bot.console.error(error.stack)
source.sendFeedback(
{
2025-05-07 07:27:41 -04:00
translate: "command.failed",
color: "red",
hoverEvent: {
action: "show_text",
contents: String(error.stack),
},
}
)
2025-05-07 07:27:41 -04:00
}
2025-02-03 09:15:29 -06:00
}
2025-05-07 07:27:41 -04:00
},
2025-02-03 09:15:29 -06:00
2025-05-07 07:27:41 -04:00
executeString(source, command) {
const [commandName, ...args] = command.split(" ");
2025-02-03 09:15:29 -06:00
2025-05-07 07:27:41 -04:00
return this.execute(source, commandName, args);
},
2024-09-07 21:13:24 -04:00
2025-05-07 07:27:41 -04:00
register(command) {
this.commands[command.data.name] = command;
if (command.data.aliases) {
command.data.aliases.map((a) => (this.commands[a] = command));
}
},
2024-10-23 10:33:14 -04:00
2025-05-07 07:27:41 -04:00
getCommand(name) {
return this.commands[name];
},
2024-07-07 15:44:16 -04:00
2025-05-07 07:27:41 -04:00
getCommands() {
return Object.values(this.commands);
},
};
2024-07-07 15:44:16 -04:00
2025-05-07 07:27:41 -04:00
/*
command loader ported from my discord bot SkiBot
*/
2025-05-07 07:27:41 -04:00
let commandlist = [];
const foldersPath = path.join(__dirname, "../commands");
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath);
for (const filename of commandFiles) {
try {
const filePath = path.join(commandsPath, filename);
if (filename.endsWith(".js")) {
const command = require(filePath);
const commands = new command();
bot.commandManager.register(commands);
bot.commandManager.commandlist.push(commands);
}
} catch (error) {
console.error("Failed to load command ", filename, ":", error);
2024-11-06 11:50:24 -05:00
}
2024-10-23 10:33:14 -04:00
}
2024-07-07 15:44:16 -04:00
}
}
}
2025-08-12 10:39:50 -05:00
module.exports = command_manager;