golifehk/utils/system.go

94 lines
1.8 KiB
Go

package utils
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"path/filepath"
"strings"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var JSON_SETTINGS string = filepath.Join( WORKDIR, "settings.json" )
var settingsTime = time.Unix( 0, 0 )
type SysSettings struct {
IgnoredChats map[int64] bool `json:"IgnoredChats"`
}
var Settings = SysSettings{ IgnoredChats: map[int64] bool{} }
func rwSettings() {
buff, err := ChangedStream( JSON_SETTINGS, writeSettings, settingsTime )
if err != nil {
log.Panic( err )
return
}
conf := SysSettings{}
err = json.Unmarshal( buff.Bytes(), &conf )
if err != nil {
log.Panic( err )
return
}
Settings = conf
}
func writeSettings() ( io.Reader, error ) {
b, err := json.Marshal( Settings )
if err != nil {
return nil, err
}
return bytes.NewBuffer( b ), nil
}
func SystemControl( tgMesg *tgbotapi.Message ) ( string, bool ) {
rwSettings()
processed := false
mesg := ""
if tgMesg.Text[0] == '/' {
processed = true
}
chatId := tgMesg.Chat.ID
if Settings.IgnoredChats == nil {
Settings.IgnoredChats = map[int64] bool{}
}
if strings.Contains( tgMesg.Text, "/golifehk disable" ) {
mesg = fmt.Sprintf( "OK" )
Settings.IgnoredChats[ chatId ] = true
processed = true
}
if strings.Contains( tgMesg.Text, "/golifehk enable" ) {
mesg = fmt.Sprintf( "OK" )
Settings.IgnoredChats[ chatId ] = false
processed = true
}
if processed {
settingsTime = time.Now()
rwSettings()
}
//// Begin processing settings
// ignore chats if enabled
if Settings.IgnoredChats[ chatId ] {
processed = true
}
return mesg, processed
}