Add kek command
This commit is contained in:
parent
e2a090b702
commit
d05a04ff4b
51
commands/kek.go
Normal file
51
commands/kek.go
Normal file
@ -0,0 +1,51 @@
|
||||
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{})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user