clessy-ng/modules/proverbio/mod.go

64 lines
1.5 KiB
Go
Raw Normal View History

2022-03-24 15:28:00 +00:00
package proverbio
import (
_ "embed"
"fmt"
"log"
"math/rand"
"strings"
2022-03-25 19:06:13 +00:00
"git.fromouter.space/crunchy-rocks/clessy-ng/modules"
2022-03-24 15:28:00 +00:00
"git.fromouter.space/crunchy-rocks/clessy-ng/utils"
2022-03-25 19:06:13 +00:00
2022-03-24 15:28:00 +00:00
"git.fromouter.space/hamcha/tg"
)
//go:embed proverbi.txt
var proverbidata string
var proverbipairs struct {
start []string
end []string
}
type Module struct {
client *tg.Telegram
name string
}
2022-03-25 19:06:13 +00:00
func (m *Module) Initialize(options modules.ModuleOptions) error {
m.client = options.API
m.name = options.Name
2022-03-24 15:28:00 +00:00
lines := strings.Split(proverbidata, "\n")
for i, line := range lines {
pair := strings.SplitN(line, "/", 2)
if len(pair) < 2 {
log.Printf("[proverbio] Found line without separator (#%d), skipping\n", i)
continue
}
proverbipairs.start = append(proverbipairs.start, strings.TrimSpace(pair[0]))
proverbipairs.end = append(proverbipairs.end, strings.TrimSpace(pair[1]))
}
log.Printf("[proverbio] Loaded %d pairs (%d combinations!)\n", len(proverbipairs.start), len(proverbipairs.start)*len(proverbipairs.end))
return nil
}
func (m *Module) OnUpdate(update tg.APIUpdate) {
// Not a message? Ignore
if update.Message == nil {
return
}
2022-03-27 20:53:50 +00:00
if utils.IsCommand(*update.Message, m.name, "proverbio") {
2022-03-24 15:28:00 +00:00
start := rand.Intn(len(proverbipairs.start))
end := rand.Intn(len(proverbipairs.end))
m.client.SendTextMessage(tg.ClientTextMessageData{
ChatID: update.Message.Chat.ChatID,
Text: fmt.Sprintf("<b>Dice il saggio:</b>\n%s %s", proverbipairs.start[start], proverbipairs.end[end]),
})
return
}
}