Skip to content

Commit

Permalink
feat: Prepend configurable mentions to every webhook message
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-roesch committed Jul 27, 2024
1 parent ff920f1 commit 4238a73
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ The basic structure of a config file is as follows

```yaml
discordWebhook: '<webhook-id>'
discordMentions:
roles: ['<role-id>']
users: ['<user-id>']
shared:
progress:
url: https://brandon-sanderson.com
Expand All @@ -43,6 +46,9 @@ connectors:
The `discordWebhook` item is mandatory and must be the ID (i.e. channel ID + token) of a Discord webhook. Simply use the
value after `https://discord.com/api/webhooks/` from the webhook URL Discord provides you with.

The `discordMentions` item can optionally be specified to have all webhook messages contain mentions for the listed roles
and users. _Note that in general no additional mentions will be parsed from messages, including `@everyone`._

The `shared` section defines configuration values that are used across all connectors using a plugin. Keys in the map
must be a plugin ID. The shared config object is simply merged into any connector-specific one. Connector configs always
take precedence over shared ones.
Expand Down
41 changes: 31 additions & 10 deletions common/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,38 @@ const avatarBaseUrl = "https://raw.githubusercontent.com/Palanaeum/sanderson-not
const maxRetries = 3

type DiscordClient struct {
webhookUrl string
info *log.Logger
error *log.Logger
webhookUrl string
mentions DiscordMentions
mentionPrefix string
info *log.Logger
error *log.Logger
}

func CreateDiscordClient(webhook string) DiscordClient {
type DiscordMentions struct {
Parse []string `json:"parse" yaml:"-"`
Roles []string `json:"roles" yaml:"roles"`
Users []string `json:"users" yaml:"users"`
}

func CreateDiscordClient(webhook string, mentions DiscordMentions) DiscordClient {
infoLog, errorLog := CreateLoggers("main")

mentionPrefix := ""
for _, role := range mentions.Roles {
mentionPrefix += fmt.Sprintf("<@&%s> ", role)
}
for _, user := range mentions.Users {
mentionPrefix += fmt.Sprintf("<@%s> ", user)
}

mentions.Parse = make([]string, 0)

return DiscordClient{
webhookUrl: fmt.Sprintf("%s/%s", webhookBaseUrl, webhook),
info: infoLog,
error: errorLog,
webhookUrl: fmt.Sprintf("%s/%s", webhookBaseUrl, webhook),
mentions: mentions,
mentionPrefix: mentionPrefix,
info: infoLog,
error: errorLog,
}
}

Expand All @@ -36,9 +56,10 @@ func (discord *DiscordClient) Send(text, name, avatar string, embed interface{})

func (discord *DiscordClient) trySend(text, name, avatar string, embed interface{}, try int) error {
body := map[string]interface{}{
"username": name,
"avatar_url": fmt.Sprintf("%s/%s.png", avatarBaseUrl, avatar),
"content": text,
"username": name,
"avatar_url": fmt.Sprintf("%s/%s.png", avatarBaseUrl, avatar),
"content": fmt.Sprintf("%s%s", discord.mentionPrefix, text),
"allowed_mentions": discord.mentions,
}

if embed != nil {
Expand Down
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"17thshard.com/sanderson-notifications/common"
. "17thshard.com/sanderson-notifications/plugins"
"fmt"
"github.com/mitchellh/mapstructure"
Expand All @@ -14,6 +15,7 @@ type ConfigLoader struct {

type Config struct {
DiscordWebhook string `yaml:"discordWebhook"`
DiscordMentions common.DiscordMentions `yaml:"discordMentions"`
Connectors []Connector `yaml:"-"`
SharedPluginConfigs map[string]map[string]interface{} `yaml:"shared"`
RawConnectors map[string]RawConnector `yaml:"connectors"`
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {

infoLog.Println("Checking for updates...")

client := CreateDiscordClient(config.DiscordWebhook)
client := CreateDiscordClient(config.DiscordWebhook, config.DiscordMentions)
ctx := context.Background()

var wg sync.WaitGroup
Expand Down

0 comments on commit 4238a73

Please sign in to comment.