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/Discord.ts') 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