diff --git a/controllers/indexerController.go b/controllers/indexerController.go index 8607555..b1365f3 100644 --- a/controllers/indexerController.go +++ b/controllers/indexerController.go @@ -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 +} \ No newline at end of file diff --git a/models/discordhook.go b/models/discordhook.go new file mode 100644 index 0000000..a1c5d22 --- /dev/null +++ b/models/discordhook.go @@ -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"` +}