This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/mods/main.go

110 lines
2.4 KiB
Go

package main
import (
"flag"
"log"
"math/rand"
"strings"
"time"
"github.com/hamcha/clessy/tg"
)
type Mod struct {
OnInit func()
OnMessage func(*tg.Broker, tg.APIMessage)
}
var mods = map[string]Mod{
"metafora": {
OnMessage: metafora,
},
"viaggi": {
OnInit: initviaggi,
OnMessage: viaggi,
},
"meme": {
OnInit: initmeme,
OnMessage: memegen,
},
"unsplash": {
OnInit: initunsplash,
OnMessage: unsplash,
},
"macro": {
OnInit: initmacro,
OnMessage: macro,
},
"snapchat": {
OnInit: initsnapchat,
OnMessage: snapchat,
},
}
func initmods() {
var enabledmods []string
for name, mod := range mods {
log.Printf("Initializing %s..", name)
if mod.OnInit != nil {
mod.OnInit()
}
enabledmods = append(enabledmods, name)
}
log.Println("===============================")
log.Println("All mods have been initialized!")
log.Println("===============================")
log.Println("Active modules: " + strings.Join(enabledmods, ", "))
}
func dispatch(broker *tg.Broker, update tg.APIMessage) {
for _, mod := range mods {
if mod.OnMessage != nil {
mod.OnMessage(broker, update)
}
}
}
func isCommand(update tg.APIMessage, cmdname string) bool {
if update.Text == nil {
return false
}
text := *(update.Text)
return strings.HasPrefix(text, "/"+cmdname+"@"+*botname) || (strings.HasPrefix(text, "/"+cmdname) && !strings.Contains(text, "@"))
}
var botname *string
var impact *string
var gillmt *string
var sourcesans *string
func main() {
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
botname = flag.String("botname", "maudbot", "Bot name for /targetet@commands")
impact = flag.String("impact", "impact.ttf", "Path to impact.ttf (Impact font)")
gillmt = flag.String("gillmt", "gill.ttf", "Path to gill.ttf (Gill Sans MT font)")
sourcesans = flag.String("sourcesans", "source.ttf", "Path to source.ttf (Source Sans Pro font)")
macropath = flag.String("macropath", "macros.json", "Path to macros db (JSON)")
disable := flag.String("disable", "", "Disable mods (separated by comma)")
flag.Parse()
for _, modname := range strings.Split(*disable, ",") {
modname = strings.TrimSpace(modname)
delete(mods, modname)
}
rand.Seed(time.Now().Unix())
initmods()
err := tg.CreateBrokerClient(*brokerAddr, dispatch)
if err != nil {
panic(err)
}
}
func assert(err error) {
if err != nil {
panic(err)
}
}