discord embed changes
This commit is contained in:
@@ -3,3 +3,4 @@ ip: "localhost"
|
|||||||
secret: "secret"
|
secret: "secret"
|
||||||
db: "crawler.db"
|
db: "crawler.db"
|
||||||
discordWebhook: "https://discord.com/api/webhooks/1413673169792925787/bfbCJ8wiFeKsYkLGCp1U8sr5jOOWDcF0NP9DsHuGXy8NjrfKOncYWJbPnROophTXr-kH"
|
discordWebhook: "https://discord.com/api/webhooks/1413673169792925787/bfbCJ8wiFeKsYkLGCp1U8sr5jOOWDcF0NP9DsHuGXy8NjrfKOncYWJbPnROophTXr-kH"
|
||||||
|
adminSecret: "123456789"
|
||||||
2
controllers/adminController.go
Normal file
2
controllers/adminController.go
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
@@ -15,3 +15,12 @@ func CheckAllowed(c *gin.Context) {
|
|||||||
return
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ import (
|
|||||||
"gitea.tbdevent.eu/TBD/reforger_crawler_main/models"
|
"gitea.tbdevent.eu/TBD/reforger_crawler_main/models"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
discordwebhook "github.com/bensch777/discord-webhook-golang"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetNextToBeIndexed(c *gin.Context) {
|
func GetNextToBeIndexed(c *gin.Context) {
|
||||||
@@ -85,20 +83,26 @@ func SaveIndexingResult(c *gin.Context) {
|
|||||||
return
|
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.CurrentVersionNumber = result.CurrentVersion
|
||||||
addon.IsBeingIndexed = false
|
addon.IsBeingIndexed = false
|
||||||
addon.ToBeIndexed = false
|
addon.ToBeIndexed = false
|
||||||
initializers.DB.Save(&addon)
|
ret = initializers.DB.Save(&addon)
|
||||||
|
if ret.Error != nil {
|
||||||
// Add new files with versionID
|
c.JSON(500, gin.H{"error": ret.Error.Error()})
|
||||||
for _, file := range result.Files {
|
return
|
||||||
initializers.DB.Create(&file)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if initializers.DiscordWebhook != "" {
|
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
|
colour := 2228479
|
||||||
nbr := strconv.Itoa(len(result.Files))
|
nbr := strconv.Itoa(len(result.Files))
|
||||||
|
size := fmt.Sprintf("%.2f MB", float64(addon.CurrentVersionSize)/1_000_000)
|
||||||
|
|
||||||
addonsToBeIndexed := int64(0)
|
addonsToBeIndexed := int64(0)
|
||||||
addonsCount := int64(0)
|
addonsCount := int64(0)
|
||||||
@@ -120,6 +124,23 @@ func SaveIndexingResult(c *gin.Context) {
|
|||||||
Image: models.CustomImage{
|
Image: models.CustomImage{
|
||||||
URL: addon.Preview,
|
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{
|
myHook := models.CustomHook{
|
||||||
@@ -131,41 +152,6 @@ func SaveIndexingResult(c *gin.Context) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error sending webhook:", err)
|
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"})
|
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 {
|
func SendCustomWebhook(webhookURL string, hook models.CustomHook) error {
|
||||||
payload, err := json.Marshal(hook)
|
payload, err := json.Marshal(hook)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ var IP string
|
|||||||
var SECRET string
|
var SECRET string
|
||||||
var DB_NAME string
|
var DB_NAME string
|
||||||
var DiscordWebhook string
|
var DiscordWebhook string
|
||||||
|
var ADMIN_SECRET string
|
||||||
|
|
||||||
func ConnectToDB() {
|
func ConnectToDB() {
|
||||||
db, err := gorm.Open(sqlite.Open(DB_NAME), &gorm.Config{})
|
db, err := gorm.Open(sqlite.Open(DB_NAME), &gorm.Config{})
|
||||||
@@ -34,6 +35,7 @@ type Configuration struct {
|
|||||||
Secret string `yaml:"secret"`
|
Secret string `yaml:"secret"`
|
||||||
DB string `yaml:"db"`
|
DB string `yaml:"db"`
|
||||||
DiscordWebhook string `yaml:"discordWebhook"`
|
DiscordWebhook string `yaml:"discordWebhook"`
|
||||||
|
ADMIN_SECRET string `yaml:"adminSecret"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func Load() {
|
func Load() {
|
||||||
@@ -55,4 +57,5 @@ file, err := os.ReadFile("config.yaml")
|
|||||||
SECRET = configuration.Secret
|
SECRET = configuration.Secret
|
||||||
DB_NAME = configuration.DB
|
DB_NAME = configuration.DB
|
||||||
DiscordWebhook = configuration.DiscordWebhook
|
DiscordWebhook = configuration.DiscordWebhook
|
||||||
|
ADMIN_SECRET = configuration.ADMIN_SECRET
|
||||||
}
|
}
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -26,6 +26,13 @@ func main() {
|
|||||||
back.POST("/submitAddon", controllers.CheckAllowed, controllers.SaveIndexingResult)
|
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)
|
r.Run(initializers.IP + ":" + initializers.PORT)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,16 @@ type CustomEmbed struct {
|
|||||||
Timestamp time.Time `json:"timestamp,omitempty"`
|
Timestamp time.Time `json:"timestamp,omitempty"`
|
||||||
Color int `json:"color,omitempty"`
|
Color int `json:"color,omitempty"`
|
||||||
Image CustomImage `json:"image,omitempty"` // Your new field
|
Image CustomImage `json:"image,omitempty"` // Your new field
|
||||||
|
Fields []CustomEmbedField `json:"fields,omitempty"`
|
||||||
// You can add other fields like Footer, Author, Fields as needed.
|
// 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.
|
// CustomHook is the top-level structure for the webhook payload.
|
||||||
type CustomHook struct {
|
type CustomHook struct {
|
||||||
Username string `json:"username,omitempty"`
|
Username string `json:"username,omitempty"`
|
||||||
|
|||||||
Reference in New Issue
Block a user