Skip to content

Commit

Permalink
general fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealToxicDev committed Jun 14, 2022
1 parent d61cf78 commit 6165c2c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
31 changes: 10 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,11 +1017,13 @@ print(req.json())
}

// Record new vote
r, err := col.InsertOne(ctx, bson.M{"botID": vars["bid"], "userID": vars["uid"], "date": time.Now().UnixMilli()})
col = mongoDb.Collection("votes")

r, err := mongoDb.Collection("votes").InsertOne(ctx, bson.M{"botID": vars["bid"], "userID": vars["uid"], "date": time.Now().UnixMilli()})

if err != nil {
// Revert vote
_, err := col.DeleteOne(ctx, bson.M{"_id": r.InsertedID})
_, err := mongoDb.Collection("votes").DeleteOne(ctx, bson.M{"_id": r.InsertedID})
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(internalError))
Expand All @@ -1032,11 +1034,11 @@ print(req.json())
Votes int `bson:"votes"`
}

err = col.FindOne(ctx, bson.M{"botID": vars["bid"]}, options.FindOne().SetProjection(bson.M{"votes": 1})).Decode(&oldVotes)
err = mongoDb.Collection("bots").FindOne(ctx, bson.M{"botID": vars["bid"]}, options.FindOne().SetProjection(bson.M{"votes": 1})).Decode(&oldVotes)

if err != nil {
// Revert vote
_, err := col.DeleteOne(ctx, bson.M{"_id": r.InsertedID})
_, err := mongoDb.Collection("votes").DeleteOne(ctx, bson.M{"_id": r.InsertedID})

log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -1074,23 +1076,10 @@ print(req.json())
})

if err != nil {
log.Error("Webhook error: ", err, ". Refunding vote...")
_, refErr := col.DeleteOne(ctx, bson.M{"_id": r.InsertedID})

if refErr != nil {
log.Error("Error when refunding vote: ", refErr)
}

_, refErr = mongoDb.Collection("bots").UpdateOne(ctx, bson.M{"botID": vars["bid"]}, bson.M{"$inc": bson.M{"votes": -1 * incr}})

if refErr != nil {
log.Error("Error when refunding vote: ", refErr)
}

mongoDb.Collection("notifications").InsertOne(ctx, bson.M{
"userID": vars["uid"],
"url": "https://infinitybots.gg/bots/" + vars["bid"],
"message": "Whoa there! We've failed to notify this bot about this vote. The error was: " + err.Error() + ". We have refunded this vote so you can retry voting for this bot",
"message": "Whoa there! We've failed to notify this bot about this vote. The error was: " + err.Error() + ".",
"type": "error",
})
} else {
Expand Down Expand Up @@ -1466,10 +1455,10 @@ print(req.json())
r.HandleFunc("/cosmog/tasks/{tid}.arceus", getTask)
r.HandleFunc("/cosmog/tasks/{tid}", taskFn)

docs.AddDocs("POST", "/webhook-test", "webhook_test", "Test Webhook", "Sends a test webhook to allow testing your vote system",
[]docs.Paramater{}, []string{"System"}, nil, types.ApiError{}, []string{})
docs.AddDocs("POST", "/webhook-test", "webhook_test", "Test Webhook", "Sends a test webhook to allow testing your vote system. **All fields are mandatory for test bot**",
[]docs.Paramater{}, []string{"System"}, types.WebhookPost{}, types.ApiError{}, []string{})

r.HandleFunc("/webhook-test", rateLimitWrap(3, 3*time.Minute, "webtest", func(w http.ResponseWriter, r *http.Request) {
r.HandleFunc("/webhook-test", rateLimitWrap(7, 3*time.Minute, "webtest", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(methodNotAllowed))
Expand Down
2 changes: 1 addition & 1 deletion metro.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func addBot(bot *types.Bot) (*mongo.InsertOneResult, error) {
"botName": bot.Username,
"vanity": strings.ToLower(regex.ReplaceAllString(bot.Username, "")),
"note": "Metro-approved",
"date": time.Now(),
"date": time.Now().UnixMilli(),
"prefix": prefix,
"website": bot.Website,
"github": bot.Github,
Expand Down
2 changes: 1 addition & 1 deletion siteinternal.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ func sendWebhook(webhook types.WebhookPost) error {

isDiscordIntegration := isDiscord(url)

if utils.IsNone(&url) || utils.IsNone(&token) {
if !webhook.Test && (utils.IsNone(&url) || utils.IsNone(&token)) {
// Fetch URL from mongoDB
col := mongoDb.Collection("bots")

Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Bot struct {
Claimed bool `bson:"claimed,omitempty" json:"claimed"`
ClaimedBy string `bson:"claimedBy,omitempty" json:"claimed_by"`
Note string `bson:"note,omitempty" json:"approval_note"`
Date any `bson:"date,omitempty" json:"date"`
}

type AllBots struct {
Expand Down
7 changes: 7 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ func GetVoteData(ctx context.Context, mongoDb *mongo.Database, userID, botID str
VoteTime: GetVoteTime(),
}

log.WithFields(log.Fields{
"uid": userID,
"bid": botID,
"votes": votes,
"err": err,
}).Info("Got vote info")

voteParsed.Timestamps = votes

// In most cases, will be one but not always
Expand Down

0 comments on commit 6165c2c

Please sign in to comment.