diff options
author | Duco van Amstel <duco.vanamstel@gmail.com> | 2018-12-07 22:36:01 +0000 |
---|---|---|
committer | Wim <wim@42.be> | 2018-12-07 23:36:01 +0100 |
commit | 0365c0786a6bf95b7d598d25402d9a1d28ab8036 (patch) | |
tree | 5a66e85ed37000d2b0a991ef4a324d227d909b6b /bridge/discord/handlers.go | |
parent | af7a00d030b89a0fa1c85da8f3a1485c8a757fa1 (diff) | |
download | matterbridge-msglm-0365c0786a6bf95b7d598d25402d9a1d28ab8036.tar.gz matterbridge-msglm-0365c0786a6bf95b7d598d25402d9a1d28ab8036.tar.bz2 matterbridge-msglm-0365c0786a6bf95b7d598d25402d9a1d28ab8036.zip |
Split Discord bridge in multiple files (#632)
Diffstat (limited to 'bridge/discord/handlers.go')
-rw-r--r-- | bridge/discord/handlers.go | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/bridge/discord/handlers.go b/bridge/discord/handlers.go new file mode 100644 index 00000000..79c32015 --- /dev/null +++ b/bridge/discord/handlers.go @@ -0,0 +1,125 @@ +package bdiscord + +import ( + "github.com/42wim/matterbridge/bridge/config" + "github.com/bwmarrin/discordgo" +) + +func (b *Bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelete) { + rmsg := config.Message{Account: b.Account, ID: m.ID, Event: config.EventMsgDelete, Text: config.EventMsgDelete} + rmsg.Channel = b.getChannelName(m.ChannelID) + if b.useChannelID { + rmsg.Channel = "ID:" + m.ChannelID + } + b.Log.Debugf("<= Sending message from %s to gateway", b.Account) + b.Log.Debugf("<= Message is %#v", rmsg) + b.Remote <- rmsg +} + +func (b *Bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) { + if b.GetBool("EditDisable") { + return + } + // only when message is actually edited + if m.Message.EditedTimestamp != "" { + b.Log.Debugf("Sending edit message") + m.Content += b.GetString("EditSuffix") + b.messageCreate(s, (*discordgo.MessageCreate)(m)) + } +} + +func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + var err error + + // not relay our own messages + if m.Author.Username == b.nick { + return + } + // if using webhooks, do not relay if it's ours + if b.useWebhook() && m.Author.Bot && b.isWebhookID(m.Author.ID) { + return + } + + // add the url of the attachments to content + if len(m.Attachments) > 0 { + for _, attach := range m.Attachments { + m.Content = m.Content + "\n" + attach.URL + } + } + + rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", UserID: m.Author.ID, ID: m.ID} + + if m.Content != "" { + b.Log.Debugf("== Receiving event %#v", m.Message) + m.Message.Content = b.stripCustomoji(m.Message.Content) + m.Message.Content = b.replaceChannelMentions(m.Message.Content) + rmsg.Text, err = m.ContentWithMoreMentionsReplaced(b.c) + if err != nil { + b.Log.Errorf("ContentWithMoreMentionsReplaced failed: %s", err) + rmsg.Text = m.ContentWithMentionsReplaced() + } + } + + // set channel name + rmsg.Channel = b.getChannelName(m.ChannelID) + if b.useChannelID { + rmsg.Channel = "ID:" + m.ChannelID + } + + // set username + if !b.GetBool("UseUserName") { + rmsg.Username = b.getNick(m.Author) + } else { + rmsg.Username = m.Author.Username + } + + // if we have embedded content add it to text + if b.GetBool("ShowEmbeds") && m.Message.Embeds != nil { + for _, embed := range m.Message.Embeds { + rmsg.Text = rmsg.Text + "embed: " + embed.Title + " - " + embed.Description + " - " + embed.URL + "\n" + } + } + + // no empty messages + if rmsg.Text == "" { + return + } + + // do we have a /me action + var ok bool + rmsg.Text, ok = b.replaceAction(rmsg.Text) + if ok { + rmsg.Event = config.EventUserAction + } + + b.Log.Debugf("<= Sending message from %s on %s to gateway", m.Author.Username, b.Account) + b.Log.Debugf("<= Message is %#v", rmsg) + b.Remote <- rmsg +} + +func (b *Bdiscord) memberUpdate(s *discordgo.Session, m *discordgo.GuildMemberUpdate) { + if m.Member == nil { + b.Log.Warnf("Received member update with no member information: %#v", m) + } + + b.membersMutex.Lock() + defer b.membersMutex.Unlock() + + if currMember, ok := b.userMemberMap[m.Member.User.ID]; ok { + b.Log.Debugf( + "%s: memberupdate: user %s (nick %s) changes nick to %s", + b.Account, + m.Member.User.Username, + b.userMemberMap[m.Member.User.ID].Nick, + m.Member.Nick, + ) + delete(b.nickMemberMap, currMember.User.Username) + delete(b.nickMemberMap, currMember.Nick) + delete(b.userMemberMap, m.Member.User.ID) + } + b.userMemberMap[m.Member.User.ID] = m.Member + b.nickMemberMap[m.Member.User.Username] = m.Member + if m.Member.Nick != "" { + b.nickMemberMap[m.Member.Nick] = m.Member + } +} |