discord embed changes

This commit is contained in:
Sotirios Pupakis
2025-09-07 05:13:06 +02:00
parent 557eedf02f
commit 64d9f51f19
7 changed files with 58 additions and 53 deletions

View File

@@ -0,0 +1,2 @@
package controllers

View File

@@ -15,3 +15,12 @@ func CheckAllowed(c *gin.Context) {
return
}
}
func CheckAdmin(c *gin.Context) {
adminSecret := c.GetHeader("X-ADMIN-KEY")
if adminSecret != initializers.ADMIN_SECRET {
c.JSON(http.StatusForbidden, gin.H{"error": "Forbidden"})
c.Abort()
return
}
}

View File

@@ -12,8 +12,6 @@ import (
"gitea.tbdevent.eu/TBD/reforger_crawler_main/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
discordwebhook "github.com/bensch777/discord-webhook-golang"
)
func GetNextToBeIndexed(c *gin.Context) {
@@ -85,20 +83,26 @@ func SaveIndexingResult(c *gin.Context) {
return
}
ret = initializers.DB.CreateInBatches(&result.Files, 100)
if ret.Error != nil {
c.JSON(500, gin.H{"error": ret.Error.Error()})
return
}
addon.CurrentVersionNumber = result.CurrentVersion
addon.IsBeingIndexed = false
addon.ToBeIndexed = false
initializers.DB.Save(&addon)
// Add new files with versionID
for _, file := range result.Files {
initializers.DB.Create(&file)
ret = initializers.DB.Save(&addon)
if ret.Error != nil {
c.JSON(500, gin.H{"error": ret.Error.Error()})
return
}
if initializers.DiscordWebhook != "" {
text := "Indexing of addon " + addon.ID + " (" + addon.Name + ") was successful."
text := "Indexing of addon " + addon.Name + " (" + addon.ID + ") was successful."
colour := 2228479
nbr := strconv.Itoa(len(result.Files))
size := fmt.Sprintf("%.2f MB", float64(addon.CurrentVersionSize)/1_000_000)
addonsToBeIndexed := int64(0)
addonsCount := int64(0)
@@ -120,6 +124,23 @@ func SaveIndexingResult(c *gin.Context) {
Image: models.CustomImage{
URL: addon.Preview,
},
Fields: []models.CustomEmbedField{
{
Name: "Size",
Value: size,
Inline: true,
},
{
Name: "Current Version",
Value: addon.CurrentVersionNumber,
Inline: true,
},
// {
// Name: "Indexed by",
// Value: c.ClientIP(),
// Inline: false,
// },
},
}
myHook := models.CustomHook{
@@ -131,41 +152,6 @@ func SaveIndexingResult(c *gin.Context) {
if err != nil {
fmt.Println("Error sending webhook:", err)
}
/*embed := discordwebhook.Embed{
Title: text,
Thumbnail: discordwebhook.Thumbnail{
Url: addon.Preview,
},
Description: txt,
Color: colour,
Timestamp: time.Now(),
Fields: []discordwebhook.Field{
discordwebhook.Field{
Name: "Name",
Value: addon.Name,
Inline: true,
},
discordwebhook.Field{
Name: "GUID",
Value: addon.ID,
Inline: true,
},
discordwebhook.Field{
Name: "Version",
Value: addon.CurrentVersionNumber,
Inline: false,
},
discordwebhook.Field{
Name: "Files Indexed",
Value: nbr,
Inline: false,
},
},
}
sendEmbed(initializers.DiscordWebhook, embed)*/
}
c.JSON(200, gin.H{"status": "success"})
@@ -183,16 +169,6 @@ func checkIndexingTimeout() {
}
}
func sendEmbed(webhookUrl string, embed discordwebhook.Embed) {
hook := discordwebhook.Hook{
Username: "Reforger Crawler",
Embeds: []discordwebhook.Embed{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 {