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 (#59)
Browse files Browse the repository at this point in the history
* Auto-create and reuse bot-owned webhooks

* Update README.md WRT webhooks

* Add webstorm ignore to .gitignore

* Add WebStorm configuration
  • Loading branch information
aronson authored Dec 11, 2023
1 parent a3cf98e commit e4ca652
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ discord-irc
logs
*.log

# WebStorm
**/.idea/workspace.xml
**/.idea/tasks.xml
**/.idea/discord.xml

# Runtime data
pids
*.pid
Expand Down
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/deno.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/discord-irc.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 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,14 @@ 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": true
```

The bot will automatically create and re-use its own webhooks.

## 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?: boolean;
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.boolean().optional(),
ignoreUsers: IgnoreUsersSchema.optional(),
gameLogConfig: GameLogConfigSchema.optional(),
ignoreConfig: IgnoreConfigSchema.optional(),
Expand Down

0 comments on commit e4ca652

Please sign in to comment.