Add flush command

Fixes #8
absences
William Perron 2 years ago
parent 9e63f05ebc
commit 955a2648dd

@ -128,6 +128,11 @@ func main() {
},
},
},
{
Name: "flush",
Description: "Remove all claims from the database and prepare for the next game!",
Type: discordgo.ChatApplicationCommand,
},
}
handlers := map[string]Handler{
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
@ -320,6 +325,33 @@ func main() {
log.Println("[error] failed to respond to command:", err)
}
},
"flush": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseModal,
Data: &discordgo.InteractionResponseData{
CustomID: "modals_flush_" + i.Interaction.Member.User.ID,
Title: "Are you sure?",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: "confirmation",
Label: "Delete all claims permanently? [y/N]",
Style: discordgo.TextInputShort,
Placeholder: "",
Value: "",
Required: true,
MinLength: 1,
MaxLength: 45,
},
},
},
},
},
}); err != nil {
log.Println("failed to respond to command:", err)
}
},
}
registerHandlers(discord, handlers)
@ -383,9 +415,46 @@ func registerHandlers(sess *discordgo.Session, handlers map[string]Handler) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
sess.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if h, ok := handlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
case discordgo.InteractionModalSubmit:
if strings.HasPrefix(i.ModalSubmitData().CustomID, "modals_flush_") {
sub := i.ModalSubmitData().Components[0].(*discordgo.ActionsRow).Components[0].(*discordgo.TextInput).Value
sub = strings.ToLower(sub)
if sub == "y" || sub == "ye" || sub == "yes" {
err := store.Flush(context.Background())
msg := "Flushed all claims!"
if err != nil {
msg = "failed to flush claims from database"
}
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
},
})
if err != nil {
log.Println("[error] failed to respond to command:", err)
}
return
}
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Aborted...",
},
})
if err != nil {
log.Println("[error] failed to respond to command:", err)
}
return
}
}
})
}

@ -312,3 +312,11 @@ func (s *Store) DeleteClaim(ctx context.Context, ID int, userId string) error {
}
return nil
}
func (s *Store) Flush(ctx context.Context) error {
_, err := s.db.ExecContext(ctx, "DELETE FROM claims;")
if err != nil {
return fmt.Errorf("failed to execute delete query: %w", err)
}
return nil
}

@ -164,3 +164,19 @@ func TestDescribeClaim(t *testing.T) {
detail, err = store.DescribeClaim(context.TODO(), 9999)
assert.ErrorIs(t, err, ErrNoSuchClaim)
}
func TestFlush(t *testing.T) {
store, err := NewStore(TEST_CONN_STRING)
assert.NoError(t, err)
store.Claim(context.TODO(), "000000000000000001", "foo", "Genoa", CLAIM_TYPE_TRADE)
store.Claim(context.TODO(), "000000000000000001", "foo", "Valencia", CLAIM_TYPE_TRADE)
store.Claim(context.TODO(), "000000000000000001", "foo", "Italy", CLAIM_TYPE_REGION)
store.Claim(context.TODO(), "000000000000000001", "foo", "Iberia", CLAIM_TYPE_REGION)
store.Claim(context.TODO(), "000000000000000001", "foo", "Ragusa", CLAIM_TYPE_TRADE)
assert.NoError(t, store.Flush(context.TODO()))
claims, err := store.ListClaims(context.TODO())
assert.NoError(t, err)
assert.Equal(t, 0, len(claims))
}

Loading…
Cancel
Save