Files
NitoriBot/src/modules/console.js
T

120 lines
4.0 KiB
JavaScript
Raw Normal View History

2025-05-07 07:27:41 -04:00
const CommandSource = require("../command_util/command_source");
const prismarineChat = require("prismarine-chat")("1.20.2");
2024-11-22 12:49:22 -05:00
2025-05-07 07:27:41 -04:00
class ConsoleModule {
constructor(context) {
const bot = context.bot;
const config = context.config;
const options = context.options;
let ratelimit = 0;
2025-03-07 11:14:42 -05:00
2025-05-07 07:27:41 -04:00
let serverName;
if (options.private) {
serverName = `${options.serverName}`;
} else {
serverName = `§1${bot.options.host}:§9${bot.options.port}`;
2024-08-30 11:56:58 -04:00
}
2025-05-07 07:27:41 -04:00
bot.console = {
readline: null,
server: "all",
readlineInterface(rl) {
this.readline = rl;
rl.on("line", (args) => {
if (bot.options.serverName !== this.server && this.server !== "all")
return;
if (args.startsWith(config.console.prefix)) {
return bot.commandManager.executeString(
bot.console.source,
args.substring(config.console.prefix.length),
);
} else if (args.startsWith("")) {
bot.chat.send(args);
}
rl.on("close", () => {
this.readline = null;
});
});
},
source: new CommandSource(
{
profile: {
name: bot._client.username,
},
uuid: bot._client.uuid,
},
{ console: true, discord: false },
),
2025-02-25 19:53:19 +00:00
2025-05-07 07:27:41 -04:00
refreshLine(...args) {
this.readline.output.write("\x1b[2K\r");
console.log.apply(console, arguments);
this.readline._refreshLine();
},
log(message) {
this.refreshLine(
bot
.getMessageAsPrismarine(
`§8[§1${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO" })} §3${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO" })} §6logs§8] §8[${serverName}§8] `,
)
?.toAnsi() + message,
);
},
warn(error) {
this.refreshLine(
prismarineChat
.fromNotch(
`§8[§1${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO" })} §3${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO" })} §ewarn§8] §8[${serverName}§8] `,
)
?.toAnsi() + error,
);
},
error(error) {
this.refreshLine(
bot
.getMessageAsPrismarine(
`§8[§1${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO" })} §3${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO" })} §4error§8] §8[${serverName}§8] `,
)
?.toAnsi() + error,
);
},
info(message) {
this.refreshLine(
prismarineChat
.fromNotch(
`§8[§1${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO" })} §3${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO" })} §2info§8] §8[${serverName}§8] `,
)
?.toAnsi() + message,
);
},
debug(message) {
console.log(
prismarineChat
.fromNotch(
`§8[§1${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO" })} §3${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO" })} §6debug§8] §8[${serverName}§8] `,
)
?.toAnsi() + message,
);
},
};
setInterval(() => (ratelimit = 0), 1000 * 2);
bot.on("message", (message) => {
if (options.logging.console) {
if (ratelimit > config.ratelimit.console) return;
2025-01-15 11:32:27 -05:00
2025-05-07 07:27:41 -04:00
bot.console.log(bot.getMessageAsPrismarine(message)?.toAnsi());
}
if (options.logging.file) {
bot.console.fileLogger(
`[${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO" })} ${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO" })} logs] [${serverName}] ${bot.getMessageAsPrismarine(message)?.toString()}`,
);
}
ratelimit++;
});
}
}
module.exports = ConsoleModule;