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

34
cmd_ask.go Normal file
View File

@@ -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),
},
},
},
},
})
},
}

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),
},
})
},

View File

@@ -4,5 +4,5 @@ var color map[string]string = map[string]string{
"red": "#FF6B6B",
"yellow": "#FFD93D",
"green": "#6BCB77",
"blue": "#4D96FF",
"primary": "#4D96FF",
}

33
gather_apis.go Normal file
View File

@@ -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]
}

View File

@@ -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 {