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 package controllers
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http"
"strconv" "strconv"
"time" "time"
@@ -102,7 +104,27 @@ func SaveIndexingResult(c *gin.Context) {
txt := fmt.Sprintf("%d/%d (%.2f%%).", addonsCount - addonsToBeIndexed, addonsCount, percentage) 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, Title: text,
Thumbnail: discordwebhook.Thumbnail{ Thumbnail: discordwebhook.Thumbnail{
Url: addon.Preview, 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"}) c.JSON(200, gin.H{"status": "success"})
@@ -162,3 +184,29 @@ func sendEmbed(webhookUrl string, embed discordwebhook.Embed) {
payload, _ := json.Marshal(hook) payload, _ := json.Marshal(hook)
discordwebhook.ExecuteWebhook(webhookUrl, payload) 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
}

27
models/discordhook.go Normal file
View File

@@ -0,0 +1,27 @@
package models
import (
"time"
)
type CustomImage struct {
URL string `json:"url,omitempty"`
}
// CustomEmbed mirrors the library's Embed but includes the Image field.
type CustomEmbed struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
Color int `json:"color,omitempty"`
Image CustomImage `json:"image,omitempty"` // Your new field
// You can add other fields like Footer, Author, Fields as needed.
}
// CustomHook is the top-level structure for the webhook payload.
type CustomHook struct {
Username string `json:"username,omitempty"`
Content string `json:"content,omitempty"`
Embeds []CustomEmbed `json:"embeds"`
}