added dadjokes
This commit is contained in:
53
command_dadjoke.go
Normal file
53
command_dadjoke.go
Normal 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,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
@@ -19,6 +19,7 @@ func ready(s *discordgo.Session, event *discordgo.Ready) {
|
|||||||
commands := []*discordgo.ApplicationCommand{
|
commands := []*discordgo.ApplicationCommand{
|
||||||
&tag_command.Definition,
|
&tag_command.Definition,
|
||||||
&short_get_tag_command.Definition,
|
&short_get_tag_command.Definition,
|
||||||
|
&dadjoke_command.Definition,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, guild := range event.Guilds {
|
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) {
|
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 {
|
for _, command := range commands {
|
||||||
switch i.Type {
|
switch i.Type {
|
||||||
case discordgo.InteractionApplicationCommand:
|
case discordgo.InteractionApplicationCommand:
|
||||||
|
Reference in New Issue
Block a user