diff --git a/db.go b/db.go index ea81104..213f154 100644 --- a/db.go +++ b/db.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -19,7 +17,6 @@ func ConnectDB() *gorm.DB { func addTodo(user, content string) error { newTodo := todo{User: user, Task: content, Completed: false} - fmt.Println("Adding todo: ", newTodo) result := db.Create(&newTodo) if result.Error != nil { return result.Error diff --git a/handlers.go b/handlers.go index 3739cd5..dda7141 100644 --- a/handlers.go +++ b/handlers.go @@ -40,12 +40,17 @@ func TodoHandler(s *discordgo.Session, i *discordgo.InteractionCreate) { addTodoResponse(s, i, err) case "get": if len(i.ApplicationCommandData().Options[0].Options) == 0 { - s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ - Type: discordgo.InteractionResponseChannelMessageWithSource, - Data: &discordgo.InteractionResponseData{ - Content: "Please provide a person", - }, - }) + allTodos, err := getAllTodos() + if err != nil { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Failed to get todos: " + err.Error(), + }, + }) + return + } + getAllTodoResponse(s, i, allTodos, err) return } user := i.ApplicationCommandData().Options[0].Options[0].StringValue() diff --git a/todo.go b/todo.go index 3adae8c..7d18201 100644 --- a/todo.go +++ b/todo.go @@ -50,10 +50,16 @@ func addTodoResponse(s *discordgo.Session, i *discordgo.InteractionCreate, err e return } + embed := embed.NewEmbed() + embed.SetTitle("Todo Added") + embed.SetColor(0xffab40) + embed.AddField("Person", i.ApplicationCommandData().Options[0].Options[0].StringValue()) + embed.AddField("Task", i.ApplicationCommandData().Options[0].Options[1].StringValue()) + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: "Todo added!", + Embeds: []*discordgo.MessageEmbed{embed.MessageEmbed}, }, }) } @@ -82,3 +88,28 @@ func getTodoResponse(s *discordgo.Session, i *discordgo.InteractionCreate, todos }, }) } + +func getAllTodoResponse(s *discordgo.Session, i *discordgo.InteractionCreate, todos []todo, err error) { + if err != nil { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Failed to get todos: " + err.Error(), + }, + }) + return + } + + embed := embed.NewEmbed() + embed.SetTitle("Todos") + embed.SetColor(0xffab40) + for _, t := range todos { + embed.AddField(fmt.Sprintf("Id: %d", t.Id), fmt.Sprintf("Person: %s\n Task: %s\n Completed: %t", t.User, t.Task, t.Completed)) + } + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{embed.MessageEmbed}, + }, + }) +}