added ask command

This commit is contained in:
2024-02-20 22:06:00 +01:00
parent 8e0d8480ae
commit f898010bcb
5 changed files with 73 additions and 39 deletions

View File

@@ -1,11 +1,6 @@
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"github.com/bwmarrin/discordgo"
)
@@ -15,38 +10,10 @@ var dadjoke_command Command = Command{
Description: "Gives you a random joke that is as bad as your dad would tell them",
},
Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://icanhazdadjoke.com/", nil)
if err != nil {
log.Println("Error creating request:", err)
return
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Println("Error making request:", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Error reading response body:", err)
return
}
type Joke struct {
Joke string `json:"joke"`
}
var joke Joke
err = json.Unmarshal(body, &joke)
if err != nil {
log.Println("Error decoding JSON:", err)
return
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: joke.Joke,
Content: simpleGetFromAPI("joke", "https://icanhazdadjoke.com/").(string),
},
})
},