mirror of
https://gitlab.com/prokop_paruzek/botik.git
synced 2025-12-19 06:17:56 +01:00
add Chuck Norris jokes
This commit is contained in:
37
irc_bot.go
37
irc_bot.go
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user