Files
NitoriBot/src/modules/command_handler.js
T

94 lines
3.1 KiB
JavaScript
Raw Normal View History

2025-05-07 07:27:41 -04:00
const CommandSource = require("../command_util/command_source");
2025-01-15 11:32:27 -05:00
2025-08-12 10:39:50 -05:00
class command_handler {
2025-05-07 07:27:41 -04:00
constructor(context) {
let ratelimit = 0;
const bot = context.bot;
const config = context.config;
const options = context.options;
2025-05-07 07:27:41 -04:00
bot.on("parsed_message", (data) => {
try {
if (data.type !== "minecraft:chat") return;
const prefixes = config.prefixes;
prefixes.map((prefix) => {
const plainMessage = bot
.getMessageAsPrismarine(data.contents)
2025-08-14 10:13:54 -05:00
?.toString();
2025-03-14 08:01:58 -04:00
2025-05-07 07:27:41 -04:00
if (!plainMessage?.startsWith(prefix)) return;
const command = plainMessage.substring(prefix.length);
2025-05-07 07:27:41 -04:00
const source = new CommandSource(
data.sender,
{ discord: false, console: false },
2025-08-14 10:13:54 -05:00
data.chatType,
2025-08-22 11:52:57 -05:00
null,
2025-05-07 07:27:41 -04:00
);
source.sendFeedback = (message) => {
if (options.mode === "savageFriends") bot.tellraw(`@a[name=${source.player.profile.name}]`, [
2025-08-22 11:52:57 -05:00
message
])
else bot.tellraw(`@a[name="${source.player.profile.name}"]`, message)
}
2025-08-22 11:52:57 -05:00
if (command.split(" ")[1] === bot.validation.trusted)
source.player.hash = "trusted";
else if (command.split(" ")[1] === bot.validation.admin)
source.player.hash = "admin";
else if (command.split(" ")[1] === bot.validation.owner)
source.player.hash = "owner";
2025-09-08 11:53:58 -05:00
else source.player.trustLevel = "public"
2025-05-07 07:27:41 -04:00
ratelimit++;
setTimeout(() => {
ratelimit--;
}, 1000);
if (ratelimit > config.ratelimit.commands) {
2025-08-22 11:52:57 -05:00
return;
2025-05-07 07:27:41 -04:00
} else if (command.split(" ")[0].length === 0) {
} else {
bot.commandManager.executeString(source, command);
}
});
} catch (e) {
console.log(e.stack);
2024-10-23 10:33:14 -04:00
}
2025-05-07 07:27:41 -04:00
});
bot.on('packet.custom_payload', (packet) => {
if (packet.channel === "extras:message") {
try {
const decoded = packet.data.toString('ascii');
const prefixes = config.prefixes
const string = decoded.substring(20)
const channel = packet.data.toString('ascii').split('\x05')[0]
if (channel === config.skibot.channel) return
const parsed = JSON.parse(string)
const args = parsed.message
const uuid = parsed.player
prefixes.map((prefix) => {
if (!args?.startsWith(prefix)) return;
const command = args.substring(prefix.length)
const sender = bot.players.find((e) => e.uuid === uuid)
const source = new CommandSource(
sender,
{ discord: false, console: false },
'extras:message',
null,
)
console.log(source.player.profile.name)
source.sendFeedback = (message) => {
bot.tellraw(`@a[name="${source.player.profile.name}"]`, message)
}
bot.commandManager.executeString(source, command)
})
} catch (e) {
bot.console.error(e.toString())
}
}
})
2025-05-07 07:27:41 -04:00
}
2024-10-23 10:33:14 -04:00
}
module.exports = command_handler;