Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Auto-create and reuse bot-owned webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
aronson committed Dec 10, 2023
1 parent a3cf98e commit f1dd72c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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:
Expand Down
17 changes: 8 additions & 9 deletions lib/channelMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type Config = {
parallelPingFix?: boolean;
ircStatusNotices?: boolean;
announceSelfJoin?: boolean;
webhooks?: Dictionary<string>;
webhooks?: unknown;
ignoreUsers?: IgnoreUsers;
gameLogConfig?: GameLogConfig;
ignoreConfig?: IgnoreConfig;
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions lib/discordClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class DiscordClient extends Client {
GatewayIntents.GUILD_MEMBERS,
GatewayIntents.GUILD_MESSAGES,
GatewayIntents.MESSAGE_CONTENT,
GatewayIntents.GUILD_WEBHOOKS,
],
token: discordToken,
});
Expand Down

0 comments on commit f1dd72c

Please sign in to comment.