Merge 4ec929a4bd4fa13d060584afdf733f26d806d3ab into c4157a4d5b49fce79c80a30730dc7c404bacd663

This commit is contained in:
cryox-dev 2024-08-30 12:32:40 +08:00 committed by GitHub
commit 4a487f7bd1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 1 deletions

View File

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

View File

@ -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()

View File

@ -125,6 +125,21 @@ func (b *Bdiscord) webhookSend(msg *config.Message, channelID string) (string, e
}
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