Files
NitoriBot/src/modules/command_manager.js
T

255 lines
9.1 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
2025-05-07 07:27:41 -04:00
class CommandManagerModule {
constructor(context) {
const bot = context.bot;
const config = context.config;
const discordClient = context.discordClient;
const options = context.options;
bot.commandManager = {
commands: {},
commandlist: [],
execute(source, commandName, args) {
const command = this.getCommand(commandName.toLowerCase());
try {
if (!command) {
if (source?.sources?.console) {
bot.console.warn(
bot
.getMessageAsPrismarine({
translate: "%s%s%s %s",
color: "dark_gray",
with: [
{ translate: "command.unknown.command", color: "red" },
{ text: "\n" },
{ text: `${commandName}` },
{ translate: "command.context.here", color: "red" },
],
})
?.toAnsi(),
);
} else {
throw new CommandError({
translate: "%s%s%s %s",
color: "dark_gray",
with: [
{ translate: "command.unknown.command", color: "red" },
{ text: "\n" },
{ text: `${commandName}` },
{ translate: "command.context.here", color: "red" },
],
});
}
2024-12-16 13:48:50 -05:00
}
2025-01-15 11:32:27 -05:00
2025-05-07 07:27:41 -04:00
const event = bot.discord.message;
const roles = event?.member?.roles?.cache;
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}`,
);
if (!hasRole)
throw new CommandError({
translate:
"You dont have the trusted, admin, or owner role!",
color: "dark_red",
});
} else if (!source?.sources.console) {
if (args.length === 0)
throw new CommandError({
text: "Please provide a trusted, admin or owner hash",
color: "dark_red",
});
if (
args[0] !== bot.validation.trusted &&
args[0] !== bot.validation.admin &&
args[0] !== bot.validation.owner
)
throw new CommandError({
translate: "Invalid trusted, admin or owner hash",
color: "dark_red",
});
}
2025-01-15 11:32:27 -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}`,
);
if (!hasRole)
throw new CommandError({
translate: "You dont have the admin, or owner role!",
color: "dark_red",
});
} else if (!source?.sources?.console) {
if (args.length === 0)
throw new CommandError({
text: "Please provide an admin or owner hash",
color: "dark_red",
});
if (
args[0] !== bot.validation.admin &&
args[0] !== bot.validation.owner
)
throw new CommandError({
translate: "Invalid admin or owner hash",
color: "dark_red",
});
}
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)
throw new CommandError({
translate: "You do not have the owner role!",
color: "dark_red",
});
} else if (!source?.sources?.console) {
if (args.length === 0 && bot.validation.owner)
throw new CommandError({
text: "Please provide an owner hash",
color: "dark_red",
});
if (args[0] !== bot.validation.owner)
throw new CommandError({
translate: "Invalid owner hash",
color: "dark_red",
});
}
break;
case 4:
if (!source?.sources?.console) {
throw new CommandError({
text: "This command can only be ran via console",
color: "dark_red",
});
}
break;
}
2024-10-23 10:33:14 -04:00
2025-05-07 07:27:41 -04:00
if (source.sources.discord) {
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`,
);
} 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}`,
);
}
2024-07-07 15:44:16 -04:00
2025-05-07 07:27:41 -04:00
return command?.execute({
bot,
source,
arguments: args,
config,
discordClient,
});
} catch (error) {
if (error instanceof CommandError) {
if (source.sources.discord) {
bot.discord.message.reply(
`\`\`\`ansi\n${new fixansi(bot.getMessageAsPrismarine(error._message)?.toAnsi()).ansi}\`\`\``,
);
} else if (source.sources.console) {
bot.console.warn(error._message);
} else {
bot.tellraw("@a", error._message);
}
} else {
if (source.sources.discord) {
bot.discord.message.reply(
`\`\`\`ansi\n${new fixansi(bot.getMessageAsPrismarine({ text: error.stack, color: "red"})?.toAnsi()).ansi}\`\`\``,
);
} else {
bot.tellraw("@a", {
translate: "command.failed",
color: "red",
hoverEvent: {
action: "show_text",
contents: String(error.stack),
},
});
2025-02-03 09:15:29 -06:00
2025-05-07 07:27:41 -04:00
bot?.console?.warn(error.stack);
}
}
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
2024-11-06 11:50:24 -05:00
*/
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-05-07 07:27:41 -04:00
module.exports = CommandManagerModule;