Files
NitoriBot/src/modules/command_manager.js
T

185 lines
7.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');
2025-01-24 10:00:06 -05:00
const fixansi = require('../util/ansi');
2025-01-15 11:32:27 -05:00
async function inject (context) {
2024-11-22 12:49:22 -05:00
const bot = context.bot;
const config = context.config;
const discordClient = context.discordClient;
const options = context.options;
2024-12-16 13:48:50 -05:00
const { MessageBuilder } = require('prismarine-chat')(bot.options.version);
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)
2024-12-16 13:48:50 -05:00
if (bot.options.isSavage) {
bot.chat.message(bot.getMessageAsPrismarine({
translate: "%s",
color: "dark_gray",
with: [
{ translate: "command.unknown.command", color: "red" },
]
})?.toMotd().replaceAll('§','&'));
bot.chat.message(bot.getMessageAsPrismarine({
translate: "%s %s",
color: "dark_gray",
with: [
{ text: `${commandName}` },
{ translate: "command.context.here", color: "red" }
]
})?.toMotd().replaceAll('§','&'));
} else {
bot.tellraw("@a", new MessageBuilder()
.setTranslate("%s%s%s %s")
.setColor("dark_gray")
.addWith(new MessageBuilder().setTranslate("command.unknown.command").setColor("red"))
.addWith(new MessageBuilder().setText("\n"))
.addWith(new MessageBuilder().setText(`${commandName}`))
.addWith(new MessageBuilder().setTranslate("command.context.here").setColor("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())
2025-01-15 11:32:27 -05:00
};
require('../util/command_manager_util')(bot, command, args, source);
// require('../util/command_manager_arguments')(bot, args, command);
2024-09-19 11:30:06 -04:00
if (!command?.discordExecute && command && source?.sources?.discord) {
2024-12-10 12:20:51 -05:00
throw new CommandError(`${command.data.name} command is not supported in discord!`)
2024-09-19 11:30:06 -04:00
} else if (command?.discordExecute && command && source?.sources?.discord) {
2025-01-24 10:00:06 -05:00
return command.discordExecute({ bot, source, arguments: args, config, discordClient, EmbedBuilder, fixansi })
2024-09-19 11:30:06 -04:00
} else if (!command?.execute && command && !source?.sources?.discord) {
2024-12-10 12:20:51 -05:00
throw new CommandError(`${command.data.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}`)
2024-11-22 12:49:22 -05:00
.setTitle(`${command?.data?.name} command`)
2024-07-20 20:27:17 -04:00
.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.default);
bot.commandManager.commandlist.push(command.default);
2024-11-06 11:50:24 -05:00
}
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
}
}
}
2025-01-15 11:32:27 -05:00
module.exports = {
data: {
enabled: true,
name: "command manager",
type: "commands"
},
inject
};