Skip to content

Commit d73a3c3

Browse files
authored
Merge pull request #132 from Quaver/crash-log-v2
Add crash log webhook
2 parents 71e5f14 + 824739b commit d73a3c3

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

config.example.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"team_announce_webhook": "",
5757
"clans_first_place_webhook": "",
5858
"clans_map_ranked_webhook": "",
59+
"crash_log_webhook": "",
5960
"stripe": {
6061
"api_key": "",
6162
"webhook_signing_secret": "",

config/config.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package config
33
import (
44
"encoding/json"
55
"errors"
6-
"github.com/sirupsen/logrus"
76
"os"
7+
8+
"github.com/sirupsen/logrus"
89
)
910

1011
type Config struct {
@@ -77,6 +78,7 @@ type Config struct {
7778
TeamAnnounceWebhook string `json:"team_announce_webhook"`
7879
ClansFirstPlaceWebhook string `json:"clans_first_place_webhook"`
7980
ClansMapRankedWebhook string `json:"clans_map_ranked_webhook"`
81+
CrashLogWebhook string `json:"crash_log_webhook"`
8082

8183
Stripe struct {
8284
APIKey string `json:"api_key"`

handlers/crash_logs.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package handlers
22

33
import (
4-
"github.com/Quaver/api2/db"
5-
"github.com/gin-gonic/gin"
64
"net/http"
75
"time"
6+
7+
"github.com/Quaver/api2/db"
8+
"github.com/Quaver/api2/webhooks"
9+
"github.com/gin-gonic/gin"
10+
"github.com/sirupsen/logrus"
811
)
912

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

42+
if err := webhooks.SendCrashLogWebhook(user, crashLog); err != nil {
43+
logrus.Error("Error sending crash log webhook: ", err)
44+
}
45+
3946
c.JSON(http.StatusOK, gin.H{"message": "Your crash log has been successfully submitted."})
4047
return nil
4148
}

webhooks/webhooks.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package webhooks
22

33
import (
44
"fmt"
5+
"slices"
6+
"strings"
7+
"time"
8+
59
"github.com/Quaver/api2/config"
610
"github.com/Quaver/api2/db"
711
"github.com/Quaver/api2/enums"
812
"github.com/disgoorg/disgo/discord"
913
"github.com/disgoorg/disgo/webhook"
1014
"github.com/sirupsen/logrus"
11-
"slices"
12-
"strings"
13-
"time"
1415
)
1516

1617
var (
@@ -20,6 +21,7 @@ var (
2021
teamAnnounce webhook.Client
2122
clansFirstPlace webhook.Client
2223
clansMapRanked webhook.Client
24+
crashLog webhook.Client
2325
)
2426

2527
const (
@@ -33,6 +35,7 @@ func InitializeWebhooks() {
3335
teamAnnounce, _ = webhook.NewWithURL(config.Instance.TeamAnnounceWebhook)
3436
clansFirstPlace, _ = webhook.NewWithURL(config.Instance.ClansFirstPlaceWebhook)
3537
clansMapRanked, _ = webhook.NewWithURL(config.Instance.ClansMapRankedWebhook)
38+
crashLog, _ = webhook.NewWithURL(config.Instance.CrashLogWebhook)
3639
}
3740

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

424+
func SendCrashLogWebhook(user *db.User, log *db.CrashLog) error {
425+
if crashLog == nil {
426+
return nil
427+
}
428+
429+
embed := discord.NewEmbedBuilder().
430+
SetAuthor(user.Username, fmt.Sprintf("https://quavergame.com/user/%v", user.Id), *user.AvatarUrl).
431+
SetTitle("❌ Crash Log Submitted").
432+
SetDescription("A new crash log has been submitted.").
433+
AddField("Actions", fmt.Sprintf("[View Log](https://a.quavergame.com/logs/crash/%v)", log.Id), false).
434+
SetThumbnail(QuaverLogo).
435+
SetFooter("Quaver", QuaverLogo).
436+
SetTimestamp(time.Now()).
437+
SetColor(0xFF0000).
438+
Build()
439+
440+
msg := discord.WebhookMessageCreate{
441+
Embeds: []discord.Embed{embed},
442+
}
443+
444+
_, err := crashLog.CreateMessage(msg)
445+
446+
if err != nil {
447+
logrus.Error("Failed to send crash log event webhook: ", err)
448+
return err
449+
}
450+
451+
return nil
452+
}
453+
421454
func getUserPingText(mapset *db.Mapset) string {
422455
content := ""
423456

0 commit comments

Comments
 (0)