Skip to content

Commit

Permalink
Merge pull request #132 from Quaver/crash-log-v2
Browse files Browse the repository at this point in the history
Add crash log webhook
  • Loading branch information
Swan authored Nov 26, 2024
2 parents 71e5f14 + 824739b commit d73a3c3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"team_announce_webhook": "",
"clans_first_place_webhook": "",
"clans_map_ranked_webhook": "",
"crash_log_webhook": "",
"stripe": {
"api_key": "",
"webhook_signing_secret": "",
Expand Down
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package config
import (
"encoding/json"
"errors"
"github.com/sirupsen/logrus"
"os"

"github.com/sirupsen/logrus"
)

type Config struct {
Expand Down Expand Up @@ -77,6 +78,7 @@ type Config struct {
TeamAnnounceWebhook string `json:"team_announce_webhook"`
ClansFirstPlaceWebhook string `json:"clans_first_place_webhook"`
ClansMapRankedWebhook string `json:"clans_map_ranked_webhook"`
CrashLogWebhook string `json:"crash_log_webhook"`

Stripe struct {
APIKey string `json:"api_key"`
Expand Down
11 changes: 9 additions & 2 deletions handlers/crash_logs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package handlers

import (
"github.com/Quaver/api2/db"
"github.com/gin-gonic/gin"
"net/http"
"time"

"github.com/Quaver/api2/db"
"github.com/Quaver/api2/webhooks"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

// AddCrashLog Adds a new crash log into the database
Expand Down Expand Up @@ -36,6 +39,10 @@ func AddCrashLog(c *gin.Context) *APIError {
return APIErrorServerError("Error inserting crash log", err)
}

if err := webhooks.SendCrashLogWebhook(user, crashLog); err != nil {
logrus.Error("Error sending crash log webhook: ", err)
}

c.JSON(http.StatusOK, gin.H{"message": "Your crash log has been successfully submitted."})
return nil
}
39 changes: 36 additions & 3 deletions webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package webhooks

import (
"fmt"
"slices"
"strings"
"time"

"github.com/Quaver/api2/config"
"github.com/Quaver/api2/db"
"github.com/Quaver/api2/enums"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/webhook"
"github.com/sirupsen/logrus"
"slices"
"strings"
"time"
)

var (
Expand All @@ -20,6 +21,7 @@ var (
teamAnnounce webhook.Client
clansFirstPlace webhook.Client
clansMapRanked webhook.Client
crashLog webhook.Client
)

const (
Expand All @@ -33,6 +35,7 @@ func InitializeWebhooks() {
teamAnnounce, _ = webhook.NewWithURL(config.Instance.TeamAnnounceWebhook)
clansFirstPlace, _ = webhook.NewWithURL(config.Instance.ClansFirstPlaceWebhook)
clansMapRanked, _ = webhook.NewWithURL(config.Instance.ClansMapRankedWebhook)
crashLog, _ = webhook.NewWithURL(config.Instance.CrashLogWebhook)
}

// SendQueueSubmitWebhook Sends a webhook displaying that the user submitted a mapset to the ranking queue
Expand Down Expand Up @@ -418,6 +421,36 @@ func SendClanRankedWebhook(mapQua *db.MapQua) error {
return nil
}

func SendCrashLogWebhook(user *db.User, log *db.CrashLog) error {
if crashLog == nil {
return nil
}

embed := discord.NewEmbedBuilder().
SetAuthor(user.Username, fmt.Sprintf("https://quavergame.com/user/%v", user.Id), *user.AvatarUrl).
SetTitle("❌ Crash Log Submitted").
SetDescription("A new crash log has been submitted.").
AddField("Actions", fmt.Sprintf("[View Log](https://a.quavergame.com/logs/crash/%v)", log.Id), false).
SetThumbnail(QuaverLogo).
SetFooter("Quaver", QuaverLogo).
SetTimestamp(time.Now()).
SetColor(0xFF0000).
Build()

msg := discord.WebhookMessageCreate{
Embeds: []discord.Embed{embed},
}

_, err := crashLog.CreateMessage(msg)

if err != nil {
logrus.Error("Failed to send crash log event webhook: ", err)
return err
}

return nil
}

func getUserPingText(mapset *db.Mapset) string {
content := ""

Expand Down

0 comments on commit d73a3c3

Please sign in to comment.