2016-09-19 21:05:13 +02:00
|
|
|
package bdiscord
|
|
|
|
|
|
|
|
import (
|
2017-11-03 00:05:10 +01:00
|
|
|
"bytes"
|
2018-02-24 00:05:43 +01:00
|
|
|
"fmt"
|
2018-06-09 12:47:40 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2018-02-27 00:33:21 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge"
|
2016-09-19 21:05:13 +02:00
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2020-11-30 06:47:02 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge/discord/transmitter"
|
2018-02-03 01:11:11 +01:00
|
|
|
"github.com/42wim/matterbridge/bridge/helper"
|
2022-03-12 17:06:39 +01:00
|
|
|
"github.com/bwmarrin/discordgo"
|
2022-02-05 14:45:54 +01:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2016-09-19 21:05:13 +02:00
|
|
|
)
|
|
|
|
|
2022-02-05 14:45:54 +01:00
|
|
|
const (
|
|
|
|
MessageLength = 1950
|
|
|
|
cFileUpload = "file_upload"
|
|
|
|
)
|
2018-07-22 00:27:49 +02:00
|
|
|
|
2018-02-24 23:22:15 +01:00
|
|
|
type Bdiscord struct {
|
2018-03-04 23:52:14 +01:00
|
|
|
*bridge.Config
|
2018-12-07 23:36:01 +01:00
|
|
|
|
|
|
|
c *discordgo.Session
|
|
|
|
|
2020-11-30 06:47:02 +01:00
|
|
|
nick string
|
|
|
|
userID string
|
|
|
|
guildID string
|
2018-12-07 23:36:01 +01: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
|
2020-11-30 06:47:02 +01:00
|
|
|
|
|
|
|
// Webhook specific logic
|
|
|
|
useAutoWebhooks bool
|
|
|
|
transmitter *transmitter.Transmitter
|
2022-02-05 14:45:54 +01:00
|
|
|
cache *lru.Cache
|
2016-09-19 21:05:13 +02:00
|
|
|
}
|
|
|
|
|
2018-03-04 23:52:14 +01:00
|
|
|
func New(cfg *bridge.Config) bridge.Bridger {
|
2022-02-05 14:45:54 +01:00
|
|
|
newCache, err := lru.New(5000)
|
|
|
|
if err != nil {
|
|
|
|
cfg.Log.Fatalf("Could not create LRU cache: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Bdiscord{
|
|
|
|
Config: cfg,
|
|
|
|
cache: newCache,
|
|
|
|
}
|
|
|
|
|
2017-02-13 18:52:52 +01:00
|
|
|
b.userMemberMap = make(map[string]*discordgo.Member)
|
2018-09-12 22:30:14 +02:00
|
|
|
b.nickMemberMap = make(map[string]*discordgo.Member)
|
2017-08-12 14:51:41 +02:00
|
|
|
b.channelInfoMap = make(map[string]*config.ChannelInfo)
|
2020-11-30 06:47:02 +01:00
|
|
|
|
2020-11-30 06:57:16 +01:00
|
|
|
b.useAutoWebhooks = b.GetBool("AutoWebhooks")
|
2020-11-30 06:47:02 +01:00
|
|
|
if b.useAutoWebhooks {
|
|
|
|
b.Log.Debug("Using automatic webhooks")
|
2017-06-26 20:07:27 +02:00
|
|
|
}
|
2016-09-19 21:05:13 +02:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-02-24 23:22:15 +01:00
|
|
|
func (b *Bdiscord) Connect() error {
|
2016-09-19 21:05:13 +02:00
|
|
|
var err error
|
2019-01-19 20:39:58 +01:00
|
|
|
token := b.GetString("Token")
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Info("Connecting")
|
2018-03-04 23:52:14 +01:00
|
|
|
if !strings.HasPrefix(b.GetString("Token"), "Bot ") {
|
|
|
|
token = "Bot " + b.GetString("Token")
|
2016-11-14 16:30:43 +01:00
|
|
|
}
|
2019-01-14 19:27:49 +01:00
|
|
|
// if we have a User token, remove the `Bot` prefix
|
2019-01-14 19:41:32 +01:00
|
|
|
if strings.HasPrefix(b.GetString("Token"), "User ") {
|
2019-01-19 20:39:58 +01:00
|
|
|
token = strings.Replace(b.GetString("Token"), "User ", "", -1)
|
2019-01-14 19:27:49 +01:00
|
|
|
}
|
2019-01-14 19:41:32 +01:00
|
|
|
|
2018-03-04 23:52:14 +01:00
|
|
|
b.c, err = discordgo.New(token)
|
2016-09-19 21:05:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Info("Connection succeeded")
|
2016-09-19 21:05:13 +02:00
|
|
|
b.c.AddHandler(b.messageCreate)
|
2019-10-04 00:18:56 +02:00
|
|
|
b.c.AddHandler(b.messageTyping)
|
2017-02-13 18:52:52 +01:00
|
|
|
b.c.AddHandler(b.memberUpdate)
|
2017-04-15 19:00:15 +02:00
|
|
|
b.c.AddHandler(b.messageUpdate)
|
2017-09-11 22:45:15 +02:00
|
|
|
b.c.AddHandler(b.messageDelete)
|
2019-06-16 01:15:18 +02:00
|
|
|
b.c.AddHandler(b.messageDeleteBulk)
|
2019-02-17 21:49:45 +01:00
|
|
|
b.c.AddHandler(b.memberAdd)
|
|
|
|
b.c.AddHandler(b.memberRemove)
|
2022-02-06 16:58:35 +01:00
|
|
|
if b.GetInt("debuglevel") == 1 {
|
|
|
|
b.c.AddHandler(b.messageEvent)
|
|
|
|
}
|
2021-03-20 22:46:36 +01:00
|
|
|
// Add privileged intent for guild member tracking. This is needed to track nicks
|
|
|
|
// for display names and @mention translation
|
|
|
|
b.c.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged |
|
|
|
|
discordgo.IntentsGuildMembers)
|
|
|
|
|
2016-09-19 21:05:13 +02:00
|
|
|
err = b.c.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-16 14:39:00 +02:00
|
|
|
guilds, err := b.c.UserGuilds(100, "", "")
|
2016-09-19 21:05:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userinfo, err := b.c.User("@me")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-28 23:50:40 +01:00
|
|
|
serverName := strings.Replace(b.GetString("Server"), "ID:", "", -1)
|
2018-12-07 23:36:01 +01:00
|
|
|
b.nick = userinfo.Username
|
2020-03-22 18:39:11 +01:00
|
|
|
b.userID = userinfo.ID
|
2020-12-03 22:36:08 +01:00
|
|
|
|
|
|
|
// Try and find this account's guild, and populate channels
|
2018-12-07 23:36:01 +01:00
|
|
|
b.channelsMutex.Lock()
|
2016-09-19 21:05:13 +02:00
|
|
|
for _, guild := range guilds {
|
2020-12-03 22:36:08 +01:00
|
|
|
// Skip, if the server name does not match the visible name or the ID
|
|
|
|
if guild.Name != serverName && guild.ID != serverName {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complain about an ambiguous Server setting. Two Discord servers could have the same title!
|
|
|
|
// For IDs, practically this will never happen. It would only trigger if some server's name is also an ID.
|
|
|
|
if b.guildID != "" {
|
|
|
|
return fmt.Errorf("found multiple Discord servers with the same name %#v, expected to see only one", serverName)
|
2016-09-19 21:05:13 +02:00
|
|
|
}
|
2020-12-03 22:36:08 +01:00
|
|
|
|
|
|
|
// Getting this guild's channel could result in a permission error
|
|
|
|
b.channels, err = b.c.GuildChannels(guild.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get %#v's channels: %w", b.GetString("Server"), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.guildID = guild.ID
|
2016-09-19 21:05:13 +02:00
|
|
|
}
|
2018-12-07 23:36:01 +01:00
|
|
|
b.channelsMutex.Unlock()
|
2020-12-03 22:36:08 +01:00
|
|
|
|
|
|
|
// If we couldn't find a guild, we print extra debug information and return a nice error
|
|
|
|
if b.guildID == "" {
|
|
|
|
err = fmt.Errorf("could not find Discord server %#v", b.GetString("Server"))
|
|
|
|
b.Log.Error(err.Error())
|
|
|
|
|
|
|
|
// Print all of the possible server values
|
|
|
|
b.Log.Info("Possible server values:")
|
2019-01-07 21:39:53 +01:00
|
|
|
for _, guild := range guilds {
|
2020-12-03 22:36:08 +01:00
|
|
|
b.Log.Infof("\t- Server=%#v # by name", guild.Name)
|
|
|
|
b.Log.Infof("\t- Server=%#v # by ID", guild.ID)
|
2019-01-07 21:39:53 +01:00
|
|
|
}
|
2020-12-03 22:36:08 +01:00
|
|
|
|
|
|
|
// If there are no results, we should say that
|
|
|
|
if len(guilds) == 0 {
|
|
|
|
b.Log.Info("\t- (none found)")
|
|
|
|
}
|
|
|
|
|
2018-12-07 23:36:01 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-03-17 17:04:38 +01:00
|
|
|
|
2020-11-30 06:57:16 +01:00
|
|
|
// Legacy note: WebhookURL used to have an actual webhook URL that we would edit,
|
|
|
|
// but we stopped doing that due to Discord making rate limits more aggressive.
|
|
|
|
//
|
|
|
|
// Even older: the same WebhookURL used to be used by every channel, which is usually unexpected.
|
|
|
|
// This is no longer possible.
|
|
|
|
if b.GetString("WebhookURL") != "" {
|
|
|
|
message := "The global WebhookURL setting has been removed. "
|
|
|
|
message += "You can get similar \"webhook editing\" behaviour by replacing this line with `AutoWebhooks=true`. "
|
|
|
|
message += "If you rely on the old-OLD (non-editing) behaviour, can move the WebhookURL to specific channel sections."
|
|
|
|
b.Log.Errorln(message)
|
|
|
|
return fmt.Errorf("use of removed WebhookURL setting")
|
|
|
|
}
|
|
|
|
|
2022-02-06 16:58:35 +01:00
|
|
|
if b.GetInt("debuglevel") == 2 {
|
2021-01-23 00:09:56 +01:00
|
|
|
b.Log.Debug("enabling even more discord debug")
|
|
|
|
b.c.Debug = true
|
|
|
|
}
|
|
|
|
|
2020-11-30 06:47:02 +01:00
|
|
|
// Initialise webhook management
|
|
|
|
b.transmitter = transmitter.New(b.c, b.guildID, "matterbridge", b.useAutoWebhooks)
|
|
|
|
b.transmitter.Log = b.Log
|
|
|
|
|
|
|
|
var webhookChannelIDs []string
|
|
|
|
for _, channel := range b.Channels {
|
|
|
|
channelID := b.getChannelID(channel.Name) // note(qaisjp): this readlocks channelsMutex
|
|
|
|
|
|
|
|
// If a WebhookURL was not explicitly provided for this channel,
|
|
|
|
// there are two options: just a regular bot message (ugly) or this is should be webhook sent
|
|
|
|
if channel.Options.WebhookURL == "" {
|
|
|
|
// If it should be webhook sent, we should enforce this via the transmitter
|
|
|
|
if b.useAutoWebhooks {
|
|
|
|
webhookChannelIDs = append(webhookChannelIDs, channelID)
|
2019-01-09 21:28:47 +01:00
|
|
|
}
|
2020-11-30 06:47:02 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
whID, whToken, ok := b.splitURL(channel.Options.WebhookURL)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to parse WebhookURL %#v for channel %#v", channel.Options.WebhookURL, channel.ID)
|
2019-01-09 21:28:47 +01:00
|
|
|
}
|
2020-03-17 17:18:50 +01:00
|
|
|
|
2020-11-30 06:47:02 +01:00
|
|
|
b.transmitter.AddWebhook(channelID, &discordgo.Webhook{
|
|
|
|
ID: whID,
|
|
|
|
Token: whToken,
|
|
|
|
GuildID: b.guildID,
|
|
|
|
ChannelID: channelID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.useAutoWebhooks {
|
|
|
|
err = b.transmitter.RefreshGuildWebhooks(webhookChannelIDs)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.WithError(err).Println("transmitter could not refresh guild webhooks")
|
|
|
|
return err
|
2020-11-22 21:26:49 +01:00
|
|
|
}
|
2018-03-17 22:56:58 +01:00
|
|
|
}
|
2018-12-07 23:36:01 +01:00
|
|
|
|
|
|
|
// Obtaining guild members and initializing nickname mapping.
|
|
|
|
b.membersMutex.Lock()
|
|
|
|
defer b.membersMutex.Unlock()
|
2018-09-17 21:25:06 +02:00
|
|
|
members, err := b.c.GuildMembers(b.guildID, "", 1000)
|
|
|
|
if err != nil {
|
2019-01-07 21:39:53 +01:00
|
|
|
b.Log.Error("Error obtaining server members: ", err)
|
2018-09-17 21:25:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, member := range members {
|
2018-12-07 23:36:01 +01:00
|
|
|
if member == nil {
|
|
|
|
b.Log.Warnf("Skipping missing information for a user.")
|
|
|
|
continue
|
|
|
|
}
|
2018-09-17 21:25:06 +02:00
|
|
|
b.userMemberMap[member.User.ID] = member
|
|
|
|
b.nickMemberMap[member.User.Username] = member
|
|
|
|
if member.Nick != "" {
|
|
|
|
b.nickMemberMap[member.Nick] = member
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 21:05:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-24 23:22:15 +01:00
|
|
|
func (b *Bdiscord) Disconnect() error {
|
2018-03-04 23:52:14 +01:00
|
|
|
return b.c.Close()
|
2017-02-14 21:12:02 +01:00
|
|
|
}
|
|
|
|
|
2018-02-24 23:22:15 +01:00
|
|
|
func (b *Bdiscord) JoinChannel(channel config.ChannelInfo) error {
|
2018-12-07 23:36:01 +01:00
|
|
|
b.channelsMutex.Lock()
|
|
|
|
defer b.channelsMutex.Unlock()
|
|
|
|
|
2017-08-12 14:51:41 +02:00
|
|
|
b.channelInfoMap[channel.ID] = &channel
|
2016-09-19 21:05:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-24 23:22:15 +01:00
|
|
|
func (b *Bdiscord) Send(msg config.Message) (string, error) {
|
2018-02-28 22:23:29 +01:00
|
|
|
b.Log.Debugf("=> Receiving %#v", msg)
|
2018-02-24 00:05:43 +01:00
|
|
|
|
2016-09-19 21:05:13 +02:00
|
|
|
channelID := b.getChannelID(msg.Channel)
|
|
|
|
if channelID == "" {
|
2018-02-24 00:05:43 +01:00
|
|
|
return "", fmt.Errorf("Could not find channelID for %v", msg.Channel)
|
2016-09-19 21:05:13 +02:00
|
|
|
}
|
2018-02-24 00:05:43 +01:00
|
|
|
|
2019-10-04 00:18:56 +02:00
|
|
|
if msg.Event == config.EventUserTyping {
|
|
|
|
if b.GetBool("ShowUserTyping") {
|
|
|
|
err := b.c.ChannelTyping(channelID)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2018-02-24 00:05:43 +01:00
|
|
|
// Make a action /me of the message
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event == config.EventUserAction {
|
2017-07-31 21:37:19 +02:00
|
|
|
msg.Text = "_" + msg.Text + "_"
|
|
|
|
}
|
2017-08-12 14:51:41 +02:00
|
|
|
|
2020-12-31 17:15:42 +01:00
|
|
|
// Handle prefix hint for unthreaded messages.
|
2020-12-31 19:01:57 +01:00
|
|
|
if msg.ParentNotFound() {
|
2020-12-31 17:15:42 +01:00
|
|
|
msg.ParentID = ""
|
|
|
|
}
|
|
|
|
|
2018-02-24 00:05:43 +01:00
|
|
|
// Use webhook to send the message
|
2020-11-30 06:47:02 +01:00
|
|
|
useWebhooks := b.shouldMessageUseWebhooks(&msg)
|
2020-12-31 16:59:47 +01:00
|
|
|
if useWebhooks && msg.Event != config.EventMsgDelete && msg.ParentID == "" {
|
2020-12-06 18:47:10 +01:00
|
|
|
return b.handleEventWebhook(&msg, channelID)
|
2018-02-24 00:05:43 +01:00
|
|
|
}
|
2017-11-03 00:05:10 +01:00
|
|
|
|
2020-12-06 18:47:10 +01:00
|
|
|
return b.handleEventBotUser(&msg, channelID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleEventDirect handles events via the bot user
|
|
|
|
func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (string, error) {
|
2018-02-27 00:33:21 +01:00
|
|
|
b.Log.Debugf("Broadcasting using token (API)")
|
2018-02-24 00:05:43 +01:00
|
|
|
|
|
|
|
// Delete message
|
2018-11-15 20:43:43 +01:00
|
|
|
if msg.Event == config.EventMsgDelete {
|
2018-02-24 00:05:43 +01:00
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
2017-11-03 00:05:10 +01:00
|
|
|
}
|
2018-02-24 00:05:43 +01:00
|
|
|
err := b.c.ChannelMessageDelete(channelID, msg.ID)
|
|
|
|
return "", err
|
|
|
|
}
|
2017-11-04 14:47:14 +01:00
|
|
|
|
2022-02-05 14:45:54 +01:00
|
|
|
// Delete a file
|
|
|
|
if msg.Event == config.EventFileDelete {
|
|
|
|
if msg.ID == "" {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi, ok := b.cache.Get(cFileUpload + msg.ID); ok {
|
|
|
|
err := b.c.ChannelMessageDelete(channelID, fi.(string)) // nolint:forcetypeassert
|
|
|
|
b.cache.Remove(cFileUpload + msg.ID)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("file %s not found", msg.ID)
|
|
|
|
}
|
|
|
|
|
2018-02-24 00:05:43 +01:00
|
|
|
// Upload a file if it exists
|
|
|
|
if msg.Extra != nil {
|
2020-12-06 18:47:10 +01:00
|
|
|
for _, rmsg := range helper.HandleExtra(msg, b.General) {
|
2021-05-27 21:45:23 +02:00
|
|
|
rmsg.Text = helper.ClipMessage(rmsg.Text, MessageLength, b.GetString("MessageClipped"))
|
2018-12-07 23:36:01 +01:00
|
|
|
if _, err := b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text); err != nil {
|
2019-02-26 17:21:23 +01:00
|
|
|
b.Log.Errorf("Could not send message %#v: %s", rmsg, err)
|
2018-12-07 23:36:01 +01:00
|
|
|
}
|
2018-02-24 00:05:43 +01:00
|
|
|
}
|
|
|
|
// check if we have files to upload (from slack, telegram or mattermost)
|
|
|
|
if len(msg.Extra["file"]) > 0 {
|
2020-12-06 18:47:10 +01:00
|
|
|
return b.handleUploadFile(msg, channelID)
|
2017-08-28 00:33:17 +02:00
|
|
|
}
|
2018-02-24 00:05:43 +01:00
|
|
|
}
|
|
|
|
|
2021-05-27 21:45:23 +02:00
|
|
|
msg.Text = helper.ClipMessage(msg.Text, MessageLength, b.GetString("MessageClipped"))
|
2018-09-12 22:30:14 +02:00
|
|
|
msg.Text = b.replaceUserMentions(msg.Text)
|
|
|
|
|
2018-02-24 00:05:43 +01:00
|
|
|
// Edit message
|
|
|
|
if msg.ID != "" {
|
|
|
|
_, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text)
|
|
|
|
return msg.ID, err
|
|
|
|
}
|
|
|
|
|
2020-12-31 16:59:47 +01:00
|
|
|
m := discordgo.MessageSend{
|
2021-05-13 22:39:25 +02:00
|
|
|
Content: msg.Username + msg.Text,
|
|
|
|
AllowedMentions: b.getAllowedMentions(),
|
2020-12-31 16:59:47 +01:00
|
|
|
}
|
|
|
|
|
2020-12-31 19:01:57 +01:00
|
|
|
if msg.ParentValid() {
|
2020-12-31 16:59:47 +01:00
|
|
|
m.Reference = &discordgo.MessageReference{
|
|
|
|
MessageID: msg.ParentID,
|
|
|
|
ChannelID: channelID,
|
|
|
|
GuildID: b.guildID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 00:05:43 +01:00
|
|
|
// Post normal message
|
2020-12-31 16:59:47 +01:00
|
|
|
res, err := b.c.ChannelMessageSendComplex(channelID, &m)
|
2018-02-24 00:05:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-12-31 16:59:47 +01:00
|
|
|
|
2019-08-29 00:13:10 +02:00
|
|
|
return res.ID, nil
|
2016-09-19 21:05:13 +02:00
|
|
|
}
|
|
|
|
|
2018-02-24 00:05:43 +01:00
|
|
|
// handleUploadFile handles native upload of files
|
2018-02-24 23:22:15 +01:00
|
|
|
func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (string, error) {
|
2018-02-24 00:05:43 +01:00
|
|
|
for _, f := range msg.Extra["file"] {
|
|
|
|
fi := f.(config.FileInfo)
|
2018-11-07 20:36:50 +01:00
|
|
|
file := discordgo.File{
|
|
|
|
Name: fi.Name,
|
|
|
|
ContentType: "",
|
|
|
|
Reader: bytes.NewReader(*fi.Data),
|
|
|
|
}
|
|
|
|
m := discordgo.MessageSend{
|
2021-05-13 22:39:25 +02:00
|
|
|
Content: msg.Username + fi.Comment,
|
|
|
|
Files: []*discordgo.File{&file},
|
|
|
|
AllowedMentions: b.getAllowedMentions(),
|
2018-11-07 20:36:50 +01:00
|
|
|
}
|
2022-02-05 14:45:54 +01:00
|
|
|
res, err := b.c.ChannelMessageSendComplex(channelID, &m)
|
2018-02-24 00:05:43 +01:00
|
|
|
if err != nil {
|
2019-02-26 17:21:23 +01:00
|
|
|
return "", fmt.Errorf("file upload failed: %s", err)
|
2018-02-24 00:05:43 +01:00
|
|
|
}
|
2022-02-05 14:45:54 +01:00
|
|
|
|
|
|
|
// link file_upload_nativeID (file ID from the original bridge) to our upload id
|
|
|
|
// so that we can remove this later when it eg needs to be deleted
|
|
|
|
b.cache.Add(cFileUpload+fi.NativeID, res.ID)
|
2018-02-24 00:05:43 +01:00
|
|
|
}
|
2022-02-05 14:45:54 +01:00
|
|
|
|
2018-02-24 00:05:43 +01:00
|
|
|
return "", nil
|
|
|
|
}
|