add Chuck Norris jokes

This commit is contained in:
2024-11-07 10:48:49 +01:00
parent b6c546dde1
commit 67967ee7ae

View File

@@ -3,9 +3,12 @@ package main
import (
"bufio"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"os/exec"
"strings"
@@ -28,6 +31,16 @@ Příkazy:
`
)
type ChuckJoke struct {
Categories []interface{} `json:"categories"`
Created_at string `json:"created_at"`
Icon_url string `json:"icon_url"`
Id string `json:"id"`
Update_at string `json:"update_at"`
Url string `json:"url"`
Value string `json:"value"`
}
func sendMsg(conn io.Writer, msg string, receiver string) {
fmt.Fprintln(conn, "PRIVMSG "+receiver+" :"+msg)
}
@@ -88,9 +101,27 @@ func main() {
fmt.Println(string(msg))
sendMultiline(conn, string(msg), receiver)
case "!ftip":
ftip, _ := api.Fetch()
for _, l := range ftip.Joke {
sendMultiline(conn, l, receiver)
if rand.Intn(2) == 0 {
res, err := http.Get("https://api.chucknorris.io/jokes/random")
if err != nil {
fmt.Println(err)
}
var parse ChuckJoke
bodyStr, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
fmt.Println(err)
}
err = json.Unmarshal(bodyStr, &parse)
if err != nil {
fmt.Println(err)
}
sendMultiline(conn, parse.Value, receiver)
} else {
ftip, _ := api.Fetch()
for _, l := range ftip.Joke {
sendMultiline(conn, l, receiver)
}
}
}
}