From f419ac651cc8e023f56e00a29a7afd8f6b49ad30 Mon Sep 17 00:00:00 2001 From: destruc7i0n Date: Sun, 20 Jun 2021 11:31:01 -0400 Subject: use uuids for avatar requests with webhooks --- src/Discord.ts | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Discord.ts b/src/Discord.ts index 7a4f5d6..4debee9 100644 --- a/src/Discord.ts +++ b/src/Discord.ts @@ -13,6 +13,8 @@ class Discord { channel: Snowflake + uuidCache: Map + constructor (config: Config, onReady?: () => void) { this.config = config @@ -21,6 +23,8 @@ class Discord { this.client.on('message', (message: Message) => this.onMessage(message)) this.channel = config.DISCORD_CHANNEL_ID || '' + + this.uuidCache = new Map() } public async init () { @@ -195,14 +199,33 @@ class Discord { return message } - private makeDiscordWebhook (username: string, message: string) { + private async getUUIDFromUsername (username: string): Promise { + username = username.toLowerCase() + if (this.uuidCache.has(username)) return this.uuidCache.get(username)! + // otherwise fetch and store + try { + const response = await (await axios.get('https://api.mojang.com/users/profiles/minecraft/' + username)).data + const uuid = response.id + this.uuidCache.set(username, uuid) + return uuid + } catch (e) { + console.log(`[ERROR] Could not fetch uuid for ${username}, falling back to Steve for the skin`) + return null + } + } + + private async makeDiscordWebhook (username: string, message: string) { message = this.replaceDiscordMentions(message) + const defaultHead = 'https://minotar.net/helm/c06f89064c8a49119c29ea1dbd1aab82/256.png' // MHF_Steve + + const uuid = await this.getUUIDFromUsername(username) + let avatarURL if (username === this.config.SERVER_NAME + ' - Server') { // use avatar for the server - avatarURL = this.config.SERVER_IMAGE || 'https://minotar.net/helm/Steve/256.png' + avatarURL = this.config.SERVER_IMAGE || defaultHead } else { // use avatar for player - avatarURL = `https://minotar.net/helm/${username}/256.png` + avatarURL = !!uuid ? `https://minotar.net/helm/${uuid}/256.png` : defaultHead } return { @@ -222,7 +245,7 @@ class Discord { public async sendMessage (username: string, message: string) { if (this.config.USE_WEBHOOKS) { - const webhook = this.makeDiscordWebhook(username, message) + const webhook = await this.makeDiscordWebhook(username, message) try { await axios.post(this.config.WEBHOOK_URL, webhook, { headers: { 'Content-Type': 'application/json' } }) } catch (e) { -- cgit 1.4.1 f8b8068aa7c5e9f188159ee4d4e2fb&showmsg=1&follow=1'>netinet/ip.h (unfollow)
Commit message (Collapse)Author
2022-02-27doc: Note OpenSSL 3.0.0 compatibility in README 3.5.0June McEnroe
2022-02-27tls_signer: Replace ECDSA_METHOD with EC_KEY_METHODJune McEnroe
2022-02-27tls: Replace ECDSA_METHOD with EC_KEY_METHODJune McEnroe
2022-02-27Import LibreSSL 3.5.0June McEnroe
2021-11-29tls: Revert accidentally(?) reverted upstream 3.4.2June McEnroe
2021-11-29Import LibreSSL 3.4.2June McEnroe
2021-10-14Import LibreSSL 3.4.1June McEnroe
2021-09-17Import LibreSSL 3.4.0June McEnroe
2021-08-24Import LibreSSL 3.3.4June McEnroe
2021-05-25Bump version to 3.3.3p1 3.3.3p1June McEnroe
2021-05-25build: Add scripts to EXTRA_DISTJune McEnroe
2021-05-25import: Add missing scripts/wrap-compiler-for-flag-checkJune McEnroe
2021-05-08Import LibreSSL 3.3.3June McEnroe
2021-04-18build: Remove added x509_verify.3 links 3.3.2June McEnroe
2021-04-18tls: Use EC_KEY_set_ex_dataJune McEnroe
2021-04-18Import LibreSSL 3.3.2June McEnroe
2021-03-05Bump version to 3.3.1p1 3.3.1p1June McEnroe
2021-03-05build: Add OpenSSL includes to libcompatJune McEnroe
Some compat sources (getentropy_linux.c for example) require OpenSSL. Reported by Robert Scheck.
2020-12-15Import LibreSSL 3.3.1June McEnroe
2020-11-24Import LibreSSL 3.3.0June McEnroe
2020-10-22Import LibreSSL 3.2.2June McEnroe
2020-09-29Import LibreSSL 3.2.1June McEnroe
2020-09-29import: Add m4/ax_add_fortify_source.m4June McEnroe
2020-08-05build: Add README.7 to EXTRA_DIST 3.2.0June McEnroe
2020-08-03doc: Indicate that only OpenSSL 1.1.1b and newer workJune McEnroe