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, });