52 lines
936 B
Go
52 lines
936 B
Go
package commands
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math/rand"
|
|
"os"
|
|
"sync"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
)
|
|
|
|
type KekCommand struct{}
|
|
|
|
var (
|
|
kekJokes []string
|
|
kekJokesOnce sync.Once
|
|
)
|
|
|
|
func loadKekJokes() {
|
|
file, err := os.Open("json/jokes.json")
|
|
if err != nil {
|
|
kekJokes = []string{"Failed to load jokes."}
|
|
return
|
|
}
|
|
defer file.Close()
|
|
json.NewDecoder(file).Decode(&kekJokes)
|
|
}
|
|
|
|
func (k KekCommand) Name() string {
|
|
return "kek"
|
|
}
|
|
|
|
func (k KekCommand) Help() string {
|
|
return "Get niggerkeks"
|
|
}
|
|
|
|
func (k KekCommand) Execute(update tgbotapi.Update, bot *tgbotapi.BotAPI) {
|
|
kekJokesOnce.Do(loadKekJokes)
|
|
if len(kekJokes) == 0 {
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "No jokes available.")
|
|
bot.Send(msg)
|
|
return
|
|
}
|
|
joke := kekJokes[rand.Intn(len(kekJokes))]
|
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, joke)
|
|
bot.Send(msg)
|
|
}
|
|
|
|
func init() {
|
|
Register(KekCommand{})
|
|
}
|