diff --git a/.gitignore b/.gitignore index 4e9c0e9..72fd450 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ node_modules build # ignore config -config.yml \ No newline at end of file +config.toml + +# ignore .git folder +.git \ No newline at end of file diff --git a/framework/bot.sh b/framework/bot.sh new file mode 100644 index 0000000..8574f9e --- /dev/null +++ b/framework/bot.sh @@ -0,0 +1 @@ +node --max-old-space-size=1000 --expose-gc build/index.js \ No newline at end of file diff --git a/framework/init.sh b/framework/init.sh new file mode 100644 index 0000000..ee1d011 --- /dev/null +++ b/framework/init.sh @@ -0,0 +1 @@ +sh ./framework/keepalive.sh \ No newline at end of file diff --git a/framework/keepalive.sh b/framework/keepalive.sh new file mode 100755 index 0000000..f3966cd --- /dev/null +++ b/framework/keepalive.sh @@ -0,0 +1,8 @@ +echo "starting reset process at $HOME/keepalive" + +dtach -n $HOME/keepalive sh ./framework/reset.sh + +while true; do + sh ./framework/bot.sh + sleep 5 +done diff --git a/framework/reset.sh b/framework/reset.sh new file mode 100644 index 0000000..b1bd37c --- /dev/null +++ b/framework/reset.sh @@ -0,0 +1,4 @@ +while true; do + sleep 24h + pkill -f "node --max-old-space-size=1000 --expose-gc build/index.js" +done \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 929d084..d430bef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,18 @@ { - "name": "typescript", + "name": "FNFBoyfriendBot-v9", "lockfileVersion": 3, "requires": true, "packages": { "": { "dependencies": { "minecraft-protocol": "^1.62.0", + "prismarine-chat": "^1.12.0", + "prismarine-registry": "^1.11.0", + "smol-toml": "^1.5.2", "yaml": "^2.8.2" }, "devDependencies": { - "@types/node": "^24.10.1" + "@types/node": "^24.10.2" } }, "node_modules/@azure/msal-common": { @@ -36,9 +39,9 @@ } }, "node_modules/@types/node": { - "version": "24.10.1", - "resolved": "https://registry.npmjs.org/@types/node/-/node-24.10.1.tgz", - "integrity": "sha512-GNWcUTRBgIRJD5zj+Tq0fKOJ5XZajIiBroOF0yvj2bSU1WvNdYS/dn9UxwsujGW4JX06dnHyjV2y9rRaybH0iQ==", + "version": "24.10.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-24.10.2.tgz", + "integrity": "sha512-WOhQTZ4G8xZ1tjJTvKOpyEVSGgOTvJAfDK3FNFgELyaTpzhdgHVHeqW8V+UJvzF5BT+/B54T/1S2K6gd9c7bbA==", "license": "MIT", "dependencies": { "undici-types": "~7.16.0" @@ -310,12 +313,12 @@ "license": "MIT" }, "node_modules/jsonwebtoken": { - "version": "9.0.2", - "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", - "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "version": "9.0.3", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.3.tgz", + "integrity": "sha512-MT/xP0CrubFRNLNKvxJ2BYfy53Zkm++5bX9dtuPbqAeQpTVe0MQTFhao8+Cp//EmJp244xt6Drw/GVEGCUj40g==", "license": "MIT", "dependencies": { - "jws": "^3.2.2", + "jws": "^4.0.1", "lodash.includes": "^4.3.0", "lodash.isboolean": "^3.0.3", "lodash.isinteger": "^4.0.4", @@ -332,9 +335,9 @@ } }, "node_modules/jwa": { - "version": "1.4.2", - "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.2.tgz", - "integrity": "sha512-eeH5JO+21J78qMvTIDdBXidBd6nG2kZjg5Ohz/1fpa28Z4CcsWUzJ1ZZyFq/3z3N17aZy+ZuBoHljASbL1WfOw==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-2.0.1.tgz", + "integrity": "sha512-hRF04fqJIP8Abbkq5NKGN0Bbr3JxlQ+qhZufXVr0DvujKy93ZCbXZMHDL4EOtodSbCWxOqR8MS1tXA5hwqCXDg==", "license": "MIT", "dependencies": { "buffer-equal-constant-time": "^1.0.1", @@ -343,12 +346,12 @@ } }, "node_modules/jws": { - "version": "3.2.2", - "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", - "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/jws/-/jws-4.0.1.tgz", + "integrity": "sha512-EKI/M/yqPncGUUh44xz0PxSidXFr/+r0pA70+gIYhjv+et7yxM+s29Y+VGDkovRofQem0fs7Uvf4+YmAdyRduA==", "license": "MIT", "dependencies": { - "jwa": "^1.4.1", + "jwa": "^2.0.1", "safe-buffer": "^5.0.1" } }, @@ -595,9 +598,9 @@ } }, "node_modules/prismarine-realms": { - "version": "1.3.2", - "resolved": "https://registry.npmjs.org/prismarine-realms/-/prismarine-realms-1.3.2.tgz", - "integrity": "sha512-5apl9Ru8veTj5q2OozRc4GZOuSIcs3yY4UEtALiLKHstBe8bRw8vNlaz4Zla3jsQ8yP/ul1b1IJINTRbocuA6g==", + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/prismarine-realms/-/prismarine-realms-1.4.0.tgz", + "integrity": "sha512-db3iin0ZOYWC7Ye65VSo5kKz8/hWatdDECWchTZG4yWQDC7wHrUMLvmPSnEEG0rdDwq6pMUUedss9dp8D550pA==", "license": "MIT", "dependencies": { "debug": "^4.3.3", @@ -746,6 +749,18 @@ "npm": ">= 3.0.0" } }, + "node_modules/smol-toml": { + "version": "1.5.2", + "resolved": "https://registry.npmjs.org/smol-toml/-/smol-toml-1.5.2.tgz", + "integrity": "sha512-QlaZEqcAH3/RtNyet1IPIYPsEWAaYyXXv1Krsi+1L/QHppjX4Ifm8MQsBISz9vE8cHicIq3clogsheili5vhaQ==", + "license": "BSD-3-Clause", + "engines": { + "node": ">= 18" + }, + "funding": { + "url": "https://github.com/sponsors/cyyynthia" + } + }, "node_modules/string_decoder": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", diff --git a/package.json b/package.json index cfa15fc..75472e3 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,12 @@ { "dependencies": { "minecraft-protocol": "^1.62.0", + "prismarine-chat": "^1.12.0", + "prismarine-registry": "^1.11.0", + "smol-toml": "^1.5.2", "yaml": "^2.8.2" }, "devDependencies": { - "@types/node": "^24.10.1" + "@types/node": "^24.10.2" } } diff --git a/src/bot.ts b/src/bot.ts index 43dd2aa..22f55e9 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,17 +1,25 @@ import mc from "minecraft-protocol"; import { EventEmitter } from "events"; -class createBot { +export class createBot { bot: any bots: any; - constructor (options) { + constructor (options: any) { this.bot = new EventEmitter(); const bot = this.bot; + bot.options = { + host: (options.host ??= "localhost"), + username: (options.username ??= "Player"), + version: (options.version ??= "1.21.8"), + hideErrors: (options.hideErrors ??= true), + } + //options.hideError = true; + bot.on("init_client", (client: any) => { client?.on('packet', (data: any, meta: any) => { bot.emit("packet", data, meta); @@ -31,4 +39,4 @@ class createBot { return bot; } } -export = createBot \ No newline at end of file +//export = createBot \ No newline at end of file diff --git a/src/chatParsers/kaboom.ts b/src/chatParsers/kaboom.ts new file mode 100644 index 0000000..55f35aa --- /dev/null +++ b/src/chatParsers/kaboom.ts @@ -0,0 +1,40 @@ +import util from 'util'; + +export default function kaboom (message: any, data: any) { + if (message === null || typeof message !== 'object') return + if (message.text !== '' || !Array.isArray(message.extra) || message.extra.length < 3) return + + const children = message.extra + + const prefix = children[0] + let displayName = data.senderName ?? { text: '' } + let contents = { text: '' } + + if (isSeparatorAt(children, 1)) { // Missing/blank display name + if (children.length > 3) contents = children[3] + } else if (isSeparatorAt(children, 2)) { + displayName = children[1] + if (children.length > 4) contents = children[4] + } else { + return undefined + } + + const playerListDisplayName = { extra: [prefix, displayName], text: '' } + + let sender + + if (data.senderUuid) { + sender = data.players.find((player: any) => player.uuid === data.senderUuid) + } else { + const playerListDisplayName = { extra: [prefix, displayName], text: '' } + sender = data.players.find((player: any) => util.isDeepStrictEqual(player.displayName, playerListDisplayName)) + } + + if (!sender) return undefined + + return { sender, contents, type: "minecraft:chat", displayName, chatType: data.chatType } +} + +function isSeparatorAt (children: any, start: any) { + return (children[start]?.text === ':' || children[start]?.text === '\xa7f:') && children[start + 1]?.text === ' ' +} diff --git a/src/data/version.json b/src/data/version.json index 4459274..5f60ffd 100644 --- a/src/data/version.json +++ b/src/data/version.json @@ -1,6 +1,5 @@ { "build": "5000", - "codename": "§bRedux", + "codename": "§cReimu", "version": "v9.0.0-alpha" -} - +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index b888ec1..c6af17a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,24 +1,14 @@ -import { parse } from 'yaml'; import fs from "fs"; import path from "path" -import createBot from "./bot" +import { createBot } from "./bot" +import { parseToml } from './util/parseToml'; import loadModules from './util/loadModules'; -const file = fs.readFileSync(path.join(__dirname, '../config.yml'), "utf8"); -const config = parse(file) +const config = parseToml('./config.toml'); const bots = [] for (const options of config.bots) { - const bot = new createBot(options) - - //const loadModules = lm - //console.log(loadModules + const bot = new createBot(options); + loadModules(bot, config) bots.push(bot); - bot.bots = bots; - - new loadModules(bot, config) - -/* bot.on('packet.player_chat', (packet: any) => { - console.log(packet) - })*/ } \ No newline at end of file diff --git a/src/modules/chat.ts b/src/modules/chat.ts index 173c18c..9d4fc79 100644 --- a/src/modules/chat.ts +++ b/src/modules/chat.ts @@ -1,20 +1,178 @@ -class chat { - constructor (context) { - const bot = context.bot; - const config = context.config; +import convertNbtComponentToJson from "../util/convertNbtComponentToJson"; +import loadPrismarineChat from 'prismarine-chat'; +import kaboom from '../chatParsers/kaboom'; - bot.on('packet.player_chat', (data: any) => { - - }) +// taken from FNFBoyfriendBot v8.0.0 - bot.on('packet.system_chat', (data: any) => { +function parseChatType (chatTypes: any, type: any, sender: any, target: any, message: any) { + const chatType = chatTypes[type] + const parsed = convertNbtComponentToJson(null, chatType.value.value.chat) + const array: any[] = [] + for (const parameter of parsed.parameters) { - }); - - bot.on('packet.system_chat', (data: any) => { - - }) + switch (parameter) { + case "sender": array.push(sender) + break; + case "target": array.push(target) + case "content": array.push(message) + break; + } } + let json = { translate: parsed.translation_key, with: array } + + if (type === 4) json = message + + return json } -export = chat \ No newline at end of file +export default class chat { + constructor (context: any) { + const bot = context.bot; + const config = context.config; + const options = bot.options; + let ChatMessage: any; + let chatTypes: any; + bot.on("registry_ready", (registry: any) => { + ChatMessage = loadPrismarineChat(registry); + }); + + bot.on('packet.registry_data', (packet: any) => { + if (packet.id !== 'minecraft:chat_type') return // taken from the 1.21.1 build of my bot that chayapak made + chatTypes = packet.entries + }) + + bot.chatParsers = [kaboom] + /* if (options.mode === "savageFriends") { + bot.chatParsers = [creayun]; + } else { + bot.chatParsers = [ + kaboom, + creayun, + chipmunkmod, + vanilla, + me + ]; + }*/ + + bot.on("packet.profileless_chat", (packet: any) => { + const sender = convertNbtComponentToJson(null, packet.name); + const message = convertNbtComponentToJson(null, packet.message); + const target = convertNbtComponentToJson(null, packet.target) + const type = packet.type.chatType; + const parsed = parseChatType(chatTypes, type, sender, target, message) + bot.emit('message', { + type: "minecraft:disguised_chat", + message: parsed + }) + + console.log(packet) + parseMessage(parsed, { senderName: sender, players: bot.players, getMessageAsPrismarine: bot.getMessageAsPrismarine, chatType: "minecraft:diguised_chat" }) + }); + + bot.on("packet.player_chat", (packet: any) => { + const unsigned = convertNbtComponentToJson(null, packet.unsignedChatContent); + const networkName = convertNbtComponentToJson(null, packet.networkName) + bot.emit('message', { + type: "minecraft:player_chat", + message: unsigned + }) + + parseMessage(unsigned, { + senderUuid: packet.senderUuid, + players: bot.players, + getMessageAsPrismarine: bot.getMessageAsPrismarine, + chatType: "minecraft:player_chat" + }); + }); + + bot.on("packet.system_chat", (packet: any) => { + const message = convertNbtComponentToJson(null, packet.content); + bot.emit("system_chat", message); + if ( + // message.translate === "advMode.setCommand.success" + //|| + message.translate === "multiplayer.message_not_delivered" + || + packet.isActionBar + || + message.translate === "advMode.notAllowed" + ) + return; + + bot.emit("message", { + type: "minecraft:system_chat", + message: message + }); + + parseMessage(message, { + players: bot.players, + getMessageAsPrismarine: bot.getMessageAsPrismarine, + chatType: "minecraft:system_chat", + }); + }); + + function parseMessage(message: any, data: any) { + try { + let parsed; + + for (const parser of bot.chatParsers) { + parsed = parser(message, data); + if (parsed) break; + } + + if (!parsed) return; + bot.emit("parsed_message", parsed); + } catch (e: unknown) { + console.log(e); + } + } + + bot.getMessageAsPrismarine = (message: string) => { + try { + if (ChatMessage !== undefined) { + return new ChatMessage(message); + } + } catch {} + + return undefined; + }; + + bot.chat = { + message(message: any) { + const acc = 0; + const bitset = Buffer.allocUnsafe(3); + bitset[0] = acc & 0xff; + bitset[1] = (acc >> 8) & 0xff; + bitset[2] = (acc >> 16) & 0xff; + bot._client.write("chat_message", { + message: message?.substring(0, 256)?.replaceAll("§", "&"), + timestamp: BigInt(Date.now()), + // salt: 0n, + offset: 0, + acknowledged: bitset, + }); + }, + + command(command: any) { + bot._client.write("chat_command", { + command: command?.substring(0, 256)?.replaceAll("§", "&"), + timestamp: BigInt(Date.now()), + // salt: 0n, + argumentSignatures: [], + signedPreview: false, + messageCount: 0, + acknowledged: Buffer.alloc(3), + previousMessages: [], + }); + }, + + send(message: any) { + if (message.startsWith("/")) { + bot.chat.command(message.substring(1)); + return; + } + bot.chat.message(message); + } + } + } +} \ No newline at end of file diff --git a/src/modules/commandHandler.ts b/src/modules/commandHandler.ts new file mode 100644 index 0000000..d990ff6 --- /dev/null +++ b/src/modules/commandHandler.ts @@ -0,0 +1,11 @@ +export default class commandHandler { + constructor (context: any) { + const bot = context.bot; + const config = context.config; + const options = bot.options; + + bot.on('parsed_message', (packet: any) => { + console.log(packet) + }) + } +} \ No newline at end of file diff --git a/src/modules/console.ts b/src/modules/console.ts index 3b819bf..ee4e942 100644 --- a/src/modules/console.ts +++ b/src/modules/console.ts @@ -1,7 +1,8 @@ -class console { - constructor (context) { - +export default class console { + constructor (context: any) { + const bot = context.bot; + const config = context.config; + } } -export = console; diff --git a/src/modules/playerList.ts b/src/modules/playerList.ts new file mode 100644 index 0000000..b224d34 --- /dev/null +++ b/src/modules/playerList.ts @@ -0,0 +1,103 @@ +const convertNbtComponentToJson = require("../util/convertNbtComponentToJson"); +const entityUUID = require('../util/entityUUID'); + +// taken from FNFBoyfriendBot v8.0.0 + +export default class playerList { + constructor(context: any) { + const bot = context.bot; + bot.players = []; + + bot.on("packet.player_info", (packet: any) => { + const actions = []; + if (packet.action._value & 0b000001) actions.push(addPlayer); + if (packet.action._value & 0b000010) actions.push(initializeChat); + if (packet.action._value & 0b000100) actions.push(updateGamemode); + if (packet.action._value & 0b001000) actions.push(updateListed); + if (packet.action._value & 0b010000) actions.push(updateLatency); + if (packet.action._value & 0b100000) actions.push(updateDisplayName); + for (const entry of packet.data) { + for (const action of actions) { + action(entry); + } + } + }); + + bot.on("packet.player_remove", async ({ players }: { players: any }) => { + // players has uuids of the players + let player_completion = ( + await bot.tab_complete("scoreboard players add ") + ).filter((_: any) => _.tooltip == undefined); // exclude @a, @r, @s, @e, @p -aaa + bot.players.forEach(async (player: any) => { + if (!players.includes(player.uuid)) return; + + const a = player_completion.filter( + (_: any) => _.match == player.profile.name, + ); + if (a.length >= 1) { + player.vanished = true; + } else { + bot.players = bot.players.filter((_: any) => _.uuid != player.uuid); + } + }); + console.log(playerList) + }); + + function addPlayer(entry: any) { + bot.players = bot.players.filter((_entry: any) => _entry.uuid !== entry.uuid); + bot.players.push({ + uuid: entry.uuid, + //entityUuid: entityUUID(entry.uuid), + profile: { + name: entry.player.name, + properties: entry.player.properties, + }, + chatSession: undefined, + gamemode: undefined, + listed: undefined, + latency: undefined, + displayName: undefined, + prefix: undefined, + vanished: false, + }); + + //bot.emit('player_info', entry) + } + + function initializeChat(entry: any) {} + + function updateGamemode(entry: any) { + const target = bot.players.find((_entry: any) => _entry.uuid === entry.uuid); + if (!target) return; + + target.gamemode = entry.gamemode; + } + + function updateListed(entry: any) { + const target = bot.players.find((_entry: any) => _entry.uuid === entry.uuid); + if (!target) return; + target.listed = entry.listed; + } + + function updateLatency(entry: any) { + const target = bot.players.find((_entry: any) => _entry.uuid === entry.uuid); + if (!target) return; + + target.latency = entry.latency; + } + + function updateDisplayName(entry: any) { + const target = bot.players.find((_entry: any) => _entry.uuid === entry.uuid); + if (!target) return; + + try { + target.displayName = convertNbtComponentToJson(null, entry.displayName); + // if (bot.options.mode === "kaboom") target.prefix = convertNbtComponentToJson(null, entry.displayName).extra[0] + } catch { + // do nothing + } + } + + bot.on("end", () => (bot.players = [])); + } +} \ No newline at end of file diff --git a/src/modules/registry.ts b/src/modules/registry.ts new file mode 100644 index 0000000..a283ddf --- /dev/null +++ b/src/modules/registry.ts @@ -0,0 +1,20 @@ +import createRegistry from 'prismarine-registry'; + +export default class registry { + constructor(context: any) { + const bot = context.bot; + bot.once("packet.login", (packet: any) => { + bot.registry = createRegistry(bot._client.version); + // bot.registry.language = require("../data/language.json"); + bot.emit("registry_ready", bot.registry); + }); + + +/* bot.on('packet.registry_data', async (packet: ant) => { + await sleep(1000) + if (bot.loggedIn) { + bot.registry.loadDimensionCodec(packet) + } + })*/ + } +} \ No newline at end of file diff --git a/src/modules/tabcomplete.ts b/src/modules/tabcomplete.ts new file mode 100644 index 0000000..ebd66ad --- /dev/null +++ b/src/modules/tabcomplete.ts @@ -0,0 +1,22 @@ +// taken from FNFBoyfriendBot v8.0.0 + +export default class tabComplete { + constructor(context: any) { + const bot = context.bot; + // let aaa cook + bot.tab_complete = (str: any) => { + return new Promise((resolve) => { + bot._client.write("tab_complete", { + text: str, + assumeCommand: false, + sendBlockInSight: false, + }); + const tab_completeH = (packet: any) => { + bot._client.removeListener("tab_complete", tab_completeH); + resolve(packet.matches); + }; + bot._client.once("tab_complete", tab_completeH); + }); + }; + } +} \ No newline at end of file diff --git a/src/util/commandLineArgs.ts b/src/util/commandLineArgs.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/util/convertNbtComponentToJson.js b/src/util/convertNbtComponentToJson.js new file mode 100644 index 0000000..1ecda8c --- /dev/null +++ b/src/util/convertNbtComponentToJson.js @@ -0,0 +1,65 @@ +// taken from FNFBoyfriendBot v8.0.0 + +const BOOLEAN_TYPES = [ + "interpret", + "bold", + "italic", + "underlined", + "strikethrough", + "obfuscated", +]; +function convertNbtComponentToJson(key, tag) { + if (!tag) { + return null; + } else if (tag.type === "compound") { + const object = {}; + + for (const key of Object.keys(tag.value)) { + object[key === "" ? "text" : key] = convertNbtComponentToJson( + key, + tag.value[key], + ); + } + + return object; + } else if (tag.type === "list") { + const array = []; + + for (const item of tag.value.value) { + array.push( + convertNbtComponentToJson(null, { type: tag.value.type, value: item }), + ); + } + + return array; + } else if (tag.type === "byte") { + if (key && BOOLEAN_TYPES.includes(key)) return tag.value !== 0; + + return tag.value; + } else if (tag.type === "int") { + return tag.value; + } else if (tag.type === "string") { + return tag.value; + } else if (tag.type === "float") { + return tag.value + } else if (tag.type === "intArray") { + const array = []; + + for (const value of tag.value) { + array.push(value); + } + + return array; + } else if (tag.type === "short") { + return tag.value; + } else if (tag.type === "long") { + return tag.value + } else if (tag.type === "double") { + return tag.value + } else { + console.log("Failed to parse NBT component type " + tag.type); + + return { text: "" }; + } +} +module.exports = convertNbtComponentToJson; \ No newline at end of file diff --git a/src/util/convertNbtComponentToJson.ts b/src/util/convertNbtComponentToJson.ts deleted file mode 100644 index 6fdca98..0000000 --- a/src/util/convertNbtComponentToJson.ts +++ /dev/null @@ -1,65 +0,0 @@ -const BOOLEAN_TYPES = [ - "interpret", - "bold", - "italic", - "underlined", - "strikethrough", - "obfuscated", -]; -//function convertNbtComponentToJson(key, tag) { -export default { - convertNbtComponentToJson (key, tag) { - if (!tag) { - return null; - } else if (tag.type === "compound") { - const object = {}; - - for (const key of Object.keys(tag.value)) { - object[key === "" ? "text" : key] = this.convertNbtComponentToJson( - key, - tag.value[key], - ); - } - - return object; - } else if (tag.type === "list") { - const array = []; - - for (const item of tag.value.value) { - array.push( - this.convertNbtComponentToJson(null, { type: tag.value.type, value: item }), - ); - } - - return array; - } else if (tag.type === "byte") { - if (key && BOOLEAN_TYPES.includes(key)) return tag.value !== 0; - - return tag.value; - } else if (tag.type === "int") { - return tag.value; - } else if (tag.type === "string") { - return tag.value; - } else if (tag.type === "float") { - return tag.value - } else if (tag.type === "intArray") { - const array = []; - - for (const value of tag.value) { - array.push(value); - } - - return array; - } else if (tag.type === "short") { - return tag.value; - } else if (tag.type === "long") { - return tag.value - } else if (tag.type === "double") { - return tag.value - } else { - console.log("Failed to parse NBT component type " + tag.type); - - return { text: "" }; - } - } -} diff --git a/src/util/entityUUID.ts b/src/util/entityUUID.ts new file mode 100644 index 0000000..c9d4279 --- /dev/null +++ b/src/util/entityUUID.ts @@ -0,0 +1,4 @@ +export default function entityUUID (uuid: any) { + // taken from https://gist.github.com/storycraft/7813dd0186e85daa393e1df9cfa19f2a + return uuid.replace(/-/g, '').match(/.{8}/g).map((str: any) => Number.parseInt(str, 16)).map((num: any) => Number.parseInt(num & 0x80000000 ? num - 0xffffffff - 1 : num)) +} \ No newline at end of file diff --git a/src/util/loadModules.ts b/src/util/loadModules.ts index 5a4e0da..eebc9ed 100644 --- a/src/util/loadModules.ts +++ b/src/util/loadModules.ts @@ -1,22 +1,20 @@ -import { readdirSync } from "fs"; -import { join } from "path"; +import fs from "fs"; +import path from "path"; +import url from "url"; -class loadModules { - constructor (bot, config) { - for (const filename of readdirSync(join(__dirname, '../modules'))) { - try { - if (filename.endsWith(".ts")) { - //import * as module from filename; - - const module = import(filename); - console.log(module) - //module({ bot, config }) - } - } catch (error) { +export default async function loadModules (bot: any, config: any) { + for (const filename of fs.readdirSync(path.join(__dirname, '../modules'))) { + try { + if (filename.endsWith(".ts") || filename.endsWith(".js")) { + const module = await import(path.join(__dirname, '../modules/', filename)) + const modules = module.default; + new modules({ bot, config }) + } + } catch (error: unknown) { + if (error instanceof Error) { console.error(`Failed to load module ${filename} due to error`); console.error(`\x1b[31m${error.stack}\x1b[0m`); } - } - } -} -export = loadModules; \ No newline at end of file + } + } +} \ No newline at end of file diff --git a/src/util/parseToml.ts b/src/util/parseToml.ts new file mode 100644 index 0000000..2c14e3a --- /dev/null +++ b/src/util/parseToml.ts @@ -0,0 +1,18 @@ +import { readFileSync } from 'fs'; +import { join } from 'path'; +import toml from 'smol-toml'; +export function parseToml (path: any) { + try { + const file = readFileSync(join(path), 'utf8'); + const parse = toml.parse(file) + /** + * stringify if the parsed toml is not stringified and then parsed again with JSON.stringify and JSON.parse it will error with + * Type 'TomlValue' must have a '[Symbol.iterator]()' method that returns an iterator. when we try to use it in a for const of statement + */ + const stringify = JSON.stringify(parse); + const parsed = JSON.parse(stringify) + return parsed + } catch (error: unknown) { + console.error(`\x1b[31mFailed to parse toml due to\x1b[0m\n${error}`) + } +} diff --git a/tsconfig.json b/tsconfig.json index ae32dd7..9beb273 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,13 +10,13 @@ "declaration": false, "declarationMap": false, "noUncheckedIndexedAccess": true, - "strict": false, + "strict": true, // "jsx": "react-jsx", "verbatimModuleSyntax": false, "isolatedModules": false, "noUncheckedSideEffectImports": true, "moduleDetection": "force", - "skipLibCheck": false, + "skipLibCheck": true, "esModuleInterop": true, } }