Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for incoming webhook deletions on Discord #2109

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bridge/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
17 changes: 17 additions & 0 deletions bridge/discord/transmitter/transmitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions bridge/discord/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down