diff --git a/mods/proverbio.go b/mods/proverbio.go new file mode 100644 index 0000000..c027276 --- /dev/null +++ b/mods/proverbio.go @@ -0,0 +1,46 @@ +package main + +import ( + "math/rand" + "strings" + + "io/ioutil" + + "log" + + "github.com/hamcha/clessy/tg" +) + +type ProverbioData struct { + Inizio []string + Fine []string +} + +var proverbipairs ProverbioData + +func initproverbio() { + data, err := ioutil.ReadFile(*proverbi) + assert(err) + + lines := strings.Split(string(data), "\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.Inizio = append(proverbipairs.Inizio, strings.TrimSpace(pair[0])) + proverbipairs.Fine = append(proverbipairs.Fine, strings.TrimSpace(pair[1])) + } + + log.Printf("[proverbio] Loaded %d pairs (%d combinations!)\n", len(proverbipairs.Inizio), len(proverbipairs.Inizio)*len(proverbipairs.Fine)) +} + +func proverbio(broker *tg.Broker, update tg.APIMessage) { + if isCommand(update, "proverbio") { + n := rand.Intn(len(proverbipairs.Inizio)) + m := rand.Intn(len(proverbipairs.Fine)) + broker.SendTextMessage(update.Chat, "Dice il saggio:\n"+proverbipairs.Inizio[n]+" "+proverbipairs.Fine[m], nil) + return + } +}