diff --git a/config.go b/config.go new file mode 100644 index 0000000..e69de29 diff --git a/data/config.json b/data/config.json index e69de29..ee3a231 100644 --- a/data/config.json +++ b/data/config.json @@ -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 + } +} \ No newline at end of file diff --git a/def.go b/def.go index 6008e34..5217a15 100644 --- a/def.go +++ b/def.go @@ -13,6 +13,35 @@ const maxUnixTimestamp int64 = 2147483647 const chtwrsbotID64 int64 = 408101137 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 { Messages []ChatWarsMessage `json:"messages"` } diff --git a/main.go b/main.go index f29dd9c..2c698fc 100644 --- a/main.go +++ b/main.go @@ -19,37 +19,8 @@ import ( 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 ( - 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") db *sql.DB @@ -80,11 +51,6 @@ var ( 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() { log.Println("Starting Chirpnest...") @@ -94,7 +60,7 @@ func main() { // randomize RndSrc = rand.New(rand.NewSource(time.Now().UTC().UnixNano())) - err := gcfg.ReadFileInto(&cfg, *config) + err := ReadConfig(*config) failOnError(err, "Parsing config") _, err = Asset("data/code_obj_type.json") diff --git a/obj.go b/obj.go index 540d4d9..0d04e6f 100644 --- a/obj.go +++ b/obj.go @@ -55,7 +55,7 @@ func initCache() { logOnError(err, "initCache : caching user") log.Println("Caching items ..") - err = loadObjItem2() + err = loadObjItem() logOnError(err, "initCache : caching items") log.Println("Caching messages ..") diff --git a/utils.go b/utils.go index c4182a1..f8c09b5 100644 --- a/utils.go +++ b/utils.go @@ -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) { r := regexp.MustCompile(`(?P[0-9]{2}) (?P(Wintar|Hornung|Lenzin|Ōstar|Winni|Brāh|Hewi|Aran|Witu|Wīndume|Herbist|Hailag)) (?P[0-9]{4})( (?P[0-9]{2}):(?P[0-9]{2})){0,1}`) if r.FindStringSubmatch(d) != nil {