matterbridge/bridge/discord/discord.go

431 lines
12 KiB
Go
Raw Normal View History

2016-09-19 12:05:13 -07:00
package bdiscord
import (
"bytes"
2018-02-23 15:05:43 -08:00
"fmt"
"strings"
"sync"
2018-02-26 15:33:21 -08:00
"github.com/42wim/matterbridge/bridge"
2016-09-19 12:05:13 -07:00
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/discord/transmitter"
"github.com/42wim/matterbridge/bridge/helper"
"github.com/bwmarrin/discordgo"
lru "github.com/hashicorp/golang-lru"
2016-09-19 12:05:13 -07:00
)
const (
MessageLength = 1950
cFileUpload = "file_upload"
)
2018-02-24 14:22:15 -08:00
type Bdiscord struct {
*bridge.Config
c *discordgo.Session
nick string
userID string
guildID string
channelsMutex sync.RWMutex
channels []*discordgo.Channel
channelInfoMap map[string]*config.ChannelInfo
membersMutex sync.RWMutex
userMemberMap map[string]*discordgo.Member
nickMemberMap map[string]*discordgo.Member
// Webhook specific logic
useAutoWebhooks bool
transmitter *transmitter.Transmitter
cache *lru.Cache
2016-09-19 12:05:13 -07:00
}
func New(cfg *bridge.Config) bridge.Bridger {
newCache, err := lru.New(5000)
if err != nil {
cfg.Log.Fatalf("Could not create LRU cache: %v", err)
}
b := &Bdiscord{
Config: cfg,
cache: newCache,
}
b.userMemberMap = make(map[string]*discordgo.Member)
b.nickMemberMap = make(map[string]*discordgo.Member)
b.channelInfoMap = make(map[string]*config.ChannelInfo)
2020-11-29 21:57:16 -08:00
b.useAutoWebhooks = b.GetBool("AutoWebhooks")
if b.useAutoWebhooks {
b.Log.Debug("Using automatic webhooks")
}
2016-09-19 12:05:13 -07:00
return b
}
2018-02-24 14:22:15 -08:00
func (b *Bdiscord) Connect() error {
2016-09-19 12:05:13 -07:00
var err error
token := b.GetString("Token")
2018-02-26 15:33:21 -08:00
b.Log.Info("Connecting")
if !strings.HasPrefix(b.GetString("Token"), "Bot ") {
token = "Bot " + b.GetString("Token")
}
// if we have a User token, remove the `Bot` prefix
2019-01-14 10:41:32 -08:00
if strings.HasPrefix(b.GetString("Token"), "User ") {
token = strings.Replace(b.GetString("Token"), "User ", "", -1)
}
2019-01-14 10:41:32 -08:00
b.c, err = discordgo.New(token)
2016-09-19 12:05:13 -07:00
if err != nil {
return err
}
2018-02-26 15:33:21 -08:00
b.Log.Info("Connection succeeded")
// 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 12:05:13 -07:00
err = b.c.Open()
if err != nil {
return err
}
guilds, err := b.c.UserGuilds(100, "", "", false)
2016-09-19 12:05:13 -07:00
if err != nil {
return err
}
userinfo, err := b.c.User("@me")
if err != nil {
return err
}
serverName := strings.Replace(b.GetString("Server"), "ID:", "", -1)
b.nick = userinfo.Username
b.userID = userinfo.ID
// Try and find this account's guild, and populate channels
b.channelsMutex.Lock()
2016-09-19 12:05:13 -07:00
for _, guild := range guilds {
// 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 12:05:13 -07: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 12:05:13 -07:00
}
b.channelsMutex.Unlock()
// 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:")
for _, guild := range guilds {
b.Log.Infof("\t- Server=%#v # by name", guild.Name)
b.Log.Infof("\t- Server=%#v # by ID", guild.ID)
}
// If there are no results, we should say that
if len(guilds) == 0 {
b.Log.Info("\t- (none found)")
}
return err
}
2020-11-29 21:57:16 -08: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")
}
if b.GetInt("debuglevel") == 2 {
b.Log.Debug("enabling even more discord debug")
b.c.Debug = true
}
// 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)
}
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)
}
2020-03-17 09:18:50 -07: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
}
2018-03-17 14:56:58 -07:00
}
// Obtaining guild members and initializing nickname mapping.
b.membersMutex.Lock()
defer b.membersMutex.Unlock()
members, err := b.c.GuildMembers(b.guildID, "", 1000)
if err != nil {
b.Log.Error("Error obtaining server members: ", err)
return err
}
for _, member := range members {
if member == nil {
b.Log.Warnf("Skipping missing information for a user.")
continue
}
b.userMemberMap[member.User.ID] = member
b.nickMemberMap[member.User.Username] = member
if member.Nick != "" {
b.nickMemberMap[member.Nick] = member
}
}
b.c.AddHandler(b.messageCreate)
b.c.AddHandler(b.messageTyping)
b.c.AddHandler(b.messageUpdate)
b.c.AddHandler(b.messageDelete)
b.c.AddHandler(b.messageDeleteBulk)
b.c.AddHandler(b.memberAdd)
b.c.AddHandler(b.memberRemove)
b.c.AddHandler(b.memberUpdate)
if b.GetInt("debuglevel") == 1 {
b.c.AddHandler(b.messageEvent)
}
2016-09-19 12:05:13 -07:00
return nil
}
2018-02-24 14:22:15 -08:00
func (b *Bdiscord) Disconnect() error {
return b.c.Close()
}
2018-02-24 14:22:15 -08:00
func (b *Bdiscord) JoinChannel(channel config.ChannelInfo) error {
b.channelsMutex.Lock()
defer b.channelsMutex.Unlock()
b.channelInfoMap[channel.ID] = &channel
2016-09-19 12:05:13 -07:00
return nil
}
2018-02-24 14:22:15 -08:00
func (b *Bdiscord) Send(msg config.Message) (string, error) {
2018-02-28 13:23:29 -08:00
b.Log.Debugf("=> Receiving %#v", msg)
2018-02-23 15:05:43 -08:00
2016-09-19 12:05:13 -07:00
channelID := b.getChannelID(msg.Channel)
if channelID == "" {
2018-02-23 15:05:43 -08:00
return "", fmt.Errorf("Could not find channelID for %v", msg.Channel)
2016-09-19 12:05:13 -07:00
}
2018-02-23 15:05:43 -08:00
if msg.Event == config.EventUserTyping {
if b.GetBool("ShowUserTyping") {
err := b.c.ChannelTyping(channelID)
return "", err
}
return "", nil
}
2018-02-23 15:05:43 -08:00
// Make a action /me of the message
if msg.Event == config.EventUserAction {
msg.Text = "_" + msg.Text + "_"
}
// Handle prefix hint for unthreaded messages.
if msg.ParentNotFound() {
msg.ParentID = ""
}
2018-02-23 15:05:43 -08:00
// Use webhook to send the message
useWebhooks := b.shouldMessageUseWebhooks(&msg)
if useWebhooks && msg.Event != config.EventMsgDelete && msg.ParentID == "" {
return b.handleEventWebhook(&msg, channelID)
2018-02-23 15:05:43 -08: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-26 15:33:21 -08:00
b.Log.Debugf("Broadcasting using token (API)")
2018-02-23 15:05:43 -08:00
// Delete message
if msg.Event == config.EventMsgDelete {
2018-02-23 15:05:43 -08:00
if msg.ID == "" {
return "", nil
}
2018-02-23 15:05:43 -08:00
err := b.c.ChannelMessageDelete(channelID, msg.ID)
return "", err
}
// 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-23 15:05:43 -08:00
// Upload a file if it exists
if msg.Extra != nil {
for _, rmsg := range helper.HandleExtra(msg, b.General) {
// TODO: Use ClipOrSplitMessage
rmsg.Text = helper.ClipMessage(rmsg.Text, MessageLength, b.GetString("MessageClipped"))
if _, err := b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text); err != nil {
b.Log.Errorf("Could not send message %#v: %s", rmsg, err)
}
2018-02-23 15:05:43 -08:00
}
// check if we have files to upload (from slack, telegram or mattermost)
if len(msg.Extra["file"]) > 0 {
return b.handleUploadFile(msg, channelID)
}
2018-02-23 15:05:43 -08:00
}
// Edit message
if msg.ID != "" {
// Exploit that a discord message ID is actually just a large number, and we encode a list of IDs by separating them with ";".
2024-05-23 15:23:50 -07:00
msgIds := strings.Split(msg.ID, ";")
msgParts := helper.ClipOrSplitMessage(b.replaceUserMentions(msg.Text), MessageLength, b.GetString("MessageClipped"), len(msgIds))
for len(msgParts) < len(msgIds) {
msgParts = append(msgParts, "((obsoleted by edit))")
}
for i := range msgParts {
// In case of split-messages where some parts remain the same (i.e. only a typo-fix in a huge message), this causes some noop-updates.
// TODO: Optimize away noop-updates of un-edited messages
// TODO: Use RemoteNickFormat instead of this broken concatenation
_, err := b.c.ChannelMessageEdit(channelID, msgIds[i], msg.Username+msgParts[i])
if err != nil {
return "", err
}
}
return msg.ID, nil
2018-02-23 15:05:43 -08:00
}
msgParts := helper.ClipOrSplitMessage(b.replaceUserMentions(msg.Text), MessageLength, b.GetString("MessageClipped"), b.GetInt("MessageSplitMaxCount"))
2024-05-23 15:23:50 -07:00
msgIds := []string{}
for _, msgPart := range msgParts {
m := discordgo.MessageSend{
Content: msg.Username + msgPart,
AllowedMentions: b.getAllowedMentions(),
}
if msg.ParentValid() {
m.Reference = &discordgo.MessageReference{
MessageID: msg.ParentID,
ChannelID: channelID,
GuildID: b.guildID,
}
}
// Post normal message
res, err := b.c.ChannelMessageSendComplex(channelID, &m)
if err != nil {
return "", err
}
msgIds = append(msgIds, res.ID)
2018-02-23 15:05:43 -08:00
}
// Exploit that a discord message ID is actually just a large number, so we encode a list of IDs by separating them with ";".
return strings.Join(msgIds, ";"), nil
2016-09-19 12:05:13 -07:00
}
2018-02-23 15:05:43 -08:00
// handleUploadFile handles native upload of files
2018-02-24 14:22:15 -08:00
func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (string, error) {
2018-02-23 15:05:43 -08:00
for _, f := range msg.Extra["file"] {
var err error
2018-02-23 15:05:43 -08:00
fi := f.(config.FileInfo)
if fi.URL != "" {
err = b.handleUploadFileFromURL(msg, channelID, &fi)
} else if fi.Data != nil {
err = b.handleUploadFileFromData(msg, channelID, &fi)
} else {
b.Log.Errorf("Attachment %#v for message %#v had neither Data nor URL", fi, msg)
}
2018-02-23 15:05:43 -08:00
if err != nil {
b.Log.Errorf("Could not send attachment %#v for message %#v: %s", fi, msg, err)
return "", err
2018-02-23 15:05:43 -08:00
}
}
2018-02-23 15:05:43 -08:00
return "", nil
}
func (b *Bdiscord) handleUploadFileFromData(msg *config.Message, channelID string, fi *config.FileInfo) error {
file := discordgo.File{
Name: fi.Name,
ContentType: "",
Reader: bytes.NewReader(*fi.Data),
}
m := discordgo.MessageSend{
Content: msg.Username + fi.Comment,
Files: []*discordgo.File{&file},
AllowedMentions: b.getAllowedMentions(),
}
res, err := b.c.ChannelMessageSendComplex(channelID, &m)
if err != nil {
return fmt.Errorf("file upload failed: %s", err)
}
// 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)
return nil
}
func (b *Bdiscord) handleUploadFileFromURL(msg *config.Message, channelID string, fi *config.FileInfo) error {
_, err := b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{
Content: fi.URL,
AllowedMentions: b.getAllowedMentions(),
})
return err
}