From 4238a73b3c499da186c838ff3430991164263749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20R=C3=B6sch?= Date: Sat, 27 Jul 2024 23:12:02 +0200 Subject: [PATCH] feat: Prepend configurable mentions to every webhook message --- README.md | 6 ++++++ common/discord.go | 41 +++++++++++++++++++++++++++++++---------- config.go | 2 ++ main.go | 2 +- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ff8d31b..fbd14f4 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ The basic structure of a config file is as follows ```yaml discordWebhook: '' +discordMentions: + roles: [''] + users: [''] shared: progress: url: https://brandon-sanderson.com @@ -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. diff --git a/common/discord.go b/common/discord.go index f391ac3..554f0f3 100644 --- a/common/discord.go +++ b/common/discord.go @@ -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, } } @@ -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 { diff --git a/config.go b/config.go index 86bb2fa..41cf626 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,7 @@ package main import ( + "17thshard.com/sanderson-notifications/common" . "17thshard.com/sanderson-notifications/plugins" "fmt" "github.com/mitchellh/mapstructure" @@ -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"` diff --git a/main.go b/main.go index 6348332..f9c393c 100644 --- a/main.go +++ b/main.go @@ -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