From 103b33f77d1848719bf02974409554c9a5d1bceb Mon Sep 17 00:00:00 2001 From: cryox-dev <153694079+cryox-dev@users.noreply.github.com> Date: Wed, 13 Dec 2023 14:30:01 +1300 Subject: [PATCH 1/3] Add Delete() --- bridge/discord/transmitter/transmitter.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/bridge/discord/transmitter/transmitter.go b/bridge/discord/transmitter/transmitter.go index 71407a1d23..3d5ade7c7d 100644 --- a/bridge/discord/transmitter/transmitter.go +++ b/bridge/discord/transmitter/transmitter.go @@ -94,6 +94,23 @@ func (t *Transmitter) Edit(channelID string, messageID string, params *discordgo return nil } +// Delete will delete a message from a channel, if possible. +func (t *Transmitter) Delete(channelID string, messageID string) error { + wh := t.getWebhook(channelID) + + if wh == nil { + return ErrWebhookNotFound + } + + uri := discordgo.EndpointWebhookToken(wh.ID, wh.Token) + "/messages/" + messageID + _, err := t.session.RequestWithBucketID("DELETE", uri, nil, discordgo.EndpointWebhookToken("", "")) + if err != nil { + return fmt.Errorf("delete failed: %w", err) + } + + return nil +} + // HasWebhook checks whether the transmitter is using a particular webhook. func (t *Transmitter) HasWebhook(id string) bool { t.mutex.RLock() From b32126849960be54a549680f07b4b79239706be4 Mon Sep 17 00:00:00 2001 From: cryox-dev <153694079+cryox-dev@users.noreply.github.com> Date: Wed, 13 Dec 2023 14:34:39 +1300 Subject: [PATCH 2/3] Let handleEventWebhook handle webhook message deletions --- bridge/discord/discord.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 51dbe6bc71..7c496a90cb 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -278,7 +278,7 @@ func (b *Bdiscord) Send(msg config.Message) (string, error) { // Use webhook to send the message useWebhooks := b.shouldMessageUseWebhooks(&msg) - if useWebhooks && msg.Event != config.EventMsgDelete && msg.ParentID == "" { + if useWebhooks && msg.ParentID == "" { return b.handleEventWebhook(&msg, channelID) } From 4ec929a4bd4fa13d060584afdf733f26d806d3ab Mon Sep 17 00:00:00 2001 From: cryox-dev <153694079+cryox-dev@users.noreply.github.com> Date: Wed, 13 Dec 2023 14:35:43 +1300 Subject: [PATCH 3/3] Handle deletion of webhook messages --- bridge/discord/webhook.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/bridge/discord/webhook.go b/bridge/discord/webhook.go index b518ea6262..a9754a32be 100644 --- a/bridge/discord/webhook.go +++ b/bridge/discord/webhook.go @@ -109,6 +109,21 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (*discordg } func (b *Bdiscord) handleEventWebhook(msg *config.Message, channelID string) (string, error) { + if msg.Event == config.EventMsgDelete { + if msg.ID == "" { + return "", nil + } + + err := b.transmitter.Delete(channelID, msg.ID) + if err != nil { + b.Log.Errorf("Could not delete message: %s", err) + return "", err + } + + b.Log.Infof("Message deleted successfully") + return "", nil + } + // skip events if msg.Event != "" && msg.Event != config.EventUserAction && msg.Event != config.EventJoinLeave && msg.Event != config.EventTopicChange { return "", nil