added authentication

This commit is contained in:
Parker2991
2025-08-18 16:54:25 -05:00
parent dc08bcad37
commit 06258b3a0c
15 changed files with 329 additions and 83 deletions
+120 -26
View File
@@ -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);
}
}*/
}
}
+61
View File
@@ -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
+3 -15
View File
@@ -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)
}
}
+6 -2
View File
@@ -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;
+2
View File
@@ -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
+32
View File
@@ -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;
+7 -3
View File
@@ -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,
+9 -11
View File
@@ -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++
},
};
+4 -7
View File
@@ -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);
+57 -12
View File
@@ -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:
+6 -2
View File
@@ -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) => {
+2 -2
View File
@@ -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`)
@@ -1,7 +1,6 @@
class database {
constructor (context) {
constructor(context) {
}
}
module.exports = database
+18
View File
@@ -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;