Skip to content

Commit 86e54db

Browse files
committed
Provide more helpful errors for invalid keys.
When it is first load, if an invalid key is present in `discord_format` we inform the user of a fatal error and exit. If it's on reload we don't apply the new format and inform the user such was not done and that there is an error with an invalid key being present in the `discord_format` setting.
1 parent d97cd0d commit 86e54db

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

bridge/irc_manager.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,7 @@ func (m *IRCManager) SendMessage(channel string, msg *DiscordMessage) {
361361

362362
// Person is appearing offline (or the bridge is running in Simple Mode)
363363
if !ok {
364-
// length := len(msg.Author.Username)
365364
for _, line := range strings.Split(content, "\n") {
366-
// m.bridge.ircListener.Privmsg(channel, fmt.Sprintf(
367-
// "<%s#%s> %s",
368-
// msg.Author.Username[:1]+"\u200B"+msg.Author.Username[1:length],
369-
// msg.Author.Discriminator,
370-
// line,
371-
// ))
372-
373365
m.bridge.ircListener.Privmsg(channel, m.formatIRCMessage(msg, line))
374366
}
375367
return

main.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"os"
67
"os/signal"
78
"path/filepath"
@@ -85,8 +86,14 @@ func main() {
8586
*insecure = viper.GetBool("insecure")
8687
}
8788
//
88-
discordFormat := viper.GetStringMapString("discord_format")
89-
discordFormat = setupDiscordFormat(discordFormat)
89+
rawDiscordFormat := viper.GetStringMapString("discord_format")
90+
var discordFormat map[string]string
91+
if df, err := setupDiscordFormat(rawDiscordFormat); err == nil {
92+
discordFormat = df
93+
} else {
94+
log.WithError(err).Fatal("discord_format setting is invalid")
95+
return
96+
}
9097
//
9198
viper.SetDefault("avatar_url", "https://ui-avatars.com/api/?name=${USERNAME}")
9299
avatarURL := viper.GetString("avatar_url")
@@ -217,6 +224,14 @@ func main() {
217224
}
218225
dib.Config.DiscordIgnores = discordIgnores
219226

227+
rawDiscordFormat := viper.GetStringMapString("discord_format")
228+
if discordFormat, err := setupDiscordFormat(rawDiscordFormat); err == nil {
229+
dib.Config.DiscordFormat = discordFormat
230+
} else {
231+
log.WithError(err).Error("discord_format setting is invalid, this setting has not been updated")
232+
}
233+
dib.Config.IRCFormat = viper.GetString("irc_format")
234+
220235
chans := viper.GetStringMapString("channel_mappings")
221236
equalChans := reflect.DeepEqual(chans, channelMappings)
222237
if !equalChans {
@@ -257,7 +272,8 @@ func setupHostmaskMatchers(hostmasks []string) []glob.Glob {
257272
return matchers
258273
}
259274

260-
func setupDiscordFormat(discordFormat map[string]string) map[string]string {
275+
func setupDiscordFormat(discordFormat map[string]string) (map[string]string, error) {
276+
var err error
261277
// lowercase to match that YAML lowercases it
262278
discordFormatDefaults := map[string]string{
263279
"join": "_${NICK} joined (${IDENT}@${HOST})_",
@@ -272,7 +288,14 @@ func setupDiscordFormat(discordFormat map[string]string) map[string]string {
272288
}
273289
}
274290

275-
return discordFormat
291+
for ev := range discordFormat {
292+
if _, ok := discordFormatDefaults[ev]; !ok {
293+
err = fmt.Errorf("Unknown format key %s", ev)
294+
break
295+
}
296+
}
297+
298+
return discordFormat, err
276299
}
277300

278301
func SetLogDebug(debug bool) {

0 commit comments

Comments
 (0)