2016-09-19 19:05:13 +00:00
|
|
|
package bdiscord
|
|
|
|
|
|
|
|
import (
|
2017-11-02 23:05:10 +00:00
|
|
|
"bytes"
|
2019-01-07 20:39:53 +00:00
|
|
|
"errors"
|
2018-02-23 23:05:43 +00:00
|
|
|
"fmt"
|
2018-06-09 10:47:40 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2016-09-19 19:05:13 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2018-02-03 00:11:11 +00:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2020-03-08 16:08:18 +00:00
|
|
|
"github.com/matterbridge/discordgo"
|
2016-09-19 19:05:13 +00:00
|
|
|
)
|
|
|
|
|
2018-07-21 22:27:49 +00:00
|
|
|
const MessageLength = 1950
|
|
|
|
|
2018-02-24 22:22:15 +00:00
|
|
|
type Bdiscord struct {
|
2018-03-04 22:52:14 +00:00
|
|
|
*bridge.Config
|
2018-12-07 22:36:01 +00:00
|
|
|
|
|
|
|
c *discordgo.Session
|
|
|
|
|
2019-01-09 20:28:47 +00:00
|
|
|
nick string
|
2020-03-22 17:39:11 +00:00
|
|
|
userID string
|
2019-01-09 20:28:47 +00:00
|
|
|
guildID string
|
|
|
|
webhookID string
|
|
|
|
webhookToken string
|
|
|
|
canEditWebhooks bool
|
2018-12-07 22:36:01 +00:00
|
|
|
|
|
|
|
channelsMutex sync.RWMutex
|
|
|
|
channels []*discordgo.Channel
|
|
|
|
channelInfoMap map[string]*config.ChannelInfo
|
|
|
|
|
|
|
|
membersMutex sync.RWMutex
|
|
|
|
userMemberMap map[string]*discordgo.Member
|
|
|
|
nickMemberMap map[string]*discordgo.Member
|
2016-09-19 19:05:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
|
|
|
b := &Bdiscord{Config: cfg}
|
2017-02-13 17:52:52 +00:00
|
|
|
b.userMemberMap = make(map[string]*discordgo.Member)
|
2018-09-12 20:30:14 +00:00
|
|
|
b.nickMemberMap = make(map[string]*discordgo.Member)
|
2017-08-12 12:51:41 +00:00
|
|
|
b.channelInfoMap = make(map[string]*config.ChannelInfo)
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookURL") != "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debug("Configuring Discord Incoming Webhook")
|
2018-03-04 22:52:14 +00:00
|
|
|
b.webhookID, b.webhookToken = b.splitURL(b.GetString("WebhookURL"))
|
2017-06-26 18:07:27 +00:00
|
|
|
}
|
2016-09-19 19:05:13 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:22:15 +00:00
|
|
|
func (b *Bdiscord) Connect() error {
|
2016-09-19 19:05:13 +00:00
|
|
|
var err error
|
2019-01-07 20:39:53 +00:00
|
|
|
var guildFound bool
|
2019-01-19 19:39:58 +00:00
|
|
|
token := b.GetString("Token")
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting")
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookURL") == "" {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using token")
|
2017-06-24 17:36:10 +00:00
|
|
|
} else {
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connecting using webhookurl (for posting) and token")
|
2017-06-24 17:36:10 +00:00
|
|
|
}
|
2018-03-04 22:52:14 +00:00
|
|
|
if !strings.HasPrefix(b.GetString("Token"), "Bot ") {
|
|
|
|
token = "Bot " + b.GetString("Token")
|
2016-11-14 15:30:43 +00:00
|
|
|
}
|
2019-01-14 18:27:49 +00:00
|
|
|
// if we have a User token, remove the `Bot` prefix
|
2019-01-14 18:41:32 +00:00
|
|
|
if strings.HasPrefix(b.GetString("Token"), "User ") {
|
2019-01-19 19:39:58 +00:00
|
|
|
token = strings.Replace(b.GetString("Token"), "User ", "", -1)
|
2019-01-14 18:27:49 +00:00
|
|
|
}
|
2019-01-14 18:41:32 +00:00
|
|
|
|
2018-03-04 22:52:14 +00:00
|
|
|
b.c, err = discordgo.New(token)
|
2016-09-19 19:05:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Info("Connection succeeded")
|
2016-09-19 19:05:13 +00:00
|
|
|
b.c.AddHandler(b.messageCreate)
|
2019-10-03 22:18:56 +00:00
|
|
|
b.c.AddHandler(b.messageTyping)
|
2017-02-13 17:52:52 +00:00
|
|
|
b.c.AddHandler(b.memberUpdate)
|
2017-04-15 17:00:15 +00:00
|
|
|
b.c.AddHandler(b.messageUpdate)
|
2017-09-11 20:45:15 +00:00
|
|
|
b.c.AddHandler(b.messageDelete)
|
2019-06-15 23:15:18 +00:00
|
|
|
b.c.AddHandler(b.messageDeleteBulk)
|
2019-02-17 20:49:45 +00:00
|
|
|
b.c.AddHandler(b.memberAdd)
|
|
|
|
b.c.AddHandler(b.memberRemove)
|
2016-09-19 19:05:13 +00:00
|
|
|
err = b.c.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-16 12:39:00 +00:00
|
|
|
guilds, err := b.c.UserGuilds(100, "", "")
|
2016-09-19 19:05:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userinfo, err := b.c.User("@me")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-28 22:50:40 +00:00
|
|
|
serverName := strings.Replace(b.GetString("Server"), "ID:", "", -1)
|
2018-12-07 22:36:01 +00:00
|
|
|
b.nick = userinfo.Username
|
2020-03-22 17:39:11 +00:00
|
|
|
b.userID = userinfo.ID
|
2018-12-07 22:36:01 +00:00
|
|
|
b.channelsMutex.Lock()
|
2016-09-19 19:05:13 +00:00
|
|
|
for _, guild := range guilds {
|
2018-11-28 22:50:40 +00:00
|
|
|
if guild.Name == serverName || guild.ID == serverName {
|
2019-07-15 19:56:35 +00:00
|
|
|
b.channels, err = b.c.GuildChannels(guild.ID)
|
2016-09-19 19:05:13 +00:00
|
|
|
if err != nil {
|
2018-12-07 22:36:01 +00:00
|
|
|
break
|
2016-09-19 19:05:13 +00:00
|
|
|
}
|
2019-07-14 17:53:09 +00:00
|
|
|
b.guildID = guild.ID
|
|
|
|
guildFound = true
|
2016-09-19 19:05:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 22:36:01 +00:00
|
|
|
b.channelsMutex.Unlock()
|
2019-01-07 20:39:53 +00:00
|
|
|
if !guildFound {
|
|
|
|
msg := fmt.Sprintf("Server \"%s\" not found", b.GetString("Server"))
|
|
|
|
err = errors.New(msg)
|
|
|
|
b.Log.Error(msg)
|
|
|
|
b.Log.Info("Possible values:")
|
|
|
|
for _, guild := range guilds {
|
|
|
|
b.Log.Infof("Server=\"%s\" # Server name", guild.Name)
|
|
|
|
b.Log.Infof("Server=\"%s\" # Server ID", guild.ID)
|
|
|
|
}
|
|
|
|
}
|
2018-12-07 22:36:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-17 16:04:38 +00:00
|
|
|
|
2018-12-07 22:36:01 +00:00
|
|
|
b.channelsMutex.RLock()
|
2019-01-09 20:28:47 +00:00
|
|
|
if b.GetString("WebhookURL") == "" {
|
|
|
|
for _, channel := range b.channels {
|
|
|
|
b.Log.Debugf("found channel %#v", channel)
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-17 16:18:50 +00:00
|
|
|
manageWebhooks := discordgo.PermissionManageWebhooks
|
|
|
|
var channelsDenied []string
|
2020-02-08 14:13:23 +00:00
|
|
|
for _, info := range b.Channels {
|
|
|
|
id := b.getChannelID(info.Name) // note(qaisjp): this readlocks channelsMutex
|
|
|
|
b.Log.Debugf("Verifying PermissionManageWebhooks for %s with ID %s", info.ID, id)
|
2020-03-17 16:04:38 +00:00
|
|
|
|
2020-03-17 16:18:50 +00:00
|
|
|
perms, permsErr := b.c.UserChannelPermissions(userinfo.ID, id)
|
2020-03-17 16:04:38 +00:00
|
|
|
if permsErr != nil {
|
2020-03-17 16:18:50 +00:00
|
|
|
b.Log.Warnf("Failed to check PermissionManageWebhooks in channel \"%s\": %s", info.Name, permsErr.Error())
|
|
|
|
} else if perms&manageWebhooks == manageWebhooks {
|
|
|
|
continue
|
2019-01-09 20:28:47 +00:00
|
|
|
}
|
2020-03-17 16:18:50 +00:00
|
|
|
channelsDenied = append(channelsDenied, fmt.Sprintf("%#v", info.Name))
|
2019-01-09 20:28:47 +00:00
|
|
|
}
|
2020-03-17 16:18:50 +00:00
|
|
|
|
|
|
|
b.canEditWebhooks = len(channelsDenied) == 0
|
2019-01-09 20:28:47 +00:00
|
|
|
if b.canEditWebhooks {
|
|
|
|
b.Log.Info("Can manage webhooks; will edit channel for global webhook on send")
|
|
|
|
} else {
|
|
|
|
b.Log.Warn("Can't manage webhooks; won't edit channel for global webhook on send")
|
2020-03-17 16:18:50 +00:00
|
|
|
b.Log.Warn("Can't manage webhooks in channels: ", strings.Join(channelsDenied, ", "))
|
2019-01-09 20:28:47 +00:00
|
|
|
}
|
2018-03-17 21:56:58 +00:00
|
|
|
}
|
2018-12-07 22:36:01 +00:00
|
|
|
b.channelsMutex.RUnlock()
|
|
|
|
|
|
|
|
// Obtaining guild members and initializing nickname mapping.
|
|
|
|
b.membersMutex.Lock()
|
|
|
|
defer b.membersMutex.Unlock()
|
2018-09-17 19:25:06 +00:00
|
|
|
members, err := b.c.GuildMembers(b.guildID, "", 1000)
|
|
|
|
if err != nil {
|
2019-01-07 20:39:53 +00:00
|
|
|
b.Log.Error("Error obtaining server members: ", err)
|
2018-09-17 19:25:06 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, member := range members {
|
2018-12-07 22:36:01 +00:00
|
|
|
if member == nil {
|
|
|
|
b.Log.Warnf("Skipping missing information for a user.")
|
|
|
|
continue
|
|
|
|
}
|
2018-09-17 19:25:06 +00:00
|
|
|
b.userMemberMap[member.User.ID] = member
|
|
|
|
b.nickMemberMap[member.User.Username] = member
|
|
|
|
if member.Nick != "" {
|
|
|
|
b.nickMemberMap[member.Nick] = member
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 19:05:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:22:15 +00:00
|
|
|
func (b *Bdiscord) Disconnect() error {
|
2018-03-04 22:52:14 +00:00
|
|
|
return b.c.Close()
|
2017-02-14 20:12:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 22:22:15 +00:00
|
|
|
func (b *Bdiscord) JoinChannel(channel config.ChannelInfo) error {
|
2018-12-07 22:36:01 +00:00
|
|
|
b.channelsMutex.Lock()
|
|
|
|
defer b.channelsMutex.Unlock()
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
b.channelInfoMap[channel.ID] = &channel
|
2016-09-19 19:05:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:22:15 +00:00
|
|
|
func (b *Bdiscord) Send(msg config.Message) (string, error) {
|
2018-02-28 21:23:29 +00:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2018-02-23 23:05:43 +00:00
|
|
|
|
2016-09-19 19:05:13 +00:00
|
|
|
channelID := b.getChannelID(msg.Channel)
|
|
|
|
if channelID == "" {
|
2018-02-23 23:05:43 +00:00
|
|
|
return "", fmt.Errorf("Could not find channelID for %v", msg.Channel)
|
2016-09-19 19:05:13 +00:00
|
|
|
}
|
2018-02-23 23:05:43 +00:00
|
|
|
|
2019-10-03 22:18:56 +00:00
|
|
|
if msg.Event == config.EventUserTyping {
|
|
|
|
if b.GetBool("ShowUserTyping") {
|
|
|
|
err := b.c.ChannelTyping(channelID)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:05:43 +00:00
|
|
|
// Make a action /me of the message
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventUserAction {
|
2017-07-31 19:37:19 +00:00
|
|
|
msg.Text = "_" + msg.Text + "_"
|
|
|
|
}
|
2017-08-12 12:51:41 +00:00
|
|
|
|
2019-01-09 20:28:47 +00:00
|
|
|
// use initial webhook configured for the entire Discord account
|
|
|
|
isGlobalWebhook := true
|
2017-08-12 12:51:41 +00:00
|
|
|
wID := b.webhookID
|
|
|
|
wToken := b.webhookToken
|
2018-02-23 23:05:43 +00:00
|
|
|
|
|
|
|
// check if have a channel specific webhook
|
2018-12-07 22:36:01 +00:00
|
|
|
b.channelsMutex.RLock()
|
2017-08-12 14:30:00 +00:00
|
|
|
if ci, ok := b.channelInfoMap[msg.Channel+b.Account]; ok {
|
2017-08-12 12:51:41 +00:00
|
|
|
if ci.Options.WebhookURL != "" {
|
|
|
|
wID, wToken = b.splitURL(ci.Options.WebhookURL)
|
2019-01-09 20:28:47 +00:00
|
|
|
isGlobalWebhook = false
|
2017-08-12 12:51:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 22:36:01 +00:00
|
|
|
b.channelsMutex.RUnlock()
|
2017-08-12 12:51:41 +00:00
|
|
|
|
2018-02-23 23:05:43 +00:00
|
|
|
// Use webhook to send the message
|
2019-07-08 20:18:37 +00:00
|
|
|
if wID != "" && msg.Event != config.EventMsgDelete {
|
2018-02-24 21:38:27 +00:00
|
|
|
// skip events
|
2020-05-10 22:20:35 +00:00
|
|
|
if msg.Event != "" && msg.Event != config.EventUserAction && msg.Event != config.EventJoinLeave && msg.Event != config.EventTopicChange {
|
2018-02-24 21:38:27 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2019-07-08 20:18:37 +00:00
|
|
|
|
2018-07-21 21:19:11 +00:00
|
|
|
// skip empty messages
|
2019-08-28 22:13:10 +00:00
|
|
|
if msg.Text == "" && (msg.Extra == nil || len(msg.Extra["file"]) == 0) {
|
|
|
|
b.Log.Debugf("Skipping empty message %#v", msg)
|
2018-07-21 21:19:11 +00:00
|
|
|
return "", nil
|
|
|
|
}
|
2018-07-21 22:27:49 +00:00
|
|
|
|
|
|
|
msg.Text = helper.ClipMessage(msg.Text, MessageLength)
|
2018-09-22 20:15:19 +00:00
|
|
|
msg.Text = b.replaceUserMentions(msg.Text)
|
2018-11-23 19:52:31 +00:00
|
|
|
// discord username must be [0..32] max
|
|
|
|
if len(msg.Username) > 32 {
|
|
|
|
msg.Username = msg.Username[0:32]
|
|
|
|
}
|
2020-11-14 04:08:09 +00:00
|
|
|
|
|
|
|
if msg.ID != "" {
|
|
|
|
b.Log.Debugf("Editing webhook message")
|
|
|
|
uri := discordgo.EndpointWebhookToken(wID, wToken) + "/messages/" + msg.ID
|
|
|
|
_, err := b.c.RequestWithBucketID("PATCH", uri, discordgo.WebhookParams{
|
|
|
|
Content: msg.Text,
|
|
|
|
Username: msg.Username,
|
|
|
|
}, discordgo.EndpointWebhookToken("", ""))
|
|
|
|
if err == nil {
|
|
|
|
return msg.ID, nil
|
|
|
|
}
|
|
|
|
b.Log.Errorf("Could not edit webhook message: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.Log.Debugf("Broadcasting using Webhook")
|
|
|
|
|
2019-01-09 20:28:47 +00:00
|
|
|
// if we have a global webhook for this Discord account, and permission
|
|
|
|
// to modify webhooks (previously verified), then set its channel to
|
2020-11-14 04:08:09 +00:00
|
|
|
// the message channel before using it.
|
2019-01-09 20:28:47 +00:00
|
|
|
if isGlobalWebhook && b.canEditWebhooks {
|
|
|
|
b.Log.Debugf("Setting webhook channel to \"%s\"", msg.Channel)
|
|
|
|
_, err := b.c.WebhookEdit(wID, "", "", channelID)
|
|
|
|
if err != nil {
|
2019-02-26 16:21:23 +00:00
|
|
|
b.Log.Errorf("Could not set webhook channel: %s", err)
|
2019-01-09 20:28:47 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2019-08-28 22:13:10 +00:00
|
|
|
b.Log.Debugf("Processing webhook sending for message %#v", msg)
|
|
|
|
msg, err := b.webhookSend(&msg, wID, wToken)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("Could not broadcast via webook for message %#v: %s", msg, err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if msg == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2020-04-21 21:35:46 +00:00
|
|
|
|
2019-08-28 22:13:10 +00:00
|
|
|
return msg.ID, nil
|
2018-02-23 23:05:43 +00:00
|
|
|
}
|
2017-11-02 23:05:10 +00:00
|
|
|
|
2018-02-26 23:33:21 +00:00
|
|
|
b.Log.Debugf("Broadcasting using token (API)")
|
2018-02-23 23:05:43 +00:00
|
|
|
|
|
|
|
// Delete message
|
2018-11-15 19:43:43 +00:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2018-02-23 23:05:43 +00:00
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
2017-11-02 23:05:10 +00:00
|
|
|
}
|
2018-02-23 23:05:43 +00:00
|
|
|
err := b.c.ChannelMessageDelete(channelID, msg.ID)
|
|
|
|
return "", err
|
|
|
|
}
|
2017-11-04 13:47:14 +00:00
|
|
|
|
2018-02-23 23:05:43 +00:00
|
|
|
// Upload a file if it exists
|
|
|
|
if msg.Extra != nil {
|
|
|
|
for _, rmsg := range helper.HandleExtra(&msg, b.General) {
|
2018-07-21 22:27:49 +00:00
|
|
|
rmsg.Text = helper.ClipMessage(rmsg.Text, MessageLength)
|
2018-12-07 22:36:01 +00:00
|
|
|
if _, err := b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text); err != nil {
|
2019-02-26 16:21:23 +00:00
|
|
|
b.Log.Errorf("Could not send message %#v: %s", rmsg, err)
|
2018-12-07 22:36:01 +00:00
|
|
|
}
|
2018-02-23 23:05:43 +00:00
|
|
|
}
|
|
|
|
// check if we have files to upload (from slack, telegram or mattermost)
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
|
|
|
return b.handleUploadFile(&msg, channelID)
|
2017-08-27 22:33:17 +00:00
|
|
|
}
|
2018-02-23 23:05:43 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 22:27:49 +00:00
|
|
|
msg.Text = helper.ClipMessage(msg.Text, MessageLength)
|
2018-09-12 20:30:14 +00:00
|
|
|
msg.Text = b.replaceUserMentions(msg.Text)
|
|
|
|
|
2018-02-23 23:05:43 +00:00
|
|
|
// 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
|
|
|
|
}
|
2019-08-28 22:13:10 +00:00
|
|
|
return res.ID, nil
|
2016-09-19 19:05:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
// useWebhook returns true if we have a webhook defined somewhere
|
2018-02-24 22:22:15 +00:00
|
|
|
func (b *Bdiscord) useWebhook() bool {
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookURL") != "" {
|
2017-08-12 12:51:41 +00:00
|
|
|
return true
|
|
|
|
}
|
2018-12-07 22:36:01 +00:00
|
|
|
|
|
|
|
b.channelsMutex.RLock()
|
|
|
|
defer b.channelsMutex.RUnlock()
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
for _, channel := range b.channelInfoMap {
|
|
|
|
if channel.Options.WebhookURL != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// isWebhookID returns true if the specified id is used in a defined webhook
|
2018-02-24 22:22:15 +00:00
|
|
|
func (b *Bdiscord) isWebhookID(id string) bool {
|
2018-03-04 22:52:14 +00:00
|
|
|
if b.GetString("WebhookURL") != "" {
|
|
|
|
wID, _ := b.splitURL(b.GetString("WebhookURL"))
|
2017-08-12 14:30:00 +00:00
|
|
|
if wID == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-12-07 22:36:01 +00:00
|
|
|
|
|
|
|
b.channelsMutex.RLock()
|
|
|
|
defer b.channelsMutex.RUnlock()
|
|
|
|
|
2017-08-12 12:51:41 +00:00
|
|
|
for _, channel := range b.channelInfoMap {
|
|
|
|
if channel.Options.WebhookURL != "" {
|
|
|
|
wID, _ := b.splitURL(channel.Options.WebhookURL)
|
|
|
|
if wID == id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-02-23 23:05:43 +00:00
|
|
|
|
|
|
|
// handleUploadFile handles native upload of files
|
2018-02-24 22:22:15 +00:00
|
|
|
func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (string, error) {
|
2018-02-23 23:05:43 +00:00
|
|
|
var err error
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
2018-11-07 19:36:50 +00:00
|
|
|
file := discordgo.File{
|
|
|
|
Name: fi.Name,
|
|
|
|
ContentType: "",
|
|
|
|
Reader: bytes.NewReader(*fi.Data),
|
|
|
|
}
|
|
|
|
m := discordgo.MessageSend{
|
|
|
|
Content: msg.Username + fi.Comment,
|
|
|
|
Files: []*discordgo.File{&file},
|
|
|
|
}
|
|
|
|
_, err = b.c.ChannelMessageSendComplex(channelID, &m)
|
2018-02-23 23:05:43 +00:00
|
|
|
if err != nil {
|
2019-02-26 16:21:23 +00:00
|
|
|
return "", fmt.Errorf("file upload failed: %s", err)
|
2018-02-23 23:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
2019-08-28 22:13:10 +00:00
|
|
|
|
|
|
|
// webhookSend send one or more message via webhook, taking care of file
|
|
|
|
// uploads (from slack, telegram or mattermost).
|
|
|
|
// Returns messageID and error.
|
|
|
|
func (b *Bdiscord) webhookSend(msg *config.Message, webhookID, token string) (*discordgo.Message, error) {
|
|
|
|
var (
|
|
|
|
res *discordgo.Message
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2020-02-09 21:07:26 +00:00
|
|
|
// If avatar is unset, check if UseLocalAvatar contains the message's
|
|
|
|
// account or protocol, and if so, try to find a local avatar
|
|
|
|
if msg.Avatar == "" {
|
|
|
|
for _, val := range b.GetStringSlice("UseLocalAvatar") {
|
|
|
|
if msg.Protocol == val || msg.Account == val {
|
|
|
|
if avatar := b.findAvatar(msg); avatar != "" {
|
|
|
|
msg.Avatar = avatar
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 22:13:10 +00:00
|
|
|
// WebhookParams can have either `Content` or `File`.
|
|
|
|
|
|
|
|
// We can't send empty messages.
|
|
|
|
if msg.Text != "" {
|
|
|
|
res, err = b.c.WebhookExecute(
|
|
|
|
webhookID,
|
|
|
|
token,
|
|
|
|
true,
|
|
|
|
&discordgo.WebhookParams{
|
|
|
|
Content: msg.Text,
|
|
|
|
Username: msg.Username,
|
|
|
|
AvatarURL: msg.Avatar,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("Could not send text (%s) for message %#v: %s", msg.Text, msg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Extra != nil {
|
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
|
|
|
file := discordgo.File{
|
|
|
|
Name: fi.Name,
|
|
|
|
ContentType: "",
|
|
|
|
Reader: bytes.NewReader(*fi.Data),
|
|
|
|
}
|
2020-02-02 20:14:54 +00:00
|
|
|
content := ""
|
|
|
|
if msg.Text == "" {
|
|
|
|
content = fi.Comment
|
|
|
|
}
|
2019-08-28 22:13:10 +00:00
|
|
|
_, e2 := b.c.WebhookExecute(
|
|
|
|
webhookID,
|
|
|
|
token,
|
|
|
|
false,
|
|
|
|
&discordgo.WebhookParams{
|
|
|
|
Username: msg.Username,
|
|
|
|
AvatarURL: msg.Avatar,
|
|
|
|
File: &file,
|
2020-02-02 20:14:54 +00:00
|
|
|
Content: content,
|
2019-08-28 22:13:10 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if e2 != nil {
|
|
|
|
b.Log.Errorf("Could not send file %#v for message %#v: %s", file, msg, e2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
2020-02-09 21:07:26 +00:00
|
|
|
|
|
|
|
func (b *Bdiscord) findAvatar(m *config.Message) string {
|
|
|
|
member, err := b.getGuildMemberByNick(m.Username)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return member.User.AvatarURL("")
|
|
|
|
}
|