From 955a2648dd3246c1f8efaf94ebf5ccc6c5e110a2 Mon Sep 17 00:00:00 2001 From: William Perron Date: Thu, 15 Sep 2022 14:17:24 +0000 Subject: [PATCH] Add flush command Fixes #8 --- cmd/themis-server/main.go | 73 +++++++++++++++++++++++++++++++++++++-- store.go | 8 +++++ store_test.go | 16 +++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/cmd/themis-server/main.go b/cmd/themis-server/main.go index dcf2dc0..1d76311 100644 --- a/cmd/themis-server/main.go +++ b/cmd/themis-server/main.go @@ -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,8 +415,45 @@ 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) { - if h, ok := handlers[i.ApplicationCommandData().Name]; ok { - h(s, i) + 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 + } } }) } diff --git a/store.go b/store.go index 1f88bb6..92ce02a 100644 --- a/store.go +++ b/store.go @@ -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 +} diff --git a/store_test.go b/store_test.go index cf255c0..492a645 100644 --- a/store_test.go +++ b/store_test.go @@ -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)) +}