first commit
This commit is contained in:
139
commands/delete.go
Normal file
139
commands/delete.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func newDeleteCommand() *Cmd {
|
||||
return &Cmd{
|
||||
Command: &discordgo.ApplicationCommand{
|
||||
Name: "delete",
|
||||
Description: "Delete a Thoughtful idea",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "idea",
|
||||
Description: "The idea to delete (search by title)",
|
||||
Required: true,
|
||||
Autocomplete: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
Handler: handleDelete,
|
||||
AutocompleteHandler: handleDeleteAutocomplete,
|
||||
}
|
||||
}
|
||||
|
||||
func handleDelete(s *discordgo.Session, ic *discordgo.InteractionCreate) {
|
||||
user := ic.User
|
||||
if user == nil {
|
||||
user = ic.Member.User
|
||||
}
|
||||
cfg, ok, err := GetUserConfig(user.ID)
|
||||
if err != nil {
|
||||
respondError(s, ic, "Error retrieving configuration.")
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
respondError(s, ic, "You are not logged in. Use `/setup` first.")
|
||||
return
|
||||
}
|
||||
|
||||
options := ic.ApplicationCommandData().Options
|
||||
ideaID := options[0].StringValue()
|
||||
|
||||
payload := map[string]string{"id": ideaID}
|
||||
b, _ := json.Marshal(payload)
|
||||
|
||||
req, _ := http.NewRequest("DELETE", cfg.InstanceURL+"/api/ideas/delete", bytes.NewReader(b))
|
||||
req.Header.Set("API-Authentication", cfg.APIKey)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
respondError(s, ic, "Failed to connect: "+err.Error())
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == 401 {
|
||||
respondError(s, ic, "Unauthorized.")
|
||||
return
|
||||
}
|
||||
|
||||
// Assuming 200 is success
|
||||
if resp.StatusCode == 200 {
|
||||
s.InteractionRespond(ic.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Idea deleted successfully.",
|
||||
Flags: discordgo.MessageFlagsEphemeral,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
respondError(s, ic, fmt.Sprintf("Failed to delete. Status: %d", resp.StatusCode))
|
||||
}
|
||||
}
|
||||
|
||||
func handleDeleteAutocomplete(s *discordgo.Session, ic *discordgo.InteractionCreate) {
|
||||
user := ic.User
|
||||
if user == nil {
|
||||
user = ic.Member.User
|
||||
}
|
||||
cfg, ok, err := GetUserConfig(user.ID)
|
||||
if err != nil || !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch ideas
|
||||
client := &http.Client{}
|
||||
req, _ := http.NewRequest("GET", cfg.InstanceURL+"/api/ideas/list", nil)
|
||||
req.Header.Set("API-Authentication", cfg.APIKey)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result ideaListResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
query := ""
|
||||
data := ic.ApplicationCommandData()
|
||||
for _, opt := range data.Options {
|
||||
if opt.Name == "idea" && opt.Focused {
|
||||
query = strings.ToLower(opt.StringValue())
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
choices := []*discordgo.ApplicationCommandOptionChoice{}
|
||||
for _, idea := range result.Ideas {
|
||||
if query == "" || strings.Contains(strings.ToLower(idea.Title), query) {
|
||||
choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
|
||||
Name: idea.Title,
|
||||
Value: idea.ID,
|
||||
})
|
||||
if len(choices) >= 25 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s.InteractionRespond(ic.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionApplicationCommandAutocompleteResult,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Choices: choices,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user