webhook fixes

This commit is contained in:
Dot-Rar 2020-03-05 16:40:18 +00:00
parent 44756c0517
commit 2280c2913f

View File

@ -1,13 +1,14 @@
package manage package manage
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
"github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/config"
"github.com/TicketsBot/GoPanel/database/table" "github.com/TicketsBot/GoPanel/database/table"
"github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/GoPanel/utils"
"github.com/TicketsBot/GoPanel/utils/discord" "github.com/TicketsBot/GoPanel/utils/discord"
"github.com/TicketsBot/GoPanel/utils/discord/endpoints/channel" "github.com/TicketsBot/GoPanel/utils/discord/endpoints/channel"
"github.com/TicketsBot/GoPanel/utils/discord/endpoints/webhooks"
"github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/TicketsBot/GoPanel/utils/discord/objects"
"github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -15,6 +16,7 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
"time"
) )
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
@ -109,7 +111,8 @@ func WebChatWs(ctx *gin.Context) {
data := evnt.Data.(map[string]interface{}) data := evnt.Data.(map[string]interface{})
guildId = data["guild"].(string) guildId = data["guild"].(string)
ticket, err = strconv.Atoi(data["ticket"].(string)); if err != nil { ticket, err = strconv.Atoi(data["ticket"].(string));
if err != nil {
conn.Close() conn.Close()
} }
@ -175,25 +178,7 @@ func WebChatWs(ctx *gin.Context) {
success := false success := false
if webhook != nil { if webhook != nil {
endpoint := webhooks.ExecuteWebhook(*webhook) success = executeWebhook(content, ticket.Uuid, *webhook, store)
var res *http.Response
err, res = endpoint.Request(store, &contentType, webhooks.ExecuteWebhookBody{
Content: content,
Username: store.Get("name").(string),
AvatarUrl: store.Get("avatar").(string),
AllowedMentions: objects.AllowedMention{
Parse: make([]objects.AllowedMentionType, 0),
Roles: make([]string, 0),
Users: make([]string, 0),
},
}, nil)
if res.StatusCode == 404 || res.StatusCode == 403 {
go table.DeleteWebhookByUuid(ticket.Uuid)
} else {
success = true
}
} }
if !success { if !success {
@ -212,3 +197,34 @@ func WebChatWs(ctx *gin.Context) {
} }
} }
} }
func executeWebhook(uuid, webhook, content string, store sessions.Session) bool {
body := map[string]interface{}{
"content": content,
"username": store.Get("name").(string),
"avatar_url": store.Get("avatar").(string),
}
encoded, err := json.Marshal(&body); if err != nil {
return false
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://discordapp.com/api/v6/webhooks/%s", webhook), bytes.NewBuffer(encoded)); if err != nil {
return false
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
client.Timeout = 3 * time.Second
res, err := client.Do(req); if err != nil {
return false
}
if res.StatusCode == 404 || res.StatusCode == 403 {
go table.DeleteWebhookByUuid(uuid)
} else {
return true
}
return false
}