summaryrefslogtreecommitdiffstats
path: root/bridge/discord/discord.go
diff options
context:
space:
mode:
authorQais Patankar <qaisjp@gmail.com>2020-12-03 21:36:08 +0000
committerGitHub <noreply@github.com>2020-12-03 22:36:08 +0100
commitc42167c6f46888281fb823d7263771d261365a1c (patch)
tree3c596e60e6554393f34e30ffdde79b9b7e70e52e /bridge/discord/discord.go
parent44d182e2f93b5652a3e7595030a2f61f1453fdc5 (diff)
downloadmatterbridge-msglm-c42167c6f46888281fb823d7263771d261365a1c.tar.gz
matterbridge-msglm-c42167c6f46888281fb823d7263771d261365a1c.tar.bz2
matterbridge-msglm-c42167c6f46888281fb823d7263771d261365a1c.zip
Refactor guild finding code (discord) (#1319)
Diffstat (limited to 'bridge/discord/discord.go')
-rw-r--r--bridge/discord/discord.go53
1 files changed, 35 insertions, 18 deletions
diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go
index e7d98c10..2a44775f 100644
--- a/bridge/discord/discord.go
+++ b/bridge/discord/discord.go
@@ -2,7 +2,6 @@ package bdiscord
import (
"bytes"
- "errors"
"fmt"
"strings"
"sync"
@@ -50,7 +49,6 @@ func New(cfg *bridge.Config) bridge.Bridger {
func (b *Bdiscord) Connect() error {
var err error
- var guildFound bool
token := b.GetString("Token")
b.Log.Info("Connecting")
if b.GetString("WebhookURL") == "" {
@@ -94,29 +92,48 @@ func (b *Bdiscord) Connect() error {
serverName := strings.Replace(b.GetString("Server"), "ID:", "", -1)
b.nick = userinfo.Username
b.userID = userinfo.ID
+
+ // Try and find this account's guild, and populate channels
b.channelsMutex.Lock()
for _, guild := range guilds {
- if guild.Name == serverName || guild.ID == serverName {
- b.channels, err = b.c.GuildChannels(guild.ID)
- if err != nil {
- break
- }
- b.guildID = guild.ID
- guildFound = true
+ // Skip, if the server name does not match the visible name or the ID
+ if guild.Name != serverName && guild.ID != serverName {
+ continue
+ }
+
+ // Complain about an ambiguous Server setting. Two Discord servers could have the same title!
+ // For IDs, practically this will never happen. It would only trigger if some server's name is also an ID.
+ if b.guildID != "" {
+ return fmt.Errorf("found multiple Discord servers with the same name %#v, expected to see only one", serverName)
+ }
+
+ // Getting this guild's channel could result in a permission error
+ b.channels, err = b.c.GuildChannels(guild.ID)
+ if err != nil {
+ return fmt.Errorf("could not get %#v's channels: %w", b.GetString("Server"), err)
}
+
+ b.guildID = guild.ID
}
b.channelsMutex.Unlock()
- if !guildFound {
- msg := fmt.Sprintf("Server \"%s\" not found", b.GetString("Server"))
- err = errors.New(msg)
- b.Log.Error(msg)
- b.Log.Info("Possible values:")
+
+ // If we couldn't find a guild, we print extra debug information and return a nice error
+ if b.guildID == "" {
+ err = fmt.Errorf("could not find Discord server %#v", b.GetString("Server"))
+ b.Log.Error(err.Error())
+
+ // Print all of the possible server values
+ b.Log.Info("Possible server values:")
for _, guild := range guilds {
- b.Log.Infof("Server=\"%s\" # Server name", guild.Name)
- b.Log.Infof("Server=\"%s\" # Server ID", guild.ID)
+ b.Log.Infof("\t- Server=%#v # by name", guild.Name)
+ b.Log.Infof("\t- Server=%#v # by ID", guild.ID)
}
- }
- if err != nil {
+
+ // If there are no results, we should say that
+ if len(guilds) == 0 {
+ b.Log.Info("\t- (none found)")
+ }
+
return err
}