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-11-22 12:49:22 -05:00
|
|
|
|
|
|
|
|
async function command_manager (context) {
|
|
|
|
|
const bot = context.bot;
|
|
|
|
|
const config = context.config;
|
|
|
|
|
const discordClient = context.discordClient;
|
|
|
|
|
const options = context.options;
|
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-10-30 19:07:14 -04:00
|
|
|
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-22 12:49:22 -05: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.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-22 12:49:22 -05:00
|
|
|
} 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' });
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
if (source?.sources?.discord) {
|
|
|
|
|
const hasRole = roles?.some(role => role.name === `${config.discord.roles.admin}` || role.name === `${config.discord.roles.owner}`)
|
|
|
|
|
if (!hasRole) throw new CommandError({ translate: 'You are not trusted or the owner!', 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}`)
|
|
|
|
|
if (!hasRole) throw new CommandError({ translate: 'You are not the owner!', 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-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) {
|
2024-10-17 20:38:21 -04:00
|
|
|
return command?.execute({ bot, source, arguments: args, config, discordClient });
|
2024-07-07 15:44:16 -04:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2024-10-30 19:07:14 -04:00
|
|
|
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) {
|
2024-09-25 12:20:07 -04:00
|
|
|
if (error instanceof CommandError) {
|
|
|
|
|
if (bot.options.isSavage || bot.options.isCreayun) {
|
|
|
|
|
bot.chat.message(`&4${error.message}`)
|
|
|
|
|
} else {
|
2024-10-30 19:07:14 -04:00
|
|
|
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);
|
|
|
|
|
}
|
2024-09-25 12:20:07 -04:00
|
|
|
}
|
|
|
|
|
} 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(" ");
|
2024-10-30 19:07:14 -04:00
|
|
|
if (source?.sources?.discord && !source?.sources?.console) {
|
2024-07-07 15:44:16 -04:00
|
|
|
return this.discordExecute(source, commandName, args)
|
2024-10-30 19:07:14 -04:00
|
|
|
}
|
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);
|
2024-11-06 12:21:44 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
module.exports = command_manager;
|