From f1dd72cce079ee16f51414cbb1eaa81758fa6ffa Mon Sep 17 00:00:00 2001 From: Isaac Aronson Date: Sun, 10 Dec 2023 03:55:13 -0600 Subject: [PATCH 1/6] Auto-create and reuse bot-owned webhooks --- README.md | 18 ++++++++---------- lib/channelMapping.ts | 17 ++++++++--------- lib/config.ts | 4 ++-- lib/discordClient.ts | 1 + 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index e5784aa..90763b5 100644 --- a/README.md +++ b/README.md @@ -220,10 +220,8 @@ First you need to create a Discord bot user, which you can do by following the i "discord": ["discord_nick1", "discord_nick2"], // Ignore specified Discord nicks and do not send their messages to IRC. "discordIds": ["198528216523210752"] // Ignore specified Discord ids and do not send their messages to IRC. }, - // List of webhooks per channel - "webhooks": { - "#discord": "https://discord.com/api/webhooks/id/token" - }, + // Use webhooks + "webhooks": true, // Commands that will be sent on connect // Note: these are typically optional and only provided as a reference "autoSendCommands": [ @@ -247,16 +245,16 @@ Webhooks lets you override nicknames and avatars, so messages coming from IRC ca ![discord-webhook](http://i.imgur.com/lNeJIUI.jpg) -To enable webhooks, follow part 1 of -[this guide](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks) to create and retrieve a webhook -URL for a specific channel, then enable it in discord-irc's config as follows: +To enable webhooks, enable them in discord-irc's config as follows: ```json -"webhooks": { - "#discord-channel": "https://discord.com/api/webhooks/id/token" -} +"webhooks": {} ``` +The bot will automatically create and re-use its own webhooks. + +To disable webhooks, remove the `webhook` property from the config. + ## Tests (TODO) Run the tests with: diff --git a/lib/channelMapping.ts b/lib/channelMapping.ts index d6cf00d..9847110 100644 --- a/lib/channelMapping.ts +++ b/lib/channelMapping.ts @@ -40,16 +40,15 @@ export class ChannelMapper { } // Check for webhook let webhookURL: string | null = null; - if (config.webhooks) { - if (config.webhooks['#' + discordChannel.name]) { - webhookURL = config.webhooks['#' + discordChannel.name]; - } else if (config.webhooks[discordChannel.id]) { - webhookURL = config.webhooks[discordChannel.id]; - } - } let client: Webhook | null = null; - if (webhookURL) { - client = await Webhook.fromURL(webhookURL, discord); + if (config.webhooks) { + const hookName = `${bot.config.nickname}_${discordChannel.name}`; + const hooks = await discordChannel.fetchWebhooks(); + const hook = hooks.find((h) => h.name === hookName); + client = hook ?? await Webhook.create(discordChannel, discord, { + name: hookName, + }); + webhookURL = client.url; } const mapping: ChannelMapping = { discordChannel: discordChannel, diff --git a/lib/config.ts b/lib/config.ts index 749c401..edad700 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -49,7 +49,7 @@ export type Config = { parallelPingFix?: boolean; ircStatusNotices?: boolean; announceSelfJoin?: boolean; - webhooks?: Dictionary; + webhooks?: unknown; ignoreUsers?: IgnoreUsers; gameLogConfig?: GameLogConfig; ignoreConfig?: IgnoreConfig; @@ -116,7 +116,7 @@ export const ConfigSchema = z.object({ parallelPingFix: z.boolean().optional(), ircStatusNotices: z.boolean().optional(), announceSelfJoin: z.boolean().optional(), - webhooks: z.record(z.string()).optional(), + webhooks: z.unknown().optional(), ignoreUsers: IgnoreUsersSchema.optional(), gameLogConfig: GameLogConfigSchema.optional(), ignoreConfig: IgnoreConfigSchema.optional(), diff --git a/lib/discordClient.ts b/lib/discordClient.ts index fbbc0f4..9926bac 100644 --- a/lib/discordClient.ts +++ b/lib/discordClient.ts @@ -25,6 +25,7 @@ export class DiscordClient extends Client { GatewayIntents.GUILD_MEMBERS, GatewayIntents.GUILD_MESSAGES, GatewayIntents.MESSAGE_CONTENT, + GatewayIntents.GUILD_WEBHOOKS, ], token: discordToken, }); From 3c5047f2e2ac29ea4ff3354be877fdd5a5a1d7ff Mon Sep 17 00:00:00 2001 From: aronson Date: Sun, 10 Dec 2023 04:10:04 -0600 Subject: [PATCH 2/6] Update README.md WRT webhooks --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 90763b5..d8294ef 100644 --- a/README.md +++ b/README.md @@ -248,12 +248,12 @@ Webhooks lets you override nicknames and avatars, so messages coming from IRC ca To enable webhooks, enable them in discord-irc's config as follows: ```json -"webhooks": {} +"webhooks": true ``` The bot will automatically create and re-use its own webhooks. -To disable webhooks, remove the `webhook` property from the config. +To disable webhooks, remove the `webhooks` property from the config. ## Tests (TODO) From 88156acdda3143d421033e3fc780807f97ffdc3e Mon Sep 17 00:00:00 2001 From: Isaac Aronson Date: Sun, 10 Dec 2023 04:13:24 -0600 Subject: [PATCH 3/6] Don't use GUILD_WEBHOOKS intent as unused --- lib/discordClient.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/discordClient.ts b/lib/discordClient.ts index 9926bac..fbbc0f4 100644 --- a/lib/discordClient.ts +++ b/lib/discordClient.ts @@ -25,7 +25,6 @@ export class DiscordClient extends Client { GatewayIntents.GUILD_MEMBERS, GatewayIntents.GUILD_MESSAGES, GatewayIntents.MESSAGE_CONTENT, - GatewayIntents.GUILD_WEBHOOKS, ], token: discordToken, }); From aeac4ef5395b8f2f312555e1f8b47cbb39b3990d Mon Sep 17 00:00:00 2001 From: Isaac Aronson Date: Sun, 10 Dec 2023 21:42:09 -0600 Subject: [PATCH 4/6] Switch webhooks to boolean --- README.md | 2 -- lib/config.ts | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d8294ef..0d4802c 100644 --- a/README.md +++ b/README.md @@ -253,8 +253,6 @@ To enable webhooks, enable them in discord-irc's config as follows: The bot will automatically create and re-use its own webhooks. -To disable webhooks, remove the `webhooks` property from the config. - ## Tests (TODO) Run the tests with: diff --git a/lib/config.ts b/lib/config.ts index edad700..b09e8e9 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -49,7 +49,7 @@ export type Config = { parallelPingFix?: boolean; ircStatusNotices?: boolean; announceSelfJoin?: boolean; - webhooks?: unknown; + webhooks?: boolean; ignoreUsers?: IgnoreUsers; gameLogConfig?: GameLogConfig; ignoreConfig?: IgnoreConfig; @@ -116,7 +116,7 @@ export const ConfigSchema = z.object({ parallelPingFix: z.boolean().optional(), ircStatusNotices: z.boolean().optional(), announceSelfJoin: z.boolean().optional(), - webhooks: z.unknown().optional(), + webhooks: z.boolean().optional(), ignoreUsers: IgnoreUsersSchema.optional(), gameLogConfig: GameLogConfigSchema.optional(), ignoreConfig: IgnoreConfigSchema.optional(), From a168df2fe7b96233287e2526c21a8c0b332894eb Mon Sep 17 00:00:00 2001 From: Isaac Aronson Date: Sun, 10 Dec 2023 21:43:09 -0600 Subject: [PATCH 5/6] Add webstorm ignore to .gitignore --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index ff85bd2..4a90b2f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,11 @@ discord-irc logs *.log +# WebStorm +**/.idea/workspace.xml +**/.idea/tasks.xml +**/.idea/discord.xml + # Runtime data pids *.pid From 13e8c2865804a74bc31abc5ffc1aa43ca6e511f5 Mon Sep 17 00:00:00 2001 From: Isaac Aronson Date: Sun, 10 Dec 2023 21:43:35 -0600 Subject: [PATCH 6/6] Add WebStorm configuration --- .idea/.gitignore | 5 +++++ .idea/deno.xml | 6 ++++++ .idea/discord-irc.iml | 12 ++++++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ 5 files changed, 37 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/deno.xml create mode 100644 .idea/discord-irc.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..b58b603 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/deno.xml b/.idea/deno.xml new file mode 100644 index 0000000..b03feb5 --- /dev/null +++ b/.idea/deno.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/discord-irc.iml b/.idea/discord-irc.iml new file mode 100644 index 0000000..24643cc --- /dev/null +++ b/.idea/discord-irc.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..520fe72 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file