From 0b1de4432b2f58670285c78d45b4cdfc9fae10a2 Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 11:05:50 +0100 Subject: [PATCH] added dadjokes --- command_dadjoke.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ register_commands.go | 3 ++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 command_dadjoke.go diff --git a/command_dadjoke.go b/command_dadjoke.go new file mode 100644 index 0000000..fe53f72 --- /dev/null +++ b/command_dadjoke.go @@ -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, + }, + }) + }, +} diff --git a/register_commands.go b/register_commands.go index 2ce1db5..128801a 100644 --- a/register_commands.go +++ b/register_commands.go @@ -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: