summaryrefslogtreecommitdiffstats
path: root/bridge/discord/discord.go
blob: c8b810c674bbb29e947da8621a07b4d60f9fa80f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package bdiscord

import (
	"bytes"
	"fmt"
	"strings"
	"sync"

	"github.com/42wim/matterbridge/bridge"
	"github.com/42wim/matterbridge/bridge/config"
	"github.com/42wim/matterbridge/bridge/discord/transmitter"
	"github.com/42wim/matterbridge/bridge/helper"
	"github.com/matterbridge/discordgo"
)

const MessageLength = 1950

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
}

func New(cfg *bridge.Config) bridge.Bridger {
	b := &Bdiscord{Config: cfg}
	b.userMemberMap = make(map[string]*discordgo.Member)
	b.nickMemberMap = make(map[string]*discordgo.Member)
	b.channelInfoMap = make(map[string]*config.ChannelInfo)

	b.useAutoWebhooks = b.GetBool("AutoWebhooks")
	if b.useAutoWebhooks {
		b.Log.Debug("Using automatic webhooks")
	}
	return b
}

func (b *Bdiscord) Connect() error {
	var err error
	token := b.GetString("Token")
	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
	if strings.HasPrefix(b.GetString("Token"), "User ") {
		token = strings.Replace(b.GetString("Token"), "User ", "", -1)
	}

	b.c, err = discordgo.New(token)
	if err != nil {
		return err
	}
	b.Log.Info("Connection succeeded")
	b.c.AddHandler(b.messageCreate)
	b.c.AddHandler(b.messageTyping)
	b.c.AddHandler(b.memberUpdate)
	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)
	err = b.c.Open()
	if err != nil {
		return err
	}
	guilds, err := b.c.UserGuilds(100, "", "")
	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()
	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)
		}

		// 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
	}
	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
	}

	// 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")
	}

	// 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)
		}

		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
		}
	}

	// 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
		}
	}
	return nil
}

func (b *Bdiscord) Disconnect() error {
	return b.c.Close()
}

func (b *Bdiscord) JoinChannel(channel config.ChannelInfo) error {
	b.channelsMutex.Lock()
	defer b.channelsMutex.Unlock()

	b.channelInfoMap[channel.ID] = &channel
	return nil
}

func (b *Bdiscord) Send(msg config.Message) (string, error) {
	b.Log.Debugf("=> Receiving %#v", msg)

	channelID := b.getChannelID(msg.Channel)
	if channelID == "" {
		return "", fmt.Errorf("Could not find channelID for %v", msg.Channel)
	}

	if msg.Event == config.EventUserTyping {
		if b.GetBool("ShowUserTyping") {
			err := b.c.ChannelTyping(channelID)
			return "", err
		}
		return "", nil
	}

	// Make a action /me of the message
	if msg.Event == config.EventUserAction {
		msg.Text = "_" + msg.Text + "_"
	}

	// Use webhook to send the message
	useWebhooks := b.shouldMessageUseWebhooks(&msg)
	if useWebhooks && msg.Event != config.EventMsgDelete && msg.ParentID == "" {
		return b.handleEventWebhook(&msg, channelID)
	}

	return b.handleEventBotUser(&msg, channelID)
}

// handleEventDirect handles events via the bot user
func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (string, error) {
	b.Log.Debugf("Broadcasting using token (API)")

	// Delete message
	if msg.Event == config.EventMsgDelete {
		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) {
			rmsg.Text = helper.ClipMessage(rmsg.Text, MessageLength)
			if _, err := b.c.ChannelMessageSend(channelID, rmsg.Username+rmsg.Text); err != nil {
				b.Log.Errorf("Could not send message %#v: %s", rmsg, err)
			}
		}
		// check if we have files to upload (from slack, telegram or mattermost)
		if len(msg.Extra["file"]) > 0 {
			return b.handleUploadFile(msg, channelID)
		}
	}

	msg.Text = helper.ClipMessage(msg.Text, MessageLength)
	msg.Text = b.replaceUserMentions(msg.Text)

	// Edit message
	if msg.ID != "" {
		_, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text)
		return msg.ID, err
	}

	m := discordgo.MessageSend{
		Content: msg.Username + msg.Text,
	}

	if msg.ParentID != "" && msg.ParentID != "msg-parent-not-found" {
		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
	}

	return res.ID, nil
}

// 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)
		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)
		if err != nil {
			return "", fmt.Errorf("file upload failed: %s", err)
		}
	}
	return "", nil
}