Skip to content

Commit 5b6ddfe

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 55f639a commit 5b6ddfe

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"
@@ -92,8 +93,14 @@ func main() {
9293
//
9394
viper.SetDefault("irc_puppet_prejoin_commands", []string{"MODE ${NICK} +D"})
9495
ircPuppetPrejoinCommands := viper.GetStringSlice("irc_puppet_prejoin_commands") // Commands for each connection to send before joining channels
95-
discordFormat := viper.GetStringMapString("discord_format")
96-
discordFormat = setupDiscordFormat(discordFormat)
96+
rawDiscordFormat := viper.GetStringMapString("discord_format")
97+
var discordFormat map[string]string
98+
if df, err := setupDiscordFormat(rawDiscordFormat); err == nil {
99+
discordFormat = df
100+
} else {
101+
log.WithError(err).Fatal("discord_format setting is invalid")
102+
return
103+
}
97104
//
98105
viper.SetDefault("avatar_url", "https://ui-avatars.com/api/?name=${USERNAME}")
99106
avatarURL := viper.GetString("avatar_url")
@@ -226,6 +233,14 @@ func main() {
226233
}
227234
dib.Config.DiscordIgnores = discordIgnores
228235

236+
rawDiscordFormat := viper.GetStringMapString("discord_format")
237+
if discordFormat, err := setupDiscordFormat(rawDiscordFormat); err == nil {
238+
dib.Config.DiscordFormat = discordFormat
239+
} else {
240+
log.WithError(err).Error("discord_format setting is invalid, this setting has not been updated")
241+
}
242+
dib.Config.IRCFormat = viper.GetString("irc_format")
243+
229244
chans := viper.GetStringMapString("channel_mappings")
230245
equalChans := reflect.DeepEqual(chans, channelMappings)
231246
if !equalChans {
@@ -266,7 +281,8 @@ func setupHostmaskMatchers(hostmasks []string) []glob.Glob {
266281
return matchers
267282
}
268283

269-
func setupDiscordFormat(discordFormat map[string]string) map[string]string {
284+
func setupDiscordFormat(discordFormat map[string]string) (map[string]string, error) {
285+
var err error
270286
// lowercase to match that YAML lowercases it
271287
discordFormatDefaults := map[string]string{
272288
"join": "_${NICK} joined (${IDENT}@${HOST})_",
@@ -281,7 +297,14 @@ func setupDiscordFormat(discordFormat map[string]string) map[string]string {
281297
}
282298
}
283299

284-
return discordFormat
300+
for ev := range discordFormat {
301+
if _, ok := discordFormatDefaults[ev]; !ok {
302+
err = fmt.Errorf("Unknown format key %s", ev)
303+
break
304+
}
305+
}
306+
307+
return discordFormat, err
285308
}
286309

287310
func SetLogDebug(debug bool) {

0 commit comments

Comments
 (0)