5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-22 20:40:27 +00:00

Refactor discord

This commit is contained in:
Wim 2018-02-24 00:05:43 +01:00
parent cac5d56d60
commit 3d8f15c20b

View File

@ -2,6 +2,7 @@ package bdiscord
import ( import (
"bytes" "bytes"
"fmt"
"github.com/42wim/matterbridge/bridge/config" "github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper" "github.com/42wim/matterbridge/bridge/helper"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -56,7 +57,6 @@ func (b *bdiscord) Connect() error {
} }
b.c, err = discordgo.New(b.Config.Token) b.c, err = discordgo.New(b.Config.Token)
if err != nil { if err != nil {
flog.Debugf("%#v", err)
return err return err
} }
flog.Info("Connection succeeded") flog.Info("Connection succeeded")
@ -66,17 +66,14 @@ func (b *bdiscord) Connect() error {
b.c.AddHandler(b.messageDelete) b.c.AddHandler(b.messageDelete)
err = b.c.Open() err = b.c.Open()
if err != nil { if err != nil {
flog.Debugf("%#v", err)
return err return err
} }
guilds, err := b.c.UserGuilds(100, "", "") guilds, err := b.c.UserGuilds(100, "", "")
if err != nil { if err != nil {
flog.Debugf("%#v", err)
return err return err
} }
userinfo, err := b.c.User("@me") userinfo, err := b.c.User("@me")
if err != nil { if err != nil {
flog.Debugf("%#v", err)
return err return err
} }
b.Nick = userinfo.Username b.Nick = userinfo.Username
@ -85,7 +82,6 @@ func (b *bdiscord) Connect() error {
b.Channels, err = b.c.GuildChannels(guild.ID) b.Channels, err = b.c.GuildChannels(guild.ID)
b.guildID = guild.ID b.guildID = guild.ID
if err != nil { if err != nil {
flog.Debugf("%#v", err)
return err return err
} }
} }
@ -108,63 +104,30 @@ func (b *bdiscord) JoinChannel(channel config.ChannelInfo) error {
func (b *bdiscord) Send(msg config.Message) (string, error) { func (b *bdiscord) Send(msg config.Message) (string, error) {
flog.Debugf("Receiving %#v", msg) flog.Debugf("Receiving %#v", msg)
channelID := b.getChannelID(msg.Channel) channelID := b.getChannelID(msg.Channel)
if channelID == "" { if channelID == "" {
flog.Errorf("Could not find channelID for %v", msg.Channel) return "", fmt.Errorf("Could not find channelID for %v", msg.Channel)
return "", nil
} }
// Make a action /me of the message
if msg.Event == config.EVENT_USER_ACTION { if msg.Event == config.EVENT_USER_ACTION {
msg.Text = "_" + msg.Text + "_" msg.Text = "_" + msg.Text + "_"
} }
// use initial webhook
wID := b.webhookID wID := b.webhookID
wToken := b.webhookToken wToken := b.webhookToken
// check if have a channel specific webhook
if ci, ok := b.channelInfoMap[msg.Channel+b.Account]; ok { if ci, ok := b.channelInfoMap[msg.Channel+b.Account]; ok {
if ci.Options.WebhookURL != "" { if ci.Options.WebhookURL != "" {
wID, wToken = b.splitURL(ci.Options.WebhookURL) wID, wToken = b.splitURL(ci.Options.WebhookURL)
} }
} }
if wID == "" { // Use webhook to send the message
flog.Debugf("Broadcasting using token (API)") if wID != "" {
if msg.Event == config.EVENT_MSG_DELETE {
if msg.ID == "" {
return "", nil
}
err := b.c.ChannelMessageDelete(channelID, msg.ID)
return "", err
}
if msg.ID != "" {
_, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text)
return msg.ID, err
}
if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text)
}
// check if we have files to upload (from slack, telegram or mattermost)
if len(msg.Extra["file"]) > 0 {
var err error
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo)
files := []*discordgo.File{}
files = append(files, &discordgo.File{fi.Name, "", bytes.NewReader(*fi.Data)})
_, err = b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{Content: msg.Username + fi.Comment, Files: files})
if err != nil {
flog.Errorf("file upload failed: %#v", err)
}
}
return "", nil
}
}
res, err := b.c.ChannelMessageSend(channelID, msg.Username+msg.Text)
if err != nil {
return "", err
}
return res.ID, err
}
flog.Debugf("Broadcasting using Webhook") flog.Debugf("Broadcasting using Webhook")
err := b.c.WebhookExecute( err := b.c.WebhookExecute(
wID, wID,
@ -178,6 +141,42 @@ func (b *bdiscord) Send(msg config.Message) (string, error) {
return "", err return "", err
} }
flog.Debugf("Broadcasting using token (API)")
// Delete message
if msg.Event == config.EVENT_MSG_DELETE {
if msg.ID == "" {
return "", nil
}
err := b.c.ChannelMessageDelete(channelID, msg.ID)
return "", err
}
// Upload a file if it exists
if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text)
}
// check if we have files to upload (from slack, telegram or mattermost)
if len(msg.Extra["file"]) > 0 {
return b.handleUploadFile(&msg, channelID)
}
}
// Edit message
if msg.ID != "" {
_, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text)
return msg.ID, err
}
// Post normal message
res, err := b.c.ChannelMessageSend(channelID, msg.Username+msg.Text)
if err != nil {
return "", err
}
return res.ID, err
}
func (b *bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelete) { func (b *bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelete) {
rmsg := config.Message{Account: b.Account, ID: m.ID, Event: config.EVENT_MSG_DELETE, Text: config.EVENT_MSG_DELETE} rmsg := config.Message{Account: b.Account, ID: m.ID, Event: config.EVENT_MSG_DELETE, Text: config.EVENT_MSG_DELETE}
rmsg.Channel = b.getChannelName(m.ChannelID) rmsg.Channel = b.getChannelName(m.ChannelID)
@ -203,6 +202,7 @@ func (b *bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdat
func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
var err error var err error
// not relay our own messages // not relay our own messages
if m.Author.Username == b.Nick { if m.Author.Username == b.Nick {
return return
@ -212,55 +212,58 @@ func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
return return
} }
// add the url of the attachments to content
if len(m.Attachments) > 0 { if len(m.Attachments) > 0 {
for _, attach := range m.Attachments { for _, attach := range m.Attachments {
m.Content = m.Content + "\n" + attach.URL m.Content = m.Content + "\n" + attach.URL
} }
} }
var text string 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 != "" { if m.Content != "" {
flog.Debugf("Receiving message %#v", m.Message) flog.Debugf("Receiving message %#v", m.Message)
m.Message.Content = b.stripCustomoji(m.Message.Content) m.Message.Content = b.stripCustomoji(m.Message.Content)
m.Message.Content = b.replaceChannelMentions(m.Message.Content) m.Message.Content = b.replaceChannelMentions(m.Message.Content)
text, err = m.ContentWithMoreMentionsReplaced(b.c) rmsg.Text, err = m.ContentWithMoreMentionsReplaced(b.c)
if err != nil { if err != nil {
flog.Errorf("ContentWithMoreMentionsReplaced failed: %s", err) flog.Errorf("ContentWithMoreMentionsReplaced failed: %s", err)
text = m.ContentWithMentionsReplaced() rmsg.Text = m.ContentWithMentionsReplaced()
} }
} }
rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", // set channel name
UserID: m.Author.ID, ID: m.ID}
rmsg.Channel = b.getChannelName(m.ChannelID) rmsg.Channel = b.getChannelName(m.ChannelID)
if b.UseChannelID { if b.UseChannelID {
rmsg.Channel = "ID:" + m.ChannelID rmsg.Channel = "ID:" + m.ChannelID
} }
// set username
if !b.Config.UseUserName { if !b.Config.UseUserName {
rmsg.Username = b.getNick(m.Author) rmsg.Username = b.getNick(m.Author)
} else { } else {
rmsg.Username = m.Author.Username rmsg.Username = m.Author.Username
} }
// if we have embedded content add it to text
if b.Config.ShowEmbeds && m.Message.Embeds != nil { if b.Config.ShowEmbeds && m.Message.Embeds != nil {
for _, embed := range m.Message.Embeds { for _, embed := range m.Message.Embeds {
text = text + "embed: " + embed.Title + " - " + embed.Description + " - " + embed.URL + "\n" rmsg.Text = rmsg.Text + "embed: " + embed.Title + " - " + embed.Description + " - " + embed.URL + "\n"
} }
} }
// no empty messages // no empty messages
if text == "" { if rmsg.Text == "" {
return return
} }
text, ok := b.replaceAction(text) // do we have a /me action
var ok bool
rmsg.Text, ok = b.replaceAction(rmsg.Text)
if ok { if ok {
rmsg.Event = config.EVENT_USER_ACTION rmsg.Event = config.EVENT_USER_ACTION
} }
rmsg.Text = text
flog.Debugf("Sending message from %s on %s to gateway", m.Author.Username, b.Account) flog.Debugf("Sending message from %s on %s to gateway", m.Author.Username, b.Account)
flog.Debugf("Message is %#v", rmsg) flog.Debugf("Message is %#v", rmsg)
b.Remote <- rmsg b.Remote <- rmsg
@ -396,3 +399,18 @@ func (b *bdiscord) isWebhookID(id string) bool {
} }
return false return false
} }
// handleUploadFile handles native upload of files
func (b *bdiscord) handleUploadFile(msg *config.Message, channelID string) (string, error) {
var err error
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo)
files := []*discordgo.File{}
files = append(files, &discordgo.File{fi.Name, "", bytes.NewReader(*fi.Data)})
_, err = b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{Content: msg.Username + fi.Comment, Files: files})
if err != nil {
return "", fmt.Errorf("file upload failed: %#v", err)
}
}
return "", nil
}