Files
NitoriBot/src/modules/command_manager.js
T

176 lines
8.1 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-09-19 11:30:06 -04:00
bot.console.warn([
2024-08-04 22:59:52 -04:00
{
translate: 'command.unknown.command',
color: "dark_red"
},
{
text: "\n",
},
{
text: `${commandName}`,
color: "dark_red"
},
{
translate: "command.context.here",
color: "dark_red"
}
2024-09-19 11:30:06 -04:00
])
2024-07-07 15:44:16 -04:00
}
2024-07-20 20:27:17 -04:00
if (command?.trustLevel > 0) {
const event = bot.discord.message;
2024-07-20 20:27:17 -04:00
const roles = event?.member?.roles?.cache;
if (command?.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-07-20 20:27:17 -04:00
} else if (command?.trustLevel === 1 && source?.sources.discord) {
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" })
}
if (command?.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' })
if (args[0] !== bot.validation.trusted && 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
}
if (command?.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' })
} else if (command?.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" })
} else if (command?.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 {
// console.log(error.toString());
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 if (error.toString().length < 256 && error._useChat === false) {
bot.tellraw("@a", error._message);*/
} else {
bot.tellraw("@a", error._message);
}
// 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) {
this.commands[command.name] = command
if (command.aliases) {
command.aliases.map((a) => (this.commands[a] = command));
}
},
2024-10-23 10:33:14 -04:00
2024-08-09 22:08:42 -04:00
unregister (command) {
this.commands = {};
2024-08-09 22:08:42 -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
}
commandlist = [];
for (const filename of fs.readdirSync(path.join(__dirname, '../commands'))) {
try {
2024-10-23 10:33:14 -04:00
if (filename.endsWith('.mjs')) {
let commands = await import(path.join(__dirname, '../commands', filename))
bot.commandManager.commandlist.push(commands.command);
} if (filename.endsWith('.js')) {
let commands = require(path.join(__dirname, '../commands', filename));
bot.commandManager.register(commands);
bot.commandManager.commandlist.push(commands);
}
2024-07-07 15:44:16 -04:00
} catch (error) {
2024-10-23 10:33:14 -04:00
console.error('Failed to load command ', filename, ':', error)
2024-07-07 15:44:16 -04:00
}
}
}
module.exports = command_manager;