Skip to content

Commit

Permalink
todo improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
evandeters committed Sep 24, 2024
1 parent 6e7f8d3 commit f5f329d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
3 changes: 0 additions & 3 deletions db.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"fmt"

"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
33 changes: 32 additions & 1 deletion todo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
})
}
Expand Down Expand Up @@ -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},
},
})
}

0 comments on commit f5f329d

Please sign in to comment.