2022-03-24 15:28:00 +00:00
|
|
|
package macro
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2022-03-25 19:06:13 +00:00
|
|
|
"github.com/cockroachdb/pebble"
|
2022-03-24 15:28:00 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Macro struct {
|
|
|
|
Value string
|
|
|
|
Author tg.APIUser
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
var macros map[string]Macro
|
|
|
|
|
2022-03-25 19:06:13 +00:00
|
|
|
const macroKey = "mod/macros/data"
|
|
|
|
|
2022-03-24 15:28:00 +00:00
|
|
|
type Module struct {
|
|
|
|
client *tg.Telegram
|
|
|
|
name string
|
2022-03-25 19:06:13 +00:00
|
|
|
kv *pebble.DB
|
2022-03-24 15:28:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 19:06:13 +00:00
|
|
|
func (m *Module) Initialize(options modules.ModuleOptions) error {
|
|
|
|
m.client = options.API
|
|
|
|
m.name = options.Name
|
|
|
|
m.kv = options.KV
|
2022-03-24 15:28:00 +00:00
|
|
|
|
|
|
|
macros = make(map[string]Macro)
|
2022-03-25 19:06:13 +00:00
|
|
|
err := utils.ReadJSON(m.kv, macroKey, ¯os)
|
2022-03-24 15:28:00 +00:00
|
|
|
if err != nil {
|
2022-03-25 19:06:13 +00:00
|
|
|
if !errors.Is(err, pebble.ErrNotFound) {
|
|
|
|
log.Println("[macro] WARN: Could not load macros (db error): " + err.Error())
|
2022-03-24 15:28:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 19:06:13 +00:00
|
|
|
log.Printf("[macro] Loaded %d macros\n", len(macros))
|
2022-03-24 15:28:00 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Module) OnUpdate(update tg.APIUpdate) {
|
2022-11-28 10:06:15 +00:00
|
|
|
if !utils.IsCommand(update, m.name, "macro") {
|
2022-03-24 15:28:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-28 10:06:15 +00:00
|
|
|
parts := strings.SplitN(*(update.Message.Text), " ", 3)
|
|
|
|
switch len(parts) {
|
|
|
|
case 2:
|
|
|
|
name := strings.ToLower(parts[1])
|
|
|
|
item, ok := macros[name]
|
|
|
|
var out string
|
|
|
|
if ok {
|
|
|
|
out = fmt.Sprintf("<b>%s</b>\n%s\n<i>%s - %s</i>", name, item.Value, item.Author.Username, item.Time.Format("02-01-06 15:04"))
|
|
|
|
} else {
|
|
|
|
out = fmt.Sprintf("<b>%s</b>\n<i>macro inesistente</i>\n(configura con /macro %s <i>contenuto</i>)", name, name)
|
2022-03-24 15:28:00 +00:00
|
|
|
}
|
2022-11-28 10:06:15 +00:00
|
|
|
m.client.SendTextMessage(tg.ClientTextMessageData{
|
|
|
|
ChatID: update.Message.Chat.ChatID,
|
|
|
|
Text: out,
|
|
|
|
ReplyID: &update.Message.MessageID,
|
|
|
|
})
|
|
|
|
case 3:
|
|
|
|
name := strings.ToLower(parts[1])
|
|
|
|
macros[name] = Macro{
|
|
|
|
Value: parts[2],
|
|
|
|
Author: update.Message.User,
|
|
|
|
Time: time.Now(),
|
|
|
|
}
|
|
|
|
m.save()
|
|
|
|
m.client.SendTextMessage(tg.ClientTextMessageData{
|
|
|
|
ChatID: update.Message.Chat.ChatID,
|
|
|
|
Text: fmt.Sprintf("<b>%s</b> → %s", name, parts[2]),
|
|
|
|
ReplyID: &update.Message.MessageID,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
m.client.SendTextMessage(tg.ClientTextMessageData{
|
|
|
|
ChatID: update.Message.Chat.ChatID,
|
|
|
|
Text: "<b>Sintassi</b>\n<b>Leggi</b>: /macro <i>nome-macro</i>\n<b>Scrivi</b>: /macro <i>nome-macro</i> <i>contenuto macro</i>",
|
|
|
|
ReplyID: &update.Message.MessageID,
|
|
|
|
})
|
2022-03-24 15:28:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 19:06:13 +00:00
|
|
|
func (m *Module) save() {
|
|
|
|
byt, err := jsoniter.ConfigFastest.Marshal(macros)
|
2022-03-24 15:28:00 +00:00
|
|
|
if err != nil {
|
2022-03-25 19:06:13 +00:00
|
|
|
log.Println("[macro] WARN: Could not encode macros: " + err.Error())
|
2022-03-24 15:28:00 +00:00
|
|
|
}
|
2022-03-25 19:06:13 +00:00
|
|
|
err = m.kv.Set([]byte(macroKey), byt, &pebble.WriteOptions{Sync: true})
|
2022-03-24 15:28:00 +00:00
|
|
|
if err != nil {
|
2022-03-25 19:06:13 +00:00
|
|
|
log.Println("[macro] WARN: Could not save macros to db: " + err.Error())
|
|
|
|
return
|
2022-03-24 15:28:00 +00:00
|
|
|
}
|
|
|
|
}
|