diff options
author | Wim <wim@42.be> | 2019-07-15 21:56:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-15 21:56:35 +0200 |
commit | 7d2e440c8311199dddb3f1e7c73c223325e51dff (patch) | |
tree | f418c899664720eff53bf555fc38ce01dcdfe630 /bridge | |
parent | 5551f9d56f2270c58738d3d43b3f4ef82507a3b9 (diff) | |
download | matterbridge-msglm-7d2e440c8311199dddb3f1e7c73c223325e51dff.tar.gz matterbridge-msglm-7d2e440c8311199dddb3f1e7c73c223325e51dff.tar.bz2 matterbridge-msglm-7d2e440c8311199dddb3f1e7c73c223325e51dff.zip |
Add support for discord category channels (discord) (#863)
This adds support for the discord category option that can be used
to group channels in. This means we can have multiple channels with
the same name.
We add the option to specify a category in the channel option of a
discord account under [[gateway]]
Besides channel="channel" or channel="ID:channelID", now also
channel="category/channel" can be specified.
This change remains backwards compatible with people that haven't
specified the category and incorporates the fix in #861
Diffstat (limited to 'bridge')
-rw-r--r-- | bridge/discord/discord.go | 4 | ||||
-rw-r--r-- | bridge/discord/helpers.go | 72 |
2 files changed, 53 insertions, 23 deletions
diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 66c8134c..1ae655bf 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -95,12 +95,10 @@ func (b *Bdiscord) Connect() error { b.channelsMutex.Lock() for _, guild := range guilds { if guild.Name == serverName || guild.ID == serverName { - var chans []*discordgo.Channel - chans, err = b.c.GuildChannels(guild.ID) + b.channels, err = b.c.GuildChannels(guild.ID) if err != nil { break } - b.channels = filterChannelsByType(chans, discordgo.ChannelTypeGuildText, false) b.guildID = guild.ID guildFound = true } diff --git a/bridge/discord/helpers.go b/bridge/discord/helpers.go index 5bf6931d..cf12c952 100644 --- a/bridge/discord/helpers.go +++ b/bridge/discord/helpers.go @@ -52,6 +52,9 @@ func (b *Bdiscord) getGuildMemberByNick(nick string) (*discordgo.Member, error) } func (b *Bdiscord) getChannelID(name string) string { + if strings.Contains(name, "/") { + return b.getCategoryChannelID(name) + } b.channelsMutex.RLock() defer b.channelsMutex.RUnlock() @@ -60,25 +63,70 @@ func (b *Bdiscord) getChannelID(name string) string { return idcheck[1] } for _, channel := range b.channels { - if channel.Name == name { + if channel.Name == name && channel.Type == discordgo.ChannelTypeGuildText { return channel.ID } } return "" } +func (b *Bdiscord) getCategoryChannelID(name string) string { + b.channelsMutex.RLock() + defer b.channelsMutex.RUnlock() + res := strings.Split(name, "/") + // shouldn't happen because function should be only called from getChannelID + if len(res) != 2 { + return "" + } + catName, chanName := res[0], res[1] + for _, channel := range b.channels { + // if we have a parentID, lookup the name of that parent (category) + // and if it matches return it + if channel.Name == chanName && channel.ParentID != "" { + for _, cat := range b.channels { + if cat.ID == channel.ParentID && cat.Name == catName { + return channel.ID + } + } + } + } + return "" +} + func (b *Bdiscord) getChannelName(id string) string { b.channelsMutex.RLock() defer b.channelsMutex.RUnlock() for _, channel := range b.channels { if channel.ID == id { - return channel.Name + return b.getCategoryChannelName(channel.Name, channel.ParentID) } } return "" } +func (b *Bdiscord) getCategoryChannelName(name, parentID string) string { + var usesCat bool + // do we have a category configuration in the channel config + for _, c := range b.channelInfoMap { + if strings.Contains(c.Name, "/") { + usesCat = true + break + } + } + // configuration without category, return the normal channel name + if !usesCat { + return name + } + // create a category/channel response + for _, c := range b.channels { + if c.ID == parentID { + name = c.Name + "/" + name + } + } + return name +} + var ( // See https://discordapp.com/developers/docs/reference#message-formatting. channelMentionRE = regexp.MustCompile("<#[0-9]+>") @@ -93,8 +141,8 @@ func (b *Bdiscord) replaceChannelMentions(text string) string { // If we don't have the channel refresh our list. if channelName == "" { - chans, err := b.c.GuildChannels(b.guildID) - b.channels = filterChannelsByType(chans, discordgo.ChannelTypeGuildCategory, true) + var err error + b.channels, err = b.c.GuildChannels(b.guildID) if err != nil { return "#unknownchannel" } @@ -211,19 +259,3 @@ func (b *Bdiscord) webhookExecute(webhookID, token string, wait bool, data *disc return st, nil } - -func filterChannelsByType(chans []*discordgo.Channel, t discordgo.ChannelType, filterOut bool) []*discordgo.Channel { - cs := []*discordgo.Channel{} - for _, c := range chans { - keep := c.Type == t - if filterOut { - keep = c.Type != t - } - - if keep { - cs = append(cs, c) - } - } - return cs - -} |