From f898010bcb6ae208f47c19e907d94962f3f84b5b Mon Sep 17 00:00:00 2001 From: vaporvee Date: Tue, 20 Feb 2024 22:06:00 +0100 Subject: [PATCH] added ask command --- cmd_ask.go | 34 ++++++++++++++++++++++++++++++++++ cmd_dadjoke.go | 35 +---------------------------------- config.go | 8 ++++---- gather_apis.go | 33 +++++++++++++++++++++++++++++++++ register_commands.go | 2 +- 5 files changed, 73 insertions(+), 39 deletions(-) create mode 100644 cmd_ask.go create mode 100644 gather_apis.go diff --git a/cmd_ask.go b/cmd_ask.go new file mode 100644 index 0000000..8fc3e62 --- /dev/null +++ b/cmd_ask.go @@ -0,0 +1,34 @@ +package main + +import "github.com/bwmarrin/discordgo" + +var ask_command Command = Command{ + Definition: discordgo.ApplicationCommand{ + Name: "ask", + Description: "Ask anything and get a gif as response!", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "question", + Description: "The question you want to ask", + Required: true, + }, + }, + }, + Interact: func(s *discordgo.Session, i *discordgo.InteractionCreate) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + { + Type: discordgo.EmbedTypeImage, + Color: hexToDecimal(color["primary"]), + Image: &discordgo.MessageEmbedImage{ + URL: simpleGetFromAPI("image", "https://yesno.wtf/api").(string), + }, + }, + }, + }, + }) + }, +} diff --git a/cmd_dadjoke.go b/cmd_dadjoke.go index fe53f72..42b7898 100644 --- a/cmd_dadjoke.go +++ b/cmd_dadjoke.go @@ -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), }, }) }, diff --git a/config.go b/config.go index b9f179a..65d970c 100644 --- a/config.go +++ b/config.go @@ -1,8 +1,8 @@ package main var color map[string]string = map[string]string{ - "red": "#FF6B6B", - "yellow": "#FFD93D", - "green": "#6BCB77", - "blue": "#4D96FF", + "red": "#FF6B6B", + "yellow": "#FFD93D", + "green": "#6BCB77", + "primary": "#4D96FF", } diff --git a/gather_apis.go b/gather_apis.go new file mode 100644 index 0000000..66f861b --- /dev/null +++ b/gather_apis.go @@ -0,0 +1,33 @@ +package main + +import ( + "encoding/json" + "io" + "log" + "net/http" +) + +func simpleGetFromAPI(key string, url string) interface{} { + client := &http.Client{} + req, err := http.NewRequest("GET", url, nil) + if err != nil { + log.Println("Error creating request:", err) + } + req.Header.Set("Accept", "application/json") + resp, err := client.Do(req) + if err != nil { + log.Println("Error making request:", err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Println("Error reading response body:", err) + } + + var result map[string]interface{} + err = json.Unmarshal(body, &result) + if err != nil { + log.Println("Error decoding JSON:", err) + } + return result[key] +} diff --git a/register_commands.go b/register_commands.go index 4f5f189..615f2a5 100644 --- a/register_commands.go +++ b/register_commands.go @@ -15,7 +15,7 @@ type Command struct { ModalID string } -var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command} +var commands []Command = []Command{tag_command, short_get_tag_command, dadjoke_command, ping_command, ask_command} func ready(s *discordgo.Session, event *discordgo.Ready) { for _, guild := range event.Guilds {