forked from afspiegeling/NitoriBot
v8.0.0-alpha
This commit is contained in:
+236
-137
@@ -1,155 +1,254 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const CommandError = require('../util/command_error.js');
|
||||
const CommandSource = require('../util/command_source');
|
||||
const fixansi = require('../util/ansi');
|
||||
const cmdMgrUtil = require('../util/command_manager_util');
|
||||
const sleep = require('../util/sleep.js');
|
||||
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");
|
||||
|
||||
async function inject (context) {
|
||||
const bot = context.bot;
|
||||
const config = context.config;
|
||||
const discordClient = context.discordClient;
|
||||
const options = context.options;
|
||||
const { MessageBuilder } = require('prismarine-chat')(bot.options.version);
|
||||
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());
|
||||
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" },
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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}`,
|
||||
);
|
||||
}
|
||||
|
||||
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 {
|
||||
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" }
|
||||
]
|
||||
});
|
||||
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),
|
||||
},
|
||||
});
|
||||
|
||||
bot?.console?.warn(error.stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
cmdMgrUtil(bot, command, args, source, config);
|
||||
executeString(source, command) {
|
||||
const [commandName, ...args] = command.split(" ");
|
||||
|
||||
return command?.execute({ bot, source, arguments: args, config, discordClient });
|
||||
} catch (error) {
|
||||
if (error instanceof CommandError) {
|
||||
bot.tellraw("@a", error._message);
|
||||
} else {
|
||||
bot.tellraw("@a", {
|
||||
translate: "command.failed",
|
||||
color: "red",
|
||||
hoverEvent: {
|
||||
action: "show_text",
|
||||
contents: String(error.stack)
|
||||
}
|
||||
});
|
||||
|
||||
bot?.console?.warn(error.stack);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
executeString (source, command) {
|
||||
const [commandName, ...args] = command.split(' ');
|
||||
if (source?.sources?.discord) {
|
||||
return this.discordExecute(source, commandName, args);
|
||||
} else if (!source?.sources?.discord) {
|
||||
return this.execute(source, commandName, args);
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
discordExecute(source, commandName, args) {
|
||||
const command = this.getCommand(commandName.toLowerCase());
|
||||
|
||||
try {
|
||||
if (!command) {
|
||||
throw new CommandError(`Unknown Command: ${commandName}`);
|
||||
register(command) {
|
||||
this.commands[command.data.name] = command;
|
||||
if (command.data.aliases) {
|
||||
command.data.aliases.map((a) => (this.commands[a] = command));
|
||||
}
|
||||
},
|
||||
|
||||
cmdMgrUtil(bot, command, args, source, config);
|
||||
getCommand(name) {
|
||||
return this.commands[name];
|
||||
},
|
||||
|
||||
if (!command?.discordExecute) {
|
||||
throw new CommandError(`${command.data.name} command is not supported in discord!`);
|
||||
} else if (source.sources.discord) {
|
||||
return command?.discordExecute({ bot, source, arguments: args, config, discordClient, fixansi });
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof CommandError) {
|
||||
bot?.discord?.message?.reply(`${bot.getMessageAsPrismarine(error._message)}`);
|
||||
} else {
|
||||
bot?.discord?.message?.reply(`\`\`\`${error.stack}\`\`\``);
|
||||
bot?.console?.warn(error.stack);
|
||||
}
|
||||
}
|
||||
},
|
||||
getCommands() {
|
||||
return Object.values(this.commands);
|
||||
},
|
||||
};
|
||||
|
||||
register (command) {
|
||||
this.commands[command.data.name] = command
|
||||
if (command.data.aliases) {
|
||||
command.data.aliases.map((a) => (this.commands[a] = command));
|
||||
}
|
||||
},
|
||||
|
||||
getCommand (name) {
|
||||
return this.commands[name]
|
||||
},
|
||||
|
||||
getCommands () {
|
||||
return Object.values(this.commands)
|
||||
},
|
||||
}
|
||||
|
||||
/*
|
||||
file loader ported from my discord bot SkiBot
|
||||
and edited to support mjs files and to support FNFBoyfriendBot's command format
|
||||
/*
|
||||
command loader ported from my discord bot SkiBot
|
||||
*/
|
||||
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('.mjs')) {
|
||||
let command = await import(filePath);
|
||||
bot.commandManager.register(command.default);
|
||||
bot.commandManager.commandlist.push(command.default);
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
data: {
|
||||
enabled: true,
|
||||
name: "command manager",
|
||||
type: "commands"
|
||||
},
|
||||
inject
|
||||
};
|
||||
module.exports = CommandManagerModule;
|
||||
|
||||
Reference in New Issue
Block a user