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
+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) => {
-7
View File
@@ -1,7 +0,0 @@
class database {
constructor (context) {
}
}
module.exports = database
+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`)
-28
View File
@@ -1,28 +0,0 @@
const crypto = require("crypto");
class validation {
constructor(context) {
const bot = context.bot;
const config = context.config;
setInterval(() => {
bot.validation = {
trusted: crypto
.createHash("sha256")
.update(Math.floor(Date.now() / 2000) + config.keys.trusted)
.digest("hex")
.substring(0, 16),
admin: crypto
.createHash("sha256")
.update(Math.floor(Date.now() / 2000) + config.keys.admin)
.digest("hex")
.substring(0, 16),
owner: crypto
.createHash("sha256")
.update(Math.floor(Date.now() / 2000) + config.keys.owner)
.digest("hex")
.substring(0, 16),
};
}, 100);
}
}
module.exports = validation