summaryrefslogtreecommitdiffstats
path: root/bridge/discord
diff options
context:
space:
mode:
authorWim <wim@42.be>2017-08-28 00:33:17 +0200
committerWim <wim@42.be>2017-08-28 00:33:17 +0200
commit7c773ebae018ccb9cdf4ae3f21cbe34ffb23cbfe (patch)
tree0abdc66a0c407635b1d8faec3dec13506c5e3931 /bridge/discord
parente84417430d02663eb83e1ef8757c3b1bf7609bae (diff)
downloadmatterbridge-msglm-7c773ebae018ccb9cdf4ae3f21cbe34ffb23cbfe.tar.gz
matterbridge-msglm-7c773ebae018ccb9cdf4ae3f21cbe34ffb23cbfe.tar.bz2
matterbridge-msglm-7c773ebae018ccb9cdf4ae3f21cbe34ffb23cbfe.zip
Add support for editing messages across bridges. Currently mattermost/discord.
Our Message type has an extra ID field which contains the message ID of the specific bridge. The Send() function has been modified to return a msg ID (after the message to that specific bridge has been created). There is a lru cache of 5000 entries (message IDs). All in memory, so editing messages will only work for messages the bot has seen. Currently we go out from the idea that every message ID is unique, so we don't keep the ID separate for each bridge. (we do for each gateway though) If there's a new message from a bridge, we put that message ID in the LRU cache as key and the []*BrMsgID as value (this slice contains the message ID's of each bridge that received the new message) If there's a new message and this message ID already exists in the cache, it must be an updated message. The value from the cache gets checked for each bridge and if there is a message ID for this bridge, the ID will be added to the Message{} sent to that bridge. If the bridge sees that the ID isn't empty, it'll know it has to update the message with that specific ID instead of creating a new message.
Diffstat (limited to 'bridge/discord')
-rw-r--r--bridge/discord/discord.go37
1 files changed, 22 insertions, 15 deletions
diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go
index b44ad898..ffbada40 100644
--- a/bridge/discord/discord.go
+++ b/bridge/discord/discord.go
@@ -129,20 +129,27 @@ func (b *bdiscord) Send(msg config.Message) (string, error) {
if wID == "" {
flog.Debugf("Broadcasting using token (API)")
- b.c.ChannelMessageSend(channelID, msg.Username+msg.Text)
- } else {
- flog.Debugf("Broadcasting using Webhook")
- b.c.WebhookExecute(
- wID,
- wToken,
- true,
- &discordgo.WebhookParams{
- Content: msg.Text,
- Username: msg.Username,
- AvatarURL: msg.Avatar,
- })
- }
- return "", nil
+ if msg.ID != "" {
+ _, err := b.c.ChannelMessageEdit(channelID, msg.ID, msg.Username+msg.Text)
+ return msg.ID, err
+ }
+ res, err := b.c.ChannelMessageSend(channelID, msg.Username+msg.Text)
+ if err != nil {
+ return "", err
+ }
+ return res.ID, err
+ }
+ flog.Debugf("Broadcasting using Webhook")
+ err := b.c.WebhookExecute(
+ wID,
+ wToken,
+ true,
+ &discordgo.WebhookParams{
+ Content: msg.Text,
+ Username: msg.Username,
+ AvatarURL: msg.Avatar,
+ })
+ return "", err
}
func (b *bdiscord) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) {
@@ -185,7 +192,7 @@ func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
}
rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg",
- UserID: m.Author.ID}
+ UserID: m.Author.ID, ID: m.ID}
rmsg.Channel = b.getChannelName(m.ChannelID)
if b.UseChannelID {