From e288b74fabf5a6d69ebdf32182fadd35b2ace1b3 Mon Sep 17 00:00:00 2001 From: destruc7i0n Date: Wed, 29 Apr 2020 10:00:30 -0400 Subject: add the ability to handle webhook messages --- src/Discord.ts | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src/Discord.ts') diff --git a/src/Discord.ts b/src/Discord.ts index bc15fac..d8263e8 100644 --- a/src/Discord.ts +++ b/src/Discord.ts @@ -48,13 +48,46 @@ class Discord { } } + private parseDiscordWebhook (url: string) { + const re = /discordapp.com\/api\/webhooks\/([^\/]+)\/([^\/]+)/ + + // the is of the webhook + let id = null + let token = null + + if (!re.test(url)) { + // In case the url changes at some point, I will warn if it still works + console.log('[WARN] The Webhook URL may not be valid!') + } else { + const match = url.match(re) + if (match) { + id = match[1] + token = match[2] + } + } + + return { id, token } + } + private async onMessage (message: Message) { // no channel, done if (!this.channel) return // don't want to check other channels if (message.channel.id !== this.channel || message.channel.type !== 'text') return // if using webhooks, ignore this! - if (this.config.USE_WEBHOOKS && message.webhookID) return + if (message.webhookID) { + // if ignoring all webhooks, ignore + if (this.config.IGNORE_WEBHOOKS) { + return + } else if (this.config.USE_WEBHOOKS) { + // otherwise, ignore all webhooks that are not the same as this one + const { id } = this.parseDiscordWebhook(this.config.WEBHOOK_URL) + if (id === message.webhookID) { + if (this.config.DEBUG) console.log('[INFO] Ignoring webhook from self') + return + } + } + } // if the same user as the bot, ignore if (message.author.id === this.client.user.id) return // ignore any attachments @@ -81,6 +114,8 @@ class Discord { command = `/tellraw @a ${this.makeMinecraftTellraw(message)}` } + if (this.config.DEBUG) console.log(`[DEBUG] Sending command "${command}" to the server`) + if (command) { await rcon.command(command).catch((e) => { console.log('[ERROR] Could not send command!') -- cgit 1.4.1