Skip to content

Commit 37ad33d

Browse files
committed
Add command to give user profile badge
1 parent 9385bcb commit 37ad33d

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

cmd/console/cmd.go

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func init() {
5656
RootCmd.AddCommand(commands.UpdateStripePriceId)
5757
RootCmd.AddCommand(commands.ClanRecalculateCommand)
5858
RootCmd.AddCommand(commands.RemoveUnrankedClanScores)
59+
RootCmd.AddCommand(commands.BadgePlayerGiveCmd)
5960

6061
// Migrations
6162
RootCmd.AddCommand(migrations.MigrationPlaylistMapsetCmd)
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package commands
2+
3+
import (
4+
"github.com/Quaver/api2/db"
5+
"github.com/sirupsen/logrus"
6+
"github.com/spf13/cobra"
7+
"strconv"
8+
)
9+
10+
var BadgePlayerGiveCmd = &cobra.Command{
11+
Use: "badge:player:give",
12+
Short: "Gives a player a badge",
13+
Run: func(cmd *cobra.Command, args []string) {
14+
if len(args) < 2 {
15+
logrus.Error("You must provide a badge id and a player id")
16+
return
17+
}
18+
19+
badgeId, err := strconv.Atoi(args[0])
20+
21+
if err != nil {
22+
logrus.Error(err)
23+
return
24+
}
25+
26+
playerId, err := strconv.Atoi(args[1])
27+
28+
if err != nil {
29+
logrus.Error(err)
30+
return
31+
}
32+
33+
_, err = db.GetUserById(playerId)
34+
35+
if err != nil {
36+
logrus.Error("Error retrieving player: ", err)
37+
return
38+
}
39+
40+
hasBadge, err := db.UserHasBadge(playerId, badgeId)
41+
42+
if err != nil {
43+
logrus.Error("Error checking user has badge: ", err)
44+
return
45+
}
46+
47+
if hasBadge {
48+
logrus.Info("User already has badge")
49+
return
50+
}
51+
52+
badge := &db.UserBadge{
53+
UserId: playerId,
54+
BadgeId: badgeId,
55+
}
56+
57+
if err := badge.Insert(); err != nil {
58+
logrus.Error("Error inserting badge: ", err)
59+
return
60+
}
61+
62+
logrus.Info("Done!")
63+
},
64+
}

0 commit comments

Comments
 (0)