-
Notifications
You must be signed in to change notification settings - Fork 4
/
vote.go
39 lines (33 loc) · 1.09 KB
/
vote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package polls
import (
"github.com/bwmarrin/discordgo"
"github.com/lordralex/absol/api/database"
"github.com/lordralex/absol/api/logger"
"gorm.io/gorm/clause"
"strings"
)
func runVoteCast(ds *discordgo.Session, i *discordgo.InteractionCreate) {
db, err := database.Get()
if err != nil {
logger.Err().Println(err.Error())
msg := "Vote failed to be cast..."
_, _ = ds.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg})
return
}
vote := &Vote{
MessageId: i.Message.ID,
UserId: i.Member.User.ID,
Vote: strings.TrimPrefix(i.MessageComponentData().CustomID, "vote:"),
}
err = db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "message_id"}, {Name: "user_id"}},
DoUpdates: clause.AssignmentColumns([]string{"vote"}),
}).Create(&vote).Error
if err != nil {
logger.Err().Println(err.Error())
msg := "Vote failed to be cast..."
_, _ = ds.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg})
}
msg := "Vote cast!"
_, _ = ds.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{Content: &msg})
}