From 06258b3a0c16eab2213533b1cf79f4792a594e45 Mon Sep 17 00:00:00 2001 From: Parker2991 Date: Mon, 18 Aug 2025 16:54:25 -0500 Subject: [PATCH] added authentication --- src/commands/public/help.js | 146 ++++++++++++++++++++----- src/commands/trusted/auth.js | 61 +++++++++++ src/commands/trusted/cloop.js | 18 +-- src/index.js | 8 +- src/modules & plugins.md | 2 + src/modules/auth.js | 32 ++++++ src/modules/chat.js | 10 +- src/modules/command_core.js | 20 ++-- src/modules/command_handler.js | 11 +- src/modules/command_manager.js | 69 ++++++++++-- src/modules/console.js | 8 +- src/modules/selfcare.js | 4 +- src/{modules => plugins}/database.js | 5 +- src/{modules => plugins}/validation.js | 0 src/util/loadPlugins.js | 18 +++ 15 files changed, 329 insertions(+), 83 deletions(-) create mode 100644 src/commands/trusted/auth.js create mode 100644 src/modules & plugins.md create mode 100644 src/modules/auth.js rename src/{modules => plugins}/database.js (61%) rename src/{modules => plugins}/validation.js (100%) create mode 100644 src/util/loadPlugins.js diff --git a/src/commands/public/help.js b/src/commands/public/help.js index 1c42fdd..64aa7d9 100644 --- a/src/commands/public/help.js +++ b/src/commands/public/help.js @@ -20,6 +20,7 @@ class help extends CommandContext { const args = context.arguments; const config = context.config; const source = context.source; + const { MessageBuilder } = require('prismarine-chat')('1.20.2'); let component = []; let infoComponent = []; @@ -29,30 +30,126 @@ class help extends CommandContext { let admin = []; let owner = []; let Console = []; + let Commands = []; - let cat = { - translate: "(%s | %s | %s | %s)", - color: config.colors.commands.tertiary, - with: [ - { text: "Public", color: config.colors.help.public }, - { text: "Trusted", color: config.colors.help.trusted }, - { text: "Admin", color: config.colors.help.admin }, - { text: "Owner", color: config.colors.help.owner }, - ], - }; + function cat () { + return new MessageBuilder() + .setTranslate("(%s | %s | %s | %s)") + .setColor(config.colors.commands.tertiary) + .addWith( + new MessageBuilder() + .setText("Public") + .setColor(config.colors.help.public), + new MessageBuilder() + .setText("Trusted") + .setColor(config.colors.help.trusted), + new MessageBuilder() + .setText("Admin") + .setColor(config.colors.help.admin), + new MessageBuilder() + .setText("Owner") + .setColor(config.colors.help.owner) + ) + } + + function command_count () { + return new MessageBuilder() + .setTranslate("%s: (%s) %s \u203a") + .setColor(config.colors.commands.tertiary) + .addWith( + new MessageBuilder() + .setText("Commands") + .setColor(config.colors.commands.secondary), + new MessageBuilder() + .setText(`${bot.commandManager.commandlist.length}`) + .setColor(config.colors.integer), + cat() + ) + } + +// component.push(command_count() + '\n'); + + for (const commands of bot.commandManager.commandlist) { + switch (commands.data.trustLevel) { + case 0: + Public.push( + new MessageBuilder() + .setText('') + .addExtra( + new MessageBuilder() + .setText(commands.data.name + ' ') + .setColor(config.colors.help.public), + ) + ) + break + case 1: + trusted.push( + new MessageBuilder() + .setText('') + .addExtra( + new MessageBuilder() + .setText(commands.data.name + ' ') + .setColor(config.colors.help.trusted), + ) + ) + break + case 2: + admin.push( + new MessageBuilder() + .setText('') + .addExtra( + new MessageBuilder() + .setText(commands.data.name + ' ') + .setColor(config.colors.help.admin), + ) + ) + break + case 3: + owner.push( + new MessageBuilder() + .setText('') + .addExtra( + new MessageBuilder() + .setText(commands.data.name + ' ') + .setColor(config.colors.help.owner), + ) + ) + break + case 4: + Console.push( + new MessageBuilder() + .setText('') + .addExtra( + new MessageBuilder() + .setText(commands.data.name + ' ') + .setColor(config.colors.help.console), + ) + ) + break + } + + if ( + args[0]?.toLowerCase() === commands.data.name?.toLowerCase() || + commands.data.aliases.find( + (e) => e?.toLowerCase() === args[0]?.toLowerCase(), + ) + ) { + +/* for (const usages of commands.data.usages) { + + }*/ + } + } + if (Public.length > 0) Commands.push(Public, '\n') + if (trusted.length > 0) Commands.push(trusted, '\n') + if (admin.length > 0) Commands.push(admin, '\n') + if (owner.length > 0) Commands.push(owner) + if (source.sources.console && Console.length > 0) Commands.push('\n', Console) + + component.push([command_count(), '\n', Commands]) + bot.tellraw("@a", component) +/* - component.push({ - translate: "%s: (%s) %s \u203a\n", - color: config.colors.commands.tertiary, - with: [ - { text: "Commands", color: config.colors.commands.secondary }, - { - text: `${bot.commandManager.commandlist.length}`, - color: config.colors.integer, - }, - cat, - ], - }); for (const commands of bot.commandManager.commandlist) { switch (commands.data.trustLevel) { case 0: @@ -336,9 +433,6 @@ class help extends CommandContext { }); break; default: -/* bot.chat.message( - `fuck you ${commands.data.name} has a invalid trust level`, - );*/ } if ( @@ -442,7 +536,7 @@ class help extends CommandContext { ); } else { bot.tellraw("@a", component); - } + }*/ } } diff --git a/src/commands/trusted/auth.js b/src/commands/trusted/auth.js new file mode 100644 index 0000000..d5fda69 --- /dev/null +++ b/src/commands/trusted/auth.js @@ -0,0 +1,61 @@ +const CommandContext = require("../../command_util/command_context"); +const CommandTrustLevel = require('../../command_util/command_trust_level') +const CommandError = require('../../command_util/command_error'); +const trustLevel = new CommandTrustLevel() + +class auth extends CommandContext { + constructor() { + super("auth", ["authentication"], "auth through the bot so you dont need to use a hash everytime", trustLevel.trusted, [""], false); + } + execute(context) { + const bot = context.bot; + const args = context.arguments; + const source = context.source; + const config = context.config; + const uuid = source.player.uuid + const { MessageBuilder } = require('prismarine-chat')(bot.registry.version) + + if (bot.auth.list.find((e) => e?.player === uuid)) throw new CommandError(`You are already authed!`) + + if (source.player.hash === "trusted") { + source.player.authed = true + bot.auth.add({ + player: uuid, + trustLevel: "trusted" + }) + source.sendFeedback( + new MessageBuilder() + .setText("Authed as Trusted") + .setColor(config.colors.help.trusted) + ) + + } + else if (source.player.hash === "admin") { + source.player.authed = true + bot.auth.add({ + player: uuid, + trustLevel: "admin" + }) + source.sendFeedback( + new MessageBuilder() + .setText("Authed as Admin") + .setColor(config.colors.help.admin) + ) + } + + else if (source.player.hash === "owner") { + source.player.authed = true + bot.auth.add({ + player: source.player.uuid, + trustLevel: "owner" + }) + source.sendFeedback( + new MessageBuilder() + .setText("Authed as Owner") + .setColor(config.colors.help.owner) + ) + } + } +} + +module.exports = auth diff --git a/src/commands/trusted/cloop.js b/src/commands/trusted/cloop.js index 704105d..613793e 100644 --- a/src/commands/trusted/cloop.js +++ b/src/commands/trusted/cloop.js @@ -17,15 +17,9 @@ class cloop extends CommandContext { const bot = context.bot; const config = context.config; const source = context.source; - let args + const args = context.arguments let component = []; - if (!source.sources.console && !source.sources.discord) { - args = context.arguments.slice(16) - } else { - args = context.arguments - } - if ( !args && !args[0] && @@ -37,7 +31,7 @@ class cloop extends CommandContext { ) return; - switch (args[0]?.toLowerCase()) { + switch (args[0]) { case "add": if (isNaN(args[1])) throw new CommandError("invalid interval"); @@ -98,13 +92,7 @@ class cloop extends CommandContext { throw new CommandError("invalid argument"); } - if (source.sources.discord) { - - } else if (source.sources.console) { - bot.console.info(component) - } else { - bot.tellraw("@a", component); - } + source.sendFeedback(component) } } diff --git a/src/index.js b/src/index.js index f0757fe..8cf1184 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const readline = require("readline"); const fs = require("fs"); const path = require("path"); const loadModules = require("./util/loadModules") +const loadPlugins = require("./util/loadPlugins") const { Client, GatewayIntentBits, Partials } = require("discord.js"); const { MessageContent, GuildMessages, Guilds, DirectMessages } = GatewayIntentBits; const discordClient = new Client({ @@ -31,16 +32,19 @@ const rl = readline.createInterface({ if (config.discord.enabled) discordClient.login(config.discord.token); const bots = []; +let options; let bot; -for (const options of config.bots) { +for (const botOptions of config.bots) { + options = botOptions bot = new createBot(options, config); + bots.push(bot); bot.bots = bots; loadModules(bot, options, config, discordClient); bot.console.readlineInterface(rl); } - +loadPlugins(bot, options, config, discordClient) discordClient.on("messageCreate", (message) => { try { if (message.author.id === bot.discord.client.user.id) return; diff --git a/src/modules & plugins.md b/src/modules & plugins.md new file mode 100644 index 0000000..debf893 --- /dev/null +++ b/src/modules & plugins.md @@ -0,0 +1,2 @@ +yeah i know theres plugins and modules now but i didnt know what else to name it but +modules are for files loaded from the bot array and plugins are for files needed to be loaded outside the bot array as a lazy fix diff --git a/src/modules/auth.js b/src/modules/auth.js new file mode 100644 index 0000000..713f03d --- /dev/null +++ b/src/modules/auth.js @@ -0,0 +1,32 @@ +class auth { + constructor (context) { + const bot = context.bot + const options = context.options + const config = context.config + + bot.auth = { + list: [], + find (player) { + this.list.find() + }, + add (player) { + this.list.push(player) + }, + remove (player) { + this.list.splice(player, 1); + }, + clear () { + this.list = []; + }, + } + + bot.on('end', () => bot.auth.clear()) + + bot.on('player_left', (data) => { + const players = bot.auth.list.find((e) => e?.player === data[0])?.player + if (players === data[0]) bot.auth.remove(data) + }) + } +} + +module.exports = auth; \ No newline at end of file diff --git a/src/modules/chat.js b/src/modules/chat.js index 9833303..f6840a8 100644 --- a/src/modules/chat.js +++ b/src/modules/chat.js @@ -22,6 +22,7 @@ class chat { bot.on("registry_ready", (registry) => { ChatMessage = loadPrismarineChat(registry); }); + if (options.mode === "savageFriends") { bot.chatParsers = [CreayunChatParser]; } else { @@ -68,12 +69,10 @@ class chat { ]); break; } - tryParsingMessage(message, { senderName: sender, players: bot.players, chatType: "profileless", getMessageAsPrismarine: bot.getMessageAsPrismarine }) }); bot.on("packet.player_chat", (packet, data) => { let unsigned; - unsigned = tryParse(packet.unsignedChatContent); switch (packet.type) { @@ -138,7 +137,11 @@ class chat { message = tryParse(packet.content); - +// console.log(packet.content) + /* +{"translate":"%s","with":[{"color":"red", +"translate":"chomens_bot.command_handler.invalid_hash","fallback":"Invalid %s hash","with":["TRUSTED"]},"chomens_bot_command_output"]} + */ if ( message.translate === "advMode.setCommand.success" && config?.debug?.commandSetMessage === false @@ -155,6 +158,7 @@ class chat { bot.emit("message", message); bot.emit("system_chat", message) + if (bot.options.mode !== "savageFriends") return tryParsingMessage(message, { players: bot.players, getMessageAsPrismarine: bot.getMessageAsPrismarine, diff --git a/src/modules/command_core.js b/src/modules/command_core.js index efb90c6..5c884c8 100644 --- a/src/modules/command_core.js +++ b/src/modules/command_core.js @@ -144,6 +144,7 @@ class command_core { mode: mode, flags: flags, }); + }, run(command) { @@ -169,7 +170,9 @@ class command_core { } }, - async runTracked(command, source, selector) { + transaction_id: 0, // setting it 10 just in case the command block was already used so it doesnt send more than 1 message + + async runTracked(command, source) { const location = bot.core.currentBlock(); let transactionId = Math.floor(Math.random() * 10000); @@ -185,22 +188,17 @@ class command_core { try { if (data.transactionId == transactionId) { if (data?.nbt?.value?.LastOutput) { - const output = JSON.parse(data.nbt.value.LastOutput.value).extra - - if (source.sources.console) { - - } else { - bot.tellraw( - "@a", - output, - ); - } + const output = convertNbtComponentToJson(null, (data.nbt.value.LastOutput.value).extra) + source.sendFeedback(output) } } } catch (e) { console.log(e.stack); } }); + + await sleep(100) + this.transaction_id++ }, }; diff --git a/src/modules/command_handler.js b/src/modules/command_handler.js index 982f371..b4b3603 100644 --- a/src/modules/command_handler.js +++ b/src/modules/command_handler.js @@ -27,10 +27,10 @@ class command_handler { ); source.sendFeedback = (message) => { - if (source.sources.console) bot.console.command(message); + if (source.sources.console) bot.console.command(); else if (source.sources.discord) { - // i dont know what to do for discord yet - } else bot.tellraw(`@a[name="${source.player.profile.name}"]`, message); + return message // lazy + } else bot.tellraw(`@a[name="${source.player.profile.name}"]`, [message]); } if (command.split(' ')[1] === bot.validation.trusted) source.player.hash = "trusted"; @@ -41,10 +41,7 @@ class command_handler { ratelimit--; }, 1000); if (ratelimit > config.ratelimit.commands) { - bot.tellraw("@a", { - text: "You are using commands too fast!", - color: "dark_red", - }); + return } else if (command.split(" ")[0].length === 0) { } else { bot.commandManager.executeString(source, command); diff --git a/src/modules/command_manager.js b/src/modules/command_manager.js index 1ebf578..73a6325 100644 --- a/src/modules/command_manager.js +++ b/src/modules/command_manager.js @@ -6,6 +6,7 @@ const fixansi = require("../util/ansi"); const sleep = require("../util/sleep.js"); function unknownCommand (MessageBuilder, commandName, bot) { + return new MessageBuilder() .setTranslate("%s%s%s %s") .setColor("red") @@ -31,13 +32,12 @@ class command_manager { const discordClient = context.discordClient; const options = context.options; const { MessageBuilder } = require('prismarine-chat')(bot._client.version) - bot.commandManager = { commands: {}, commandlist: [], execute(source, commandName, args) { const command = this.getCommand(commandName.toLowerCase()); - + const authed = bot.auth.list try { if (!command) { if (source?.sources?.console) bot.console.command( @@ -48,8 +48,6 @@ class command_manager { const event = bot.discord.message; const roles = event?.member?.roles?.cache; -// console.log(command) -// console.log(source) switch (command?.data?.trustLevel) { case 0: @@ -70,20 +68,40 @@ class command_manager { .setColor("red") ) } else if (!source?.sources.console) { - if (args.length === 0) throw new CommandError( + + if (args.length === 0 && ( + authed.find((e) => e?.player === source.player.uuid)?.trustLevel !== "trusted" + && + authed.find((e) => e?.player === source.player.uuid)?.trustLevel !== "admin" + && + authed.find((e) => e?.player === source.player.uuid)?.trustLevel !== "owner" + )) throw new CommandError( new MessageBuilder() .setText("Please provide a trusted, admin, or owner hash") .setColor("red") ) if ( - args[0] !== bot.validation.trusted && + (args[0] !== bot.validation.trusted && args[0] !== bot.validation.admin && - args[0] !== bot.validation.owner + args[0] !== bot.validation.owner) + && + ( + authed.find((e) => e.player === source.player.uuid)?.trustLevel !== "trusted" + && + authed.find((e) => e.player === source.player.uuid)?.trustLevel !== "admin" + && + authed.find((e) => e.player === source.player.uuid)?.trustLevel !== "owner" + ) ) throw new CommandError( new MessageBuilder() .setText("Invalid Hash") .setColor("red") ) + if (source.player.authed) { + args = args + } else { + args = args.slice(1) + } } break; case 2: @@ -100,20 +118,39 @@ class command_manager { .setColor("red") ) } else if (!source?.sources?.console) { - if (args.length === 0) + if (args.length === 0 && ( + authed.find((e) => e?.player === source.player.uuid)?.trustLevel !== "admin" + && + authed.find((e) => e?.player === source.player.uuid)?.trustLevel !== "owner" + )) throw new CommandError( new MessageBuilder() .setText('Please provide a hash') .setColor('red') ); if ( - args[0] !== bot.validation.admin && - args[0] !== bot.validation.owner + ( + args[0] !== bot.validation.admin && + args[0] !== bot.validation.owner + ) + && + ( + authed.find((e) => e?.player === source.player.uuid)?.trustLevel !== "admin" + && + authed.find((e) => e?.player === source.player.uuid)?.trustLevel !== "owner" + ) ) throw new CommandError( new MessageBuilder() .setText('Invalid Hash') + .setColor("red") ); + + if (source.player.authed) { + args = args + } else { + args = args.slice(1) + } } break; case 3: @@ -130,19 +167,27 @@ class command_manager { .setColor("red") ) } else if (!source?.sources?.console) { - if (args.length === 0 && bot.validation.owner) + if (args.length === 0 && bot.validation.owner && authed.find((e) => e?.player === source.player.uuid)?.trustLevel !== "owner") throw new CommandError( new MessageBuilder() .setText("Please provide a hash") .setColor('red') ); - if (args[0] !== bot.validation.owner) + if (args[0] !== bot.validation.owner && ( + authed.find((e) => e?.player === source.player.uuid)?.trustLevel !== "owner" + )) throw new CommandError( new MessageBuilder() .setText('Invalid Hash') .setColor('red') ); + + if (source.player.authed) { + args = args; + } else { + args = args.slice(1) + } } break; case 4: diff --git a/src/modules/console.js b/src/modules/console.js index a4576ab..a7b4396 100644 --- a/src/modules/console.js +++ b/src/modules/console.js @@ -44,7 +44,7 @@ class Console { // named it Console so that it doesnt override the console varia }, uuid: bot._client.uuid, }, - { console: true, discord: false }, + { console: true, discord: false }, ), refreshLine(...args) { @@ -103,10 +103,14 @@ class Console { // named it Console so that it doesnt override the console varia .fromNotch( `§8[§1${new Date().toLocaleTimeString("en-US", { timeZone: "America/CHICAGO" })} §3${new Date().toLocaleDateString("en-US", { timeZone: "America/CHICAGO" })} §6command§8] §8[${serverName}§8] `, ) - ?.toAnsi() + prismarineChat.fromNotch(message)?.toAnsi(bot.registry.language), + ?.toAnsi() + bot.getMessageAsPrismarine(message)?.toAnsi(bot.registry.language), ); } }; + + bot.console.source.sendFeedback = (message) => { + bot.console.command(message) + } setInterval(() => (ratelimit = 0), 1000 * 2); bot.on("message", (message) => { diff --git a/src/modules/selfcare.js b/src/modules/selfcare.js index 5680aaa..2483745 100644 --- a/src/modules/selfcare.js +++ b/src/modules/selfcare.js @@ -23,7 +23,6 @@ class selfcare { } let selfcare = []; - for (const file of fs.readdirSync(path.join(__dirname, '../selfcare/extras'))) { try { const extrasSelfcare = require(path.join(__dirname, '../selfcare/extras', file)) @@ -52,6 +51,7 @@ class selfcare { } } + bot.on('system_chat', (message) => { const stringMessage = bot.getMessageAsPrismarine(message)?.toString() @@ -69,7 +69,7 @@ class selfcare { } else { if (!bot.selfcare.prefix) bot.chat.command(`extras:prefix &8[&bPrefix&8: &3${config.prefixes[0]}&8]`); else if (bot.selfcare.permissionLevel < 2) bot.chat.command('minecraft:op @s[type=player]') - else if (bot.selfcare.username) bot.chat.command(`extras:username ${bot._client.username}`) + // else if (bot.selfcare.username) bot.chat.command(`extras:username ${bot._client.username}`) else if (bot.selfcare.gameMode !== 1) bot.chat.command('minecraft:gamemode creative') else if (!bot.selfcare.commandSpy) bot.core.run(`commandspy ${bot.uuid} on`) else if (!bot.selfcare.vanished) bot.core.run(`essentials:vanish ${bot._client.username} on`) diff --git a/src/modules/database.js b/src/plugins/database.js similarity index 61% rename from src/modules/database.js rename to src/plugins/database.js index 5e4d585..9368ac9 100644 --- a/src/modules/database.js +++ b/src/plugins/database.js @@ -1,7 +1,6 @@ class database { - constructor (context) { - + constructor(context) { + } } - module.exports = database \ No newline at end of file diff --git a/src/modules/validation.js b/src/plugins/validation.js similarity index 100% rename from src/modules/validation.js rename to src/plugins/validation.js diff --git a/src/util/loadPlugins.js b/src/util/loadPlugins.js new file mode 100644 index 0000000..9a528bb --- /dev/null +++ b/src/util/loadPlugins.js @@ -0,0 +1,18 @@ +const fs = require("fs"); +const path = require("path"); +async function loadPlugins(bot, options, config, discordClient) { + bot.plugins = []; + for (const filename of fs.readdirSync(path.join(__dirname, "../", "plugins"))) { + try { + if (filename.endsWith(".js")) { + const plugin = require(path.join(__dirname, "../plugins", filename)); + bot.plugins.push(module); + new plugin({ bot, options, config, discordClient }); + } + } catch (error) { + console.error(`Failed to load plugin ${filename} due to error`); + console.error(`\x1b[31m${error.stack}\x1b[0m`); + } + } +} +module.exports = loadPlugins;