From 9b051fadef375a90f2b9e7548a322d865844b5eb Mon Sep 17 00:00:00 2001 From: jejebecarte <69335633+jejebecarte@users.noreply.github.com> Date: Tue, 3 Dec 2024 15:09:49 +1100 Subject: [PATCH] refactor: rewrite instructions for hypixel api keys --- .env.template | 6 +-- README.md | 47 ++++++++++++-------- src/events/mineflayer/chat/whisper.ts | 2 +- src/requests/fetch-error-embed.ts | 2 +- src/requests/fetch-hypixel-guild.ts | 3 ++ src/requests/fetch-hypixel-player-profile.ts | 3 ++ src/util/env.ts | 2 +- 7 files changed, 41 insertions(+), 24 deletions(-) diff --git a/.env.template b/.env.template index a859cf4d..6f7d7d79 100644 --- a/.env.template +++ b/.env.template @@ -3,8 +3,8 @@ MINECRAFT_PASSWORD="" MINECRAFT_CHAT_SEPARATOR=">" # The character that separates the author name from their message in game -HYPIXEL_API_KEY="" -# Do `/api new` on Hypixel for this +HYPIXEL_API_KEY= +# Some features will not work if left unset USE_PROFANITY_FILTER=true # Profanity filter is designed to only block words disallowed by Hypixel, but may have false positives @@ -30,4 +30,4 @@ MEMBER_CHANNEL_ID= OFFICER_CHANNEL_ID= BLACKLIST_CHANNEL_ID= BOT_OWNER_ID= -STAFF_ROLE_ID= +STAFF_ROLE_ID= \ No newline at end of file diff --git a/README.md b/README.md index 7cc3a441..5b7bf7fc 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,10 @@ - [Installation](#installation) - [Prerequisites](#prerequisites) - [Setup](#setup) - - [Process Management](#process-management) + - [Hypixel API keys](#hypixel-api-keys) + - [Process management](#process-management) - [Contributing](#contributing) - - [Issues and Bug Reports](#issues-and-bug-reports) + - [Issues and bug reports](#issues-and-bug-reports) - [Acknowledgements](#acknowledgements) - [License](#license) @@ -52,50 +53,57 @@ ### Setup -1. ⭐ Star this repository! -2. Install [pnpm](https://pnpm.io/) via one of their [installation methods](https://pnpm.io/installation), or the following command: +1. Install [pnpm](https://pnpm.io/) via one of their [installation methods](https://pnpm.io/installation), or the following command: ```bash $ npm install -g pnpm ``` -3. Clone the repository into a directory of your choice. +2. Clone the repository into a directory of your choice. ```bash $ git clone https://github.com/MiscGuild/bridge.git ``` -4. Enter the new directory and install packages. +3. Enter the new directory and install packages. ```bash $ cd bridge $ pnpm install ``` -5. Make a new Discord bot account on the [Discord Developer Portal](https://discord.com/developers/applications) and retrieve the bot token. +4. Make a new Discord bot account on the [Discord Developer Portal](https://discord.com/developers/applications) and retrieve the bot token. -6. Under the `Bot` tab, grant the bot the `Message Content` Intent. +5. Under the `Bot` tab, grant the bot the `Message Content` Intent. -7. Generate an invite URL by going to the OAuth2 tab and clicking on the URL Generator. Add the `bot` and `application.commands` scopes, before opening the link to invite the bot to your server. +6. Generate an invite URL by going to the OAuth2 tab and clicking on the URL Generator. Add the `bot` and `application.commands` scopes, before opening the link to invite the bot to your server. -8. Setup the project config files. +7. Setup the project config files. ```bash $ pnpm run setup-files ``` -9. Fill out the `.env` file with your credentials. +8. Fill out the `.env` file with your credentials. -10. Promote the Minecraft account used by the bot to Officer in-game in order for it to view the Officer chat and run privileged commands. +9. Promote the Minecraft account used by the bot to Officer in-game in order for it to view the Officer chat and run privileged commands. -11. Build and run the bot. +10. Build and run the bot. ```bash $ pnpm run build $ pnpm start ``` -### Process Management +### Hypixel API keys + +After the June 2023 [Hypixel Public API update](https://hypixel.net/threads/hypixel-developer-dashboard-public-api-changes-june-2023.5364455/), Development API Keys now expire after three days but can be easily renewed. + +To acquire a permanent key, first generate a Development Key [here](https://developer.hypixel.net/dashboard). Then, fill out the form under `Apps > Create app > Personal API Key`. Please note that applications concerning bridge bots may be denied. + +Alternatively, you may omit the `HYPIXEL_API_KEY` field of the `.env` file, but some features such as minimum network level enforcement will be disabled. + +### Process management If you are self-hosting or your process manager does not automatically restart the bot in the event of a crash, you may use [PM2](https://pm2.keymetrics.io/), a process manager for Node.js applications. @@ -110,14 +118,17 @@ Please refer to the [PM2 documentation](https://pm2.keymetrics.io/docs/usage/qui ## Contributing -Pull requests are welcome. If you would like to chat with other developers please join our [Discord](https://discord.gg/bHFWukp) and go to `#programming-talk`. +Pull requests are welcome. If you would like to chat with other developers please join our [Discord](https://discord.gg/bHFWukp). Before submitting your changes for review, please ensure: -- The application is fully functional, and all your code has been thoroughly tested to avoid potential bugs or security vulnerabilities. -- Your commit messages adhere to the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/). +- the application is fully functional +- your code has been thoroughly tested +- your commit messages adhere to the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/). + +Give this repository a ⭐ if it helped you! -### Issues and Bug Reports +### Issues and bug reports To raise an issue or bug report, please contact a developer or [open an issue](https://github.com/MiscGuild/bridge/issues). diff --git a/src/events/mineflayer/chat/whisper.ts b/src/events/mineflayer/chat/whisper.ts index 307032d1..c9ca92d7 100644 --- a/src/events/mineflayer/chat/whisper.ts +++ b/src/events/mineflayer/chat/whisper.ts @@ -7,7 +7,7 @@ export default { runOnce: false, run: async (bot, playerName: string, message: string) => { const errorMessage = `/w ${playerName} There was an error attempting your request! (Check spelling and/or try again later)`; - const target = message.startsWith('weeklygexp' || 'weeklygxp') + const target = message.startsWith('weeklygexp') ? playerName : (message.split(' ')[0] as string); diff --git a/src/requests/fetch-error-embed.ts b/src/requests/fetch-error-embed.ts index 846c1c75..1565d697 100644 --- a/src/requests/fetch-error-embed.ts +++ b/src/requests/fetch-error-embed.ts @@ -5,5 +5,5 @@ export default (requestError: FetchError) => .setColor('Red') .setTitle('Error') .setDescription( - `There was an error while attempting your request, a detailed log is below.\n\`\`\`Error: ${requestError.status}\nReason: ${requestError.statusText}\`\`\`` + `There was an error while attempting your request.\n\`\`\`Error: ${requestError.status}\nReason: ${requestError.statusText}\`\`\`` ); diff --git a/src/requests/fetch-hypixel-guild.ts b/src/requests/fetch-hypixel-guild.ts index 4f4e61c1..eecb802e 100644 --- a/src/requests/fetch-hypixel-guild.ts +++ b/src/requests/fetch-hypixel-guild.ts @@ -1,6 +1,9 @@ import env from '@util/env'; export default async (uuid: string) => { + if (!env.HYPIXEL_API_KEY) + return { status: 400, statusText: 'Missing Hypixel API key' } as FetchError; + const response = await fetch( `https://api.hypixel.net/guild?key=${env.HYPIXEL_API_KEY}&player=${uuid}` ); diff --git a/src/requests/fetch-hypixel-player-profile.ts b/src/requests/fetch-hypixel-player-profile.ts index 5370c5c5..1be08f6b 100644 --- a/src/requests/fetch-hypixel-player-profile.ts +++ b/src/requests/fetch-hypixel-player-profile.ts @@ -1,6 +1,9 @@ import env from '@util/env'; export default async (username: string) => { + if (!env.HYPIXEL_API_KEY) + return { status: 400, statusText: 'Missing Hypixel API key' } as FetchError; + const response = await fetch( `https://api.hypixel.net/player?key=${env.HYPIXEL_API_KEY}&name=${username}` ); diff --git a/src/util/env.ts b/src/util/env.ts index 5989b01e..9640be70 100644 --- a/src/util/env.ts +++ b/src/util/env.ts @@ -14,7 +14,7 @@ const envSchema = z .object({ MINECRAFT_EMAIL: z.string().email(), MINECRAFT_PASSWORD: z.string(), // no minimum length allows for manual authorization - dotenv populates an empty string by default - HYPIXEL_API_KEY: z.string().min(1), + HYPIXEL_API_KEY: z.string().min(1).optional(), MINECRAFT_CHAT_SEPARATOR: z.string().trim().min(1), USE_PROFANITY_FILTER: BOOLEAN_SCHEMA, USE_FIRST_WORD_OF_AUTHOR_NAME: BOOLEAN_SCHEMA,