Make sure users can't send multiple vetting requests

This commit is contained in:
Elara 2023-12-05 17:12:05 -08:00
parent dd550b10cf
commit a5933b2a97
2 changed files with 17 additions and 6 deletions

View File

@ -19,11 +19,11 @@
package db package db
func AddVettingReq(guildID, userID, msgID string) error { func AddVettingReq(guildID, userID, msgID string) error {
_, err := db.Exec("INSERT OR ABORT INTO vetting_requests (guild_id, user_id, msg_id) VALUES (?, ?, ?)", guildID, userID, msgID) _, err := db.Exec("INSERT INTO vetting_requests (guild_id, user_id, msg_id) VALUES (?, ?, ?)", guildID, userID, msgID)
return err return err
} }
func VettingReqID(guildID, userID string) (string, error) { func VettingReqMsgID(guildID, userID string) (string, error) {
var out string var out string
row := db.QueryRowx("SELECT msg_id FROM vetting_requests WHERE user_id = ? AND guild_id = ?", userID, guildID) row := db.QueryRowx("SELECT msg_id FROM vetting_requests WHERE user_id = ? AND guild_id = ?", userID, guildID)
err := row.Scan(&out) err := row.Scan(&out)
@ -37,7 +37,7 @@ func VettingReqUserID(guildID, msgID string) (string, error) {
return out, err return out, err
} }
func RemoveVettingReq(guildID, userID string) error { func RemoveVettingReq(guildID, msgID string) error {
_, err := db.Exec("DELETE FROM vetting_requests WHERE user_id = ? AND guild_id = ?", userID, guildID) _, err := db.Exec("DELETE FROM vetting_requests WHERE msg_id = ? AND guild_id = ?", msgID, guildID)
return err return err
} }

View File

@ -137,6 +137,11 @@ func onVettingRequest(s *discordgo.Session, i *discordgo.InteractionCreate) erro
return nil return nil
} }
_, err := db.VettingReqMsgID(i.GuildID, i.Member.User.ID)
if err == nil {
return errors.New("you've already sent a vetting request")
}
guild, err := db.GuildByID(i.GuildID) guild, err := db.GuildByID(i.GuildID)
if err != nil { if err != nil {
return err return err
@ -161,7 +166,7 @@ func onVettingRequest(s *discordgo.Session, i *discordgo.InteractionCreate) erro
eventlog.AddTimeToEmbed(guild.TimeFormat, embed) eventlog.AddTimeToEmbed(guild.TimeFormat, embed)
_, err = s.ChannelMessageSendComplex(guild.VettingReqChanID, &discordgo.MessageSend{ msg, err := s.ChannelMessageSendComplex(guild.VettingReqChanID, &discordgo.MessageSend{
Embeds: []*discordgo.MessageEmbed{embed}, Embeds: []*discordgo.MessageEmbed{embed},
Components: []discordgo.MessageComponent{ Components: []discordgo.MessageComponent{
discordgo.ActionsRow{Components: []discordgo.MessageComponent{ discordgo.ActionsRow{Components: []discordgo.MessageComponent{
@ -184,6 +189,11 @@ func onVettingRequest(s *discordgo.Session, i *discordgo.InteractionCreate) erro
return err return err
} }
err = db.AddVettingReq(i.GuildID, i.Member.User.ID, msg.ID)
if err != nil {
return err
}
return util.RespondEphemeral(s, i.Interaction, "Successfully sent your vetting request!") return util.RespondEphemeral(s, i.Interaction, "Successfully sent your vetting request!")
} }
@ -335,5 +345,6 @@ func onVettingResponse(s *discordgo.Session, i *discordgo.InteractionCreate) err
return err return err
} }
} }
return nil
return db.RemoveVettingReq(i.GuildID, i.Message.ID)
} }