test config

This commit is contained in:
shoopea 2020-01-14 10:34:46 +08:00
parent 6ca15389b1
commit 01e469b4c3
6 changed files with 90 additions and 37 deletions

0
config.go Normal file
View File

View File

@ -0,0 +1,28 @@
{
"telegram": {
"url": "https://api.telegram.org",
"token": ""
},
"sql": {
"driver": "mysql",
"type": "tcp",
"address": "localhost:3306",
"username": "chirpnest",
"password": "chirpnest",
"database": "chirpnest"
},
"rabbit": {
"user": "chirpnest",
"password": "chirpnest",
"host": "localhost:5672",
"path": "chirpnest"
},
"bot": {
"admin_id": 0,
"guild_name": "Guild",
"guild": "GLD",
"main_chat_id": 0,
"deposit_chat_id": 0,
"report_chat_id": 0
}
}

29
def.go
View File

@ -13,6 +13,35 @@ const maxUnixTimestamp int64 = 2147483647
const chtwrsbotID64 int64 = 408101137 const chtwrsbotID64 int64 = 408101137
const angrybirbsbotID64 int64 = 833409972 const angrybirbsbotID64 int64 = 833409972
type Config struct {
Telegram struct {
URL string `json:"url"`
Token string `json:"token"`
} `json:"telegram"`
SQL struct {
Driver string `json:"driver"`
Type string `json:"type"`
Address string `json:"address"`
Username string `json:"username"`
Password string `json:"password"`
Database string `json:"database"`
} `json:"sql"`
Rabbit struct {
User string `json:"user"`
Password string `json:"password"`
Host string `json:"host"`
Path string `json:"path"`
} `json:"rabbit"`
Bot struct {
Admin int64 `json:"admin_id"`
Guildname string `json:"guild_name"`
Guild string `json:"guild"`
Mainchat int64 `json:"main_chat_id"`
Depositchat int64 `json:"deposit_chat_id"`
Reportchat int64 `json:"report_chat_id"`
} `json:"bot"`
}
type DataBackup struct { type DataBackup struct {
Messages []ChatWarsMessage `json:"messages"` Messages []ChatWarsMessage `json:"messages"`
} }

38
main.go
View File

@ -19,37 +19,8 @@ import (
tb "gopkg.in/tucnak/telebot.v2" tb "gopkg.in/tucnak/telebot.v2"
) )
type Config struct {
Telegram struct {
URL string `json:"url"`
Token string `json:"token"`
} `json:"telegram"`
SQL struct {
Driver string `json:"driver"`
Type string `json:"type"`
Address string `json:"address"`
Username string `json:"username"`
Password string `json:"password"`
Database string `json:"database"`
} `json:"sql"`
Rabbit struct {
User string
Password string
Host string
Path string
} `json:"rabbit"`
Bot struct {
Admin int64
Guildname string
Guild string
Mainchat int64
Depositchat int64
Reportchat int64
} `json:"bot"`
}
var ( var (
config = flag.String("config", "chirpnest.cfg", "config file path") config = flag.String("config", "chirpnest.json", "config file path")
initdb = flag.Bool("initdb", false, "initialize bot database") initdb = flag.Bool("initdb", false, "initialize bot database")
db *sql.DB db *sql.DB
@ -80,11 +51,6 @@ var (
gDepositForwardMsg []int64 gDepositForwardMsg []int64
) )
func PrintText(m *tb.Message) {
fmt.Printf("[%d] %s(%d) | %s(%d) : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text)
return
}
func main() { func main() {
log.Println("Starting Chirpnest...") log.Println("Starting Chirpnest...")
@ -94,7 +60,7 @@ func main() {
// randomize // randomize
RndSrc = rand.New(rand.NewSource(time.Now().UTC().UnixNano())) RndSrc = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
err := gcfg.ReadFileInto(&cfg, *config) err := ReadConfig(*config)
failOnError(err, "Parsing config") failOnError(err, "Parsing config")
_, err = Asset("data/code_obj_type.json") _, err = Asset("data/code_obj_type.json")

2
obj.go
View File

@ -55,7 +55,7 @@ func initCache() {
logOnError(err, "initCache : caching user") logOnError(err, "initCache : caching user")
log.Println("Caching items ..") log.Println("Caching items ..")
err = loadObjItem2() err = loadObjItem()
logOnError(err, "initCache : caching items") logOnError(err, "initCache : caching items")
log.Println("Caching messages ..") log.Println("Caching messages ..")

View File

@ -36,6 +36,36 @@ func MaxInt(a int, b int) int {
} }
} }
func PrintText(m *tb.Message) {
fmt.Printf("[%d] %s(%d) | %s(%d) : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text)
return
}
func ReadConfig(path string) {
b, err := Asset("data/config.json")
logOnError(err, "readConfig : load data/config.json")
if err != nil {
return err
}
err = json.Unmarshal(b, &cfg)
logOnError(err, "readConfig : Unmarshal(data/config.json)")
b, err = ioutil.ReadFile(path)
logOnError(err, "readConfig : ReadFile("+path+")")
if err != nil {
return err
}
err = json.Unmarshal(b, &cfg)
logOnError(err, "readConfig : Unmarshal("+path+")")
b, err = json.Marshal(cfg)
log.Printf("ReadConfig : %s\n", b)
return
}
func fromChatWarsDate(d string) (t time.Time, err error) { func fromChatWarsDate(d string) (t time.Time, err error) {
r := regexp.MustCompile(`(?P<Day>[0-9]{2}) (?P<Month>(Wintar|Hornung|Lenzin|Ōstar|Winni|Brāh|Hewi|Aran|Witu|Wīndume|Herbist|Hailag)) (?P<Year>[0-9]{4})( (?P<Hour>[0-9]{2}):(?P<Minute>[0-9]{2})){0,1}`) r := regexp.MustCompile(`(?P<Day>[0-9]{2}) (?P<Month>(Wintar|Hornung|Lenzin|Ōstar|Winni|Brāh|Hewi|Aran|Witu|Wīndume|Herbist|Hailag)) (?P<Year>[0-9]{4})( (?P<Hour>[0-9]{2}):(?P<Minute>[0-9]{2})){0,1}`)
if r.FindStringSubmatch(d) != nil { if r.FindStringSubmatch(d) != nil {