1 | package bot |
||
2 | |||
3 | import ( |
||
4 | "encoding/json" |
||
5 | "fmt" |
||
6 | "io/ioutil" |
||
7 | "os" |
||
8 | |||
9 | "github.com/BurntSushi/toml" |
||
10 | ) |
||
11 | |||
12 | // WeatherConfig Weather config struct |
||
13 | type WeatherConfig struct { |
||
14 | WeatherToken string |
||
15 | City string |
||
16 | } |
||
17 | |||
18 | // VoiceConfig some voice settings |
||
19 | type VoiceConfig struct { |
||
20 | Volume float32 |
||
21 | } |
||
22 | |||
23 | // GeneralConfig General config struct |
||
24 | type GeneralConfig struct { |
||
25 | Language string |
||
26 | Timezone int |
||
27 | GeonamesUsername string |
||
28 | Game string |
||
29 | EmbedColor int |
||
30 | ServiceURL string |
||
31 | MessagePool int |
||
32 | DatabaseName string |
||
33 | GeocodingApiKey string |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
34 | AdminID string |
||
35 | } |
||
36 | |||
37 | // NewsConfig News config struct |
||
38 | type NewsConfig struct { |
||
39 | APIKey string |
||
40 | Country string |
||
41 | Articles int |
||
42 | } |
||
43 | |||
44 | // MetricsConfig InfluxDB connection settings |
||
45 | type MetricsConfig struct { |
||
46 | Address string |
||
47 | Database string |
||
48 | User string |
||
49 | Password string |
||
50 | } |
||
51 | |||
52 | // DBLConfig contains bot list configs |
||
53 | type DBLConfig struct { |
||
54 | Token string |
||
55 | BotID string |
||
56 | } |
||
57 | |||
58 | // TwitchConfig contains twitch api configs |
||
59 | type TwitchConfig struct { |
||
60 | ClientID string |
||
61 | ClientSecret string |
||
62 | } |
||
63 | |||
64 | // TranslateConfig Yandex translate config struct |
||
65 | type TranslateConfig struct { |
||
66 | APIKey string |
||
67 | } |
||
68 | |||
69 | // CurrencyConfig Currency config struct |
||
70 | type CurrencyConfig struct { |
||
71 | Default []string |
||
72 | } |
||
73 | |||
74 | // LocalesMap Map with locales |
||
75 | type LocalesMap map[string]map[string]string |
||
76 | |||
77 | // WeatherCodesMap symbols for font |
||
78 | type WeatherCodesMap map[string]string |
||
79 | |||
80 | // DarkSkyConfig Dark Sky weather api config |
||
81 | type DarkSkyConfig struct { |
||
82 | Token string |
||
83 | } |
||
84 | |||
85 | // Config Main config struct. Contains all another config structs data. |
||
86 | type Config struct { |
||
87 | Weather WeatherConfig |
||
88 | General GeneralConfig |
||
89 | News NewsConfig |
||
90 | Translate TranslateConfig |
||
91 | Locales LocalesMap |
||
92 | Currency CurrencyConfig |
||
93 | WeatherCodes WeatherCodesMap |
||
94 | Metrics MetricsConfig |
||
95 | DBL DBLConfig |
||
96 | Twitch TwitchConfig |
||
97 | DarkSky DarkSkyConfig |
||
98 | Voice VoiceConfig |
||
99 | } |
||
100 | |||
101 | // GetLocale returns locale string by key |
||
102 | func (c *Config) GetLocale(key string) string { |
||
103 | return c.Locales[c.General.Language][key] |
||
104 | } |
||
105 | |||
106 | // GetLocaleLang returns translation on specified language |
||
107 | func (c *Config) GetLocaleLang(key, lang string) string { |
||
108 | if _, ok := c.Locales[lang]; ok { |
||
109 | return c.Locales[lang][key] |
||
110 | } |
||
111 | return c.Locales[c.General.Language][key] |
||
112 | } |
||
113 | |||
114 | // LoadConfig loads configs from file 'config.toml'. Terminate program if error. |
||
115 | func LoadConfig() *Config { |
||
116 | var cfg Config |
||
117 | if _, err := toml.DecodeFile("config.toml", &cfg); err != nil { |
||
118 | fmt.Printf("Config loading error: %v\n", err) |
||
119 | os.Exit(1) |
||
120 | } |
||
121 | cfg.LoadLocales() |
||
122 | cfg.LoadWeatherCodes() |
||
123 | return &cfg |
||
124 | } |
||
125 | |||
126 | // LoadLocales loads locales from file 'locales.json'. Terminate program if error. |
||
127 | func (c *Config) LoadLocales() { |
||
128 | file, e := ioutil.ReadFile("./locales.json") |
||
129 | if e != nil { |
||
130 | fmt.Printf("Locale file error: %v\n", e) |
||
131 | os.Exit(1) |
||
132 | } |
||
133 | |||
134 | err := json.Unmarshal(file, &c.Locales) |
||
135 | if err != nil { |
||
136 | panic(err) |
||
137 | } |
||
138 | |||
139 | if _, ok := c.Locales[c.General.Language]; !ok { |
||
140 | fmt.Printf("Locale file not contain language \"%v\"\n", c.General.Language) |
||
141 | os.Exit(1) |
||
142 | } |
||
143 | |||
144 | fmt.Printf("Loaded %v translations for '%v' language\n", len(c.Locales[c.General.Language]), c.General.Language) |
||
145 | } |
||
146 | |||
147 | // LoadWeatherCodes loads weather font codes from file 'codes.json' in map. Terminate program if error. |
||
148 | func (c *Config) LoadWeatherCodes() { |
||
149 | file, e := ioutil.ReadFile("./weathercodes.json") |
||
150 | if e != nil { |
||
151 | fmt.Printf("Codes file error: %v\n", e) |
||
152 | os.Exit(1) |
||
153 | } |
||
154 | |||
155 | err := json.Unmarshal(file, &c.WeatherCodes) |
||
156 | if err != nil { |
||
157 | panic(err) |
||
158 | } |
||
159 | |||
160 | fmt.Printf("Loaded %v weather codes\n", len(c.WeatherCodes)) |
||
161 | } |
||
162 |