custom discord hook

This commit is contained in:
Sotirios Pupakis
2025-09-06 02:34:26 +02:00
parent 1dd8b1105e
commit ae534a87dd
2 changed files with 77 additions and 2 deletions

View File

@@ -1,8 +1,10 @@
package controllers
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
@@ -102,7 +104,27 @@ func SaveIndexingResult(c *gin.Context) {
txt := fmt.Sprintf("%d/%d (%.2f%%).", addonsCount - addonsToBeIndexed, addonsCount, percentage)
embed := discordwebhook.Embed{
myEmbed := models.CustomEmbed{
Title: text,
Description: fmt.Sprintf("Files Indexed: %s\nOverall Progress: %s", nbr, txt),
Color: colour,
Timestamp: time.Now(),
Image: models.CustomImage{
URL: addon.Preview,
},
}
myHook := models.CustomHook{
Username: "Reforger Crawler",
Embeds: []models.CustomEmbed{myEmbed},
}
err := SendCustomWebhook(initializers.DiscordWebhook, myHook)
if err != nil {
fmt.Println("Error sending webhook:", err)
}
/*embed := discordwebhook.Embed{
Title: text,
Thumbnail: discordwebhook.Thumbnail{
Url: addon.Preview,
@@ -135,7 +157,7 @@ func SaveIndexingResult(c *gin.Context) {
}
sendEmbed(initializers.DiscordWebhook, embed)
sendEmbed(initializers.DiscordWebhook, embed)*/
}
c.JSON(200, gin.H{"status": "success"})
@@ -162,3 +184,29 @@ func sendEmbed(webhookUrl string, embed discordwebhook.Embed) {
payload, _ := json.Marshal(hook)
discordwebhook.ExecuteWebhook(webhookUrl, payload)
}
func SendCustomWebhook(webhookURL string, hook models.CustomHook) error {
payload, err := json.Marshal(hook)
if err != nil {
return err
}
req, err := http.NewRequest("POST", webhookURL, bytes.NewBuffer(payload))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("webhook failed with status code %d", resp.StatusCode)
}
return nil
}