2018-12-07 14:36:01 -08:00
|
|
|
package bdiscord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/42wim/matterbridge/bridge/config"
|
2020-03-08 09:08:18 -07:00
|
|
|
"github.com/matterbridge/discordgo"
|
2018-12-07 14:36:01 -08:00
|
|
|
)
|
|
|
|
|
2018-12-07 14:48:00 -08:00
|
|
|
func (b *Bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelete) { //nolint:unparam
|
2018-12-07 14:36:01 -08:00
|
|
|
rmsg := config.Message{Account: b.Account, ID: m.ID, Event: config.EventMsgDelete, Text: config.EventMsgDelete}
|
|
|
|
rmsg.Channel = b.getChannelName(m.ChannelID)
|
2020-01-09 14:54:04 -08:00
|
|
|
|
2018-12-07 14:36:01 -08:00
|
|
|
b.Log.Debugf("<= Sending message from %s to gateway", b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:15:18 -07:00
|
|
|
// TODO(qaisjp): if other bridges support bulk deletions, it could be fanned out centrally
|
|
|
|
func (b *Bdiscord) messageDeleteBulk(s *discordgo.Session, m *discordgo.MessageDeleteBulk) { //nolint:unparam
|
|
|
|
for _, msgID := range m.Messages {
|
|
|
|
rmsg := config.Message{
|
|
|
|
Account: b.Account,
|
|
|
|
ID: msgID,
|
|
|
|
Event: config.EventMsgDelete,
|
|
|
|
Text: config.EventMsgDelete,
|
2020-01-09 14:54:04 -08:00
|
|
|
Channel: b.getChannelName(m.ChannelID),
|
2019-06-15 16:15:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
b.Log.Debugf("<= Sending message from %s to gateway", b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:18:56 -07:00
|
|
|
func (b *Bdiscord) messageTyping(s *discordgo.Session, m *discordgo.TypingStart) {
|
|
|
|
if !b.GetBool("ShowUserTyping") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-22 10:39:11 -07:00
|
|
|
// Ignore our own typing messages
|
|
|
|
if m.UserID == b.userID {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:18:56 -07:00
|
|
|
rmsg := config.Message{Account: b.Account, Event: config.EventUserTyping}
|
|
|
|
rmsg.Channel = b.getChannelName(m.ChannelID)
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:48:00 -08:00
|
|
|
func (b *Bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) { //nolint:unparam
|
2018-12-07 14:36:01 -08:00
|
|
|
if b.GetBool("EditDisable") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// only when message is actually edited
|
|
|
|
if m.Message.EditedTimestamp != "" {
|
|
|
|
b.Log.Debugf("Sending edit message")
|
|
|
|
m.Content += b.GetString("EditSuffix")
|
2019-10-26 16:45:57 -07:00
|
|
|
msg := &discordgo.MessageCreate{
|
|
|
|
Message: m.Message,
|
|
|
|
}
|
|
|
|
b.messageCreate(s, msg)
|
2018-12-07 14:36:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:48:00 -08:00
|
|
|
func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { //nolint:unparam
|
2018-12-07 14:36:01 -08:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// not relay our own messages
|
|
|
|
if m.Author.Username == b.nick {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// if using webhooks, do not relay if it's ours
|
2020-11-29 21:47:02 -08:00
|
|
|
if m.Author.Bot && b.transmitter.HasWebhook(m.Author.ID) {
|
2018-12-07 14:36:01 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the url of the attachments to content
|
|
|
|
if len(m.Attachments) > 0 {
|
|
|
|
for _, attach := range m.Attachments {
|
|
|
|
m.Content = m.Content + "\n" + attach.URL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 != "" {
|
|
|
|
b.Log.Debugf("== Receiving event %#v", m.Message)
|
|
|
|
m.Message.Content = b.replaceChannelMentions(m.Message.Content)
|
|
|
|
rmsg.Text, err = m.ContentWithMoreMentionsReplaced(b.c)
|
|
|
|
if err != nil {
|
|
|
|
b.Log.Errorf("ContentWithMoreMentionsReplaced failed: %s", err)
|
|
|
|
rmsg.Text = m.ContentWithMentionsReplaced()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set channel name
|
|
|
|
rmsg.Channel = b.getChannelName(m.ChannelID)
|
|
|
|
|
2020-03-18 15:12:48 -07:00
|
|
|
fromWebhook := m.WebhookID != ""
|
|
|
|
if !fromWebhook && !b.GetBool("UseUserName") {
|
2019-09-15 11:25:42 -07:00
|
|
|
rmsg.Username = b.getNick(m.Author, m.GuildID)
|
2018-12-07 14:36:01 -08:00
|
|
|
} else {
|
|
|
|
rmsg.Username = m.Author.Username
|
2020-03-18 15:12:48 -07:00
|
|
|
if !fromWebhook && b.GetBool("UseDiscriminator") {
|
2019-02-22 05:28:27 -08:00
|
|
|
rmsg.Username += "#" + m.Author.Discriminator
|
|
|
|
}
|
2018-12-07 14:36:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we have embedded content add it to text
|
|
|
|
if b.GetBool("ShowEmbeds") && m.Message.Embeds != nil {
|
|
|
|
for _, embed := range m.Message.Embeds {
|
2020-03-21 13:27:17 -07:00
|
|
|
rmsg.Text += handleEmbed(embed)
|
2018-12-07 14:36:01 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no empty messages
|
|
|
|
if rmsg.Text == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// do we have a /me action
|
|
|
|
var ok bool
|
|
|
|
rmsg.Text, ok = b.replaceAction(rmsg.Text)
|
|
|
|
if ok {
|
|
|
|
rmsg.Event = config.EventUserAction
|
|
|
|
}
|
|
|
|
|
2020-03-22 06:16:31 -07:00
|
|
|
// Replace emotes
|
|
|
|
rmsg.Text = replaceEmotes(rmsg.Text)
|
|
|
|
|
2020-12-31 07:59:47 -08:00
|
|
|
// Add our parent id if it exists
|
|
|
|
if m.MessageReference != nil {
|
|
|
|
rmsg.ParentID = m.MessageReference.MessageID
|
|
|
|
}
|
|
|
|
|
2018-12-07 14:36:01 -08:00
|
|
|
b.Log.Debugf("<= Sending message from %s on %s to gateway", m.Author.Username, b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bdiscord) memberUpdate(s *discordgo.Session, m *discordgo.GuildMemberUpdate) {
|
|
|
|
if m.Member == nil {
|
|
|
|
b.Log.Warnf("Received member update with no member information: %#v", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.membersMutex.Lock()
|
|
|
|
defer b.membersMutex.Unlock()
|
|
|
|
|
|
|
|
if currMember, ok := b.userMemberMap[m.Member.User.ID]; ok {
|
|
|
|
b.Log.Debugf(
|
|
|
|
"%s: memberupdate: user %s (nick %s) changes nick to %s",
|
|
|
|
b.Account,
|
|
|
|
m.Member.User.Username,
|
|
|
|
b.userMemberMap[m.Member.User.ID].Nick,
|
|
|
|
m.Member.Nick,
|
|
|
|
)
|
|
|
|
delete(b.nickMemberMap, currMember.User.Username)
|
|
|
|
delete(b.nickMemberMap, currMember.Nick)
|
|
|
|
delete(b.userMemberMap, m.Member.User.ID)
|
|
|
|
}
|
|
|
|
b.userMemberMap[m.Member.User.ID] = m.Member
|
|
|
|
b.nickMemberMap[m.Member.User.Username] = m.Member
|
|
|
|
if m.Member.Nick != "" {
|
|
|
|
b.nickMemberMap[m.Member.Nick] = m.Member
|
|
|
|
}
|
|
|
|
}
|
2019-02-17 12:49:45 -08:00
|
|
|
|
|
|
|
func (b *Bdiscord) memberAdd(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
|
|
|
|
if m.Member == nil {
|
|
|
|
b.Log.Warnf("Received member update with no member information: %#v", m)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username := m.Member.User.Username
|
|
|
|
if m.Member.Nick != "" {
|
|
|
|
username = m.Member.Nick
|
|
|
|
}
|
|
|
|
|
|
|
|
rmsg := config.Message{
|
|
|
|
Account: b.Account,
|
|
|
|
Event: config.EventJoinLeave,
|
|
|
|
Username: "system",
|
|
|
|
Text: username + " joins",
|
|
|
|
}
|
|
|
|
b.Log.Debugf("<= Sending message from %s to gateway", b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bdiscord) memberRemove(s *discordgo.Session, m *discordgo.GuildMemberRemove) {
|
|
|
|
if m.Member == nil {
|
|
|
|
b.Log.Warnf("Received member update with no member information: %#v", m)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
username := m.Member.User.Username
|
|
|
|
if m.Member.Nick != "" {
|
|
|
|
username = m.Member.Nick
|
|
|
|
}
|
|
|
|
|
|
|
|
rmsg := config.Message{
|
|
|
|
Account: b.Account,
|
|
|
|
Event: config.EventJoinLeave,
|
|
|
|
Username: "system",
|
|
|
|
Text: username + " leaves",
|
|
|
|
}
|
|
|
|
b.Log.Debugf("<= Sending message from %s to gateway", b.Account)
|
|
|
|
b.Log.Debugf("<= Message is %#v", rmsg)
|
|
|
|
b.Remote <- rmsg
|
|
|
|
}
|
2020-03-21 13:27:17 -07:00
|
|
|
|
|
|
|
func handleEmbed(embed *discordgo.MessageEmbed) string {
|
|
|
|
var t []string
|
|
|
|
var result string
|
|
|
|
|
|
|
|
t = append(t, embed.Title)
|
|
|
|
t = append(t, embed.Description)
|
|
|
|
t = append(t, embed.URL)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, e := range t {
|
|
|
|
if e == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
if i == 1 {
|
2020-05-06 15:19:48 -07:00
|
|
|
result += " embed: " + e
|
2020-03-21 13:27:17 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result += " - " + e
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != "" {
|
|
|
|
result += "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|