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

@@ -3,3 +3,4 @@ ip: "localhost"
secret: "secret"
db: "crawler.db"
discordWebhook: "https://discord.com/api/webhooks/1413673169792925787/bfbCJ8wiFeKsYkLGCp1U8sr5jOOWDcF0NP9DsHuGXy8NjrfKOncYWJbPnROophTXr-kH"
adminSecret: "123456789"

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 {

View File

@@ -16,6 +16,7 @@ var IP string
var SECRET string
var DB_NAME string
var DiscordWebhook string
var ADMIN_SECRET string
func ConnectToDB() {
db, err := gorm.Open(sqlite.Open(DB_NAME), &gorm.Config{})
@@ -34,6 +35,7 @@ type Configuration struct {
Secret string `yaml:"secret"`
DB string `yaml:"db"`
DiscordWebhook string `yaml:"discordWebhook"`
ADMIN_SECRET string `yaml:"adminSecret"`
}
func Load() {
@@ -55,4 +57,5 @@ file, err := os.ReadFile("config.yaml")
SECRET = configuration.Secret
DB_NAME = configuration.DB
DiscordWebhook = configuration.DiscordWebhook
ADMIN_SECRET = configuration.ADMIN_SECRET
}

View File

@@ -26,6 +26,13 @@ func main() {
back.POST("/submitAddon", controllers.CheckAllowed, controllers.SaveIndexingResult)
}
admin := r.Group("/admin")
{
admin.GET("/duplicates", controllers.CheckAdmin)
admin.POST("/setPriority", controllers.CheckAdmin)
admin.POST("/setToBeIndexed", controllers.CheckAdmin)
}
r.Run(initializers.IP + ":" + initializers.PORT)
}

View File

@@ -16,9 +16,16 @@ type CustomEmbed struct {
Timestamp time.Time `json:"timestamp,omitempty"`
Color int `json:"color,omitempty"`
Image CustomImage `json:"image,omitempty"` // Your new field
Fields []CustomEmbedField `json:"fields,omitempty"`
// You can add other fields like Footer, Author, Fields as needed.
}
type CustomEmbedField struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
Inline bool `json:"inline,omitempty"`
}
// CustomHook is the top-level structure for the webhook payload.
type CustomHook struct {
Username string `json:"username,omitempty"`