mirror of
https://github.com/cwinfo/matterbridge.git
synced 2024-11-22 19:40:25 +00:00
Refactor guild finding code (discord) (#1319)
This commit is contained in:
parent
44d182e2f9
commit
c42167c6f4
@ -2,7 +2,6 @@ package bdiscord
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -50,7 +49,6 @@ func New(cfg *bridge.Config) bridge.Bridger {
|
|||||||
|
|
||||||
func (b *Bdiscord) Connect() error {
|
func (b *Bdiscord) Connect() error {
|
||||||
var err error
|
var err error
|
||||||
var guildFound bool
|
|
||||||
token := b.GetString("Token")
|
token := b.GetString("Token")
|
||||||
b.Log.Info("Connecting")
|
b.Log.Info("Connecting")
|
||||||
if b.GetString("WebhookURL") == "" {
|
if b.GetString("WebhookURL") == "" {
|
||||||
@ -94,29 +92,48 @@ func (b *Bdiscord) Connect() error {
|
|||||||
serverName := strings.Replace(b.GetString("Server"), "ID:", "", -1)
|
serverName := strings.Replace(b.GetString("Server"), "ID:", "", -1)
|
||||||
b.nick = userinfo.Username
|
b.nick = userinfo.Username
|
||||||
b.userID = userinfo.ID
|
b.userID = userinfo.ID
|
||||||
|
|
||||||
|
// Try and find this account's guild, and populate channels
|
||||||
b.channelsMutex.Lock()
|
b.channelsMutex.Lock()
|
||||||
for _, guild := range guilds {
|
for _, guild := range guilds {
|
||||||
if guild.Name == serverName || guild.ID == serverName {
|
// 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)
|
b.channels, err = b.c.GuildChannels(guild.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
return fmt.Errorf("could not get %#v's channels: %w", b.GetString("Server"), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
b.guildID = guild.ID
|
b.guildID = guild.ID
|
||||||
guildFound = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
b.channelsMutex.Unlock()
|
b.channelsMutex.Unlock()
|
||||||
if !guildFound {
|
|
||||||
msg := fmt.Sprintf("Server \"%s\" not found", b.GetString("Server"))
|
// If we couldn't find a guild, we print extra debug information and return a nice error
|
||||||
err = errors.New(msg)
|
if b.guildID == "" {
|
||||||
b.Log.Error(msg)
|
err = fmt.Errorf("could not find Discord server %#v", b.GetString("Server"))
|
||||||
b.Log.Info("Possible values:")
|
b.Log.Error(err.Error())
|
||||||
|
|
||||||
|
// Print all of the possible server values
|
||||||
|
b.Log.Info("Possible server values:")
|
||||||
for _, guild := range guilds {
|
for _, guild := range guilds {
|
||||||
b.Log.Infof("Server=\"%s\" # Server name", guild.Name)
|
b.Log.Infof("\t- Server=%#v # by name", guild.Name)
|
||||||
b.Log.Infof("Server=\"%s\" # Server ID", guild.ID)
|
b.Log.Infof("\t- Server=%#v # by ID", guild.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there are no results, we should say that
|
||||||
|
if len(guilds) == 0 {
|
||||||
|
b.Log.Info("\t- (none found)")
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user