Files
NitoriBot/src/modules/command_manager.js
T

173 lines
8.2 KiB
JavaScript
Raw Normal View History

2024-07-07 15:44:16 -04:00
const fs = require('fs');
const path = require('path');
const CommandError = require('../util/command_error.js');
const CommandSource = require('../util/command_source');
2024-07-20 20:27:17 -04:00
const { EmbedBuilder } = require('discord.js');
2024-10-23 10:33:14 -04:00
async function command_manager (bot, options, config, discordClient) {
2024-07-07 15:44:16 -04:00
bot.commandManager = {
commands: {},
commandlist: [],
execute (source, commandName, args) {
2024-07-20 20:27:17 -04:00
const command = this.getCommand(commandName.toLowerCase());
2024-07-07 15:44:16 -04:00
try {
2024-07-20 20:27:17 -04:00
if (source?.sources?.discord && !source?.sources?.console) {
if (!command || !command?.execute) {
const Embed = new EmbedBuilder()
.setColor(`${config.colors.discord.error}`)
.setTitle("Unknown command")
.setDescription(`Unknown command: ${commandName}`)
bot?.discord?.message?.reply({ embeds: [Embed] })
}
} else if (!source?.sources?.discord && !source?.sources?.console) {
if (!command || !command.execute)
bot.tellraw("@a", {
2024-10-23 10:33:14 -04:00
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-08-04 22:59:52 -04:00
} else if (source?.sources?.console && !source?.sources?.discord) {
if (!command || !command.execute)
2024-11-06 11:50:24 -05:00
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())
2024-07-07 15:44:16 -04:00
}
2024-11-06 11:50:24 -05:00
if (command?.data?.trustLevel > 0) {
const event = bot.discord.message;
2024-07-20 20:27:17 -04:00
const roles = event?.member?.roles?.cache;
2024-11-06 11:50:24 -05:00
if (command?.data?.trustLevel === 1 && !source?.sources?.discord) {
2024-10-23 10:33:14 -04:00
if (args.length === 0 && bot.validation.trusted && bot.validation.admin && bot.validation.owner && !source?.sources?.console) throw new CommandError({ text: "Please provide an trusted or an admin or an owner hash", color: "dark_red" })
if (args[0] !== bot.validation.trusted && args[0] !== bot.validation.admin && args[0] !== bot.validation.owner && !source.sources.console) throw new CommandError({ translate: 'Invalid trusted or admin or owner hash', color: 'dark_red' });
2024-11-06 11:50:24 -05:00
} else if (command?.data?.trustLevel === 1 && source?.sources.discord) {
2024-07-20 20:27:17 -04:00
const hasRole = roles?.some(role => role.name === `${config.discord.roles.trusted}` || role.name === `${config.discord.roles.owner}`)
2024-10-23 10:33:14 -04:00
if (!hasRole) throw new CommandError({ translate: 'You are not trusted or the owner!', color: "dark_red" })
}
2024-11-06 11:50:24 -05:00
if (command?.data?.trustLevel === 2 && !source.sources.console) {
2024-10-23 10:33:14 -04:00
if (args.length === 0 && bot.validation.admin && bot.validation.owner && !source.sources.console) throw new CommandError({ text: "Please provide an trusted or owner hash", color: 'dark_red' })
2024-11-06 11:50:24 -05:00
if (args[0] !== bot.validation.admin && args[0] !== bot.validation.owner && !source.sources.console) throw new CommandError({ translate: 'Invalid trusted or owner hash', color: 'dark_red' });
2024-07-20 20:27:17 -04:00
}
2024-11-06 11:50:24 -05:00
if (command?.data?.trustLevel === 3 && !source.sources.discord && !source.sources.console) {
2024-10-23 10:33:14 -04:00
if (args.length === 0 && bot.validation.owner) throw new CommandError({ text: "Please provide an owner hash", color: "dark_red" })
2024-07-20 20:27:17 -04:00
if (args[0] !== bot.validation.owner) throw new CommandError({ translate: 'Invalid owner hash', color: 'dark_red' })
2024-11-06 11:50:24 -05:00
} else if (command?.data?.trustLevel === 3 && source.sources.discord && !source.sources.console) {
2024-07-20 20:27:17 -04:00
const hasRole = roles?.some(role => role.name === `${config.discord.roles.owner}`)
2024-10-23 10:33:14 -04:00
if (!hasRole) throw new CommandError({ translate: 'You are not the owner!', color: "dark_red" })
2024-11-06 11:50:24 -05:00
} else if (command?.data?.trustLevel === 4 && !source.sources.console) {
2024-10-23 10:33:14 -04:00
throw new CommandError({ text: 'This command can only be ran via console', color: "dark_red" });
2024-07-20 20:27:17 -04:00
}
2024-09-19 11:30:06 -04:00
}
if (!command?.discordExecute && command && source?.sources?.discord) {
2024-07-07 15:44:16 -04:00
throw new CommandError(`${command.name} command is not supported in discord!`)
2024-09-19 11:30:06 -04:00
} else if (command?.discordExecute && command && source?.sources?.discord) {
2024-07-07 15:44:16 -04:00
return command.discordExecute({ bot, source, arguments: args, config, discordClient })
2024-09-19 11:30:06 -04:00
} else if (!command?.execute && command && !source?.sources?.discord) {
2024-07-20 20:27:17 -04:00
throw new CommandError(`${command.name} command is not supported in game!`)
2024-09-19 11:30:06 -04:00
} else if (command?.execute && command && !source?.sources?.discord) {
return command?.execute({ bot, source, arguments: args, config, discordClient });
2024-07-07 15:44:16 -04:00
}
} catch (error) {
console.error(error)
2024-07-20 20:27:17 -04:00
if (source?.sources?.discord && !source?.sources?.console) {
const Embed = new EmbedBuilder()
.setColor(`${config.colors.discord.error}`)
.setTitle(`${command?.name} command`)
.setDescription(`\`\`\`${error}\`\`\``)
bot?.discord?.message?.reply({
embeds: [
Embed
]
})
} else if (!source?.sources?.discord && !source?.sources?.console) {
if (error instanceof CommandError) {
if (bot.options.isSavage || bot.options.isCreayun) {
bot.chat.message(`&4${error.message}`)
} else {
if (error.toString().length > 256) {
bot.tellraw("@a", error._message);
} else if (error.toString().length < 256) {
bot.chat.message(`${bot.getMessageAsPrismarine(error._message)?.toMotd().replaceAll('§','&')}`)
} else {
bot.tellraw("@a", error._message);
}
}
} else {
if (bot.options.isSavage || bot.options.isCreayun) {
bot.chat.message(`${bot.getMessageAsPrismarine({ translate: "command.failed", color: "dark_red" })?.toMotd().replaceAll('§','&')}`)
} else {
bot.tellraw("@a", [{ translate: 'command.failed', color: "dark_red", hoverEvent: { action: 'show_text', contents: `${error.stack}` } }])
}
}
2024-07-20 20:27:17 -04:00
}
2024-07-07 15:44:16 -04:00
}
},
2024-10-23 10:33:14 -04:00
2024-07-07 15:44:16 -04:00
executeString (source, command) {
const [commandName, ...args] = command.split(' ')
return this.execute(source, commandName, args)
},
discordExecute(source, command) {
const [commandName, ...args] = command.split(" ");
if (source?.sources?.discord && !source?.sources?.console) {
2024-07-07 15:44:16 -04:00
return this.discordExecute(source, commandName, args)
}
2024-07-07 15:44:16 -04:00
},
2024-09-07 21:13:24 -04:00
2024-07-07 15:44:16 -04:00
register (command) {
2024-11-06 11:50:24 -05:00
this.commands[command.data.name] = command
if (command.data.aliases) {
command.data.aliases.map((a) => (this.commands[a] = command));
2024-07-07 15:44:16 -04:00
}
},
2024-10-23 10:33:14 -04:00
2024-07-07 15:44:16 -04:00
getCommand (name) {
return this.commands[name]
},
getCommands () {
return Object.values(this.commands)
2024-09-07 21:13:24 -04:00
},
2024-07-07 15:44:16 -04:00
}
2024-11-06 11:50:24 -05:00
/*
file loader ported from my discord bot SkiBot
and edited to support mjs files and to support FNFBoyfriendBot's command format
*/
2024-07-07 15:44:16 -04:00
commandlist = [];
2024-11-06 11:50:24 -05:00
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('.mjs')) {
let command = await import(filePath);
bot.commandManager.register(command);
bot.commandManager.commandlist.push(command);
}
if (filename.endsWith('.js')) {
let command = require(filePath);
bot.commandManager.register(command);
bot.commandManager.commandlist.push(command);
}
} catch (error) {
console.error('Failed to load command ', filename, ':', error);
2024-10-23 10:33:14 -04:00
}
2024-07-07 15:44:16 -04:00
}
}
}
module.exports = command_manager;