added dadjokes

This commit is contained in:
2024-02-20 11:05:50 +01:00
parent 4b4dcff397
commit 0b1de4432b
2 changed files with 55 additions and 1 deletions

53
command_dadjoke.go Normal file
View File

@@ -0,0 +1,53 @@
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"github.com/bwmarrin/discordgo"
)
var dadjoke_command Command = Command{
Definition: discordgo.ApplicationCommand{
Name: "dadjoke",
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,
},
})
},
}

View File

@@ -19,6 +19,7 @@ func ready(s *discordgo.Session, event *discordgo.Ready) {
commands := []*discordgo.ApplicationCommand{
&tag_command.Definition,
&short_get_tag_command.Definition,
&dadjoke_command.Definition,
}
for _, guild := range event.Guilds {
@@ -33,7 +34,7 @@ func ready(s *discordgo.Session, event *discordgo.Ready) {
}
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
var commands []Command = []Command{tag_command, short_get_tag_command}
var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command}
for _, command := range commands {
switch i.Type {
case discordgo.InteractionApplicationCommand: