Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 756ac6b95261f4aee18e2eb951d73b32583fc478 Author: Wim <wim@42.be> Date: Mon Jul 15 21:56:35 2019 +0000 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 diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 66c8134..1ae655b 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -9512 +9510 @@ 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 5bf6931..cf12c95 100644 --- a/bridge/discord/helpers.go +++ b/bridge/discord/helpers.go @@ -526 +529 @@ 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()   @@ -6025 +6370 @@ 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]+>") @@ -938 +1418 @@ 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"   } @@ -21119 +2593 @@ 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 - -} diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 2aa203d..4a50aed 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -15536 +15537 @@ enable=true # discord - channel (without the #) # - ID:123456789 (where 123456789 is the channel ID) # (https://github.com/42wim/matterbridge/issues/57) + # - category/channel (without the #) if you're using discord categories to group your channels # telegram - chatid (a large negative number, eg -123456789) # see (https://www.linkedin.com/pulse/telegram-bots-beginners-marco-frau) # hipchat - id_channel (see https://www.hipchat.com/account/xmpp for the correct channel)