49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"go.yaml.in/yaml/v3"
|
|
)
|
|
|
|
type Config struct {
|
|
BotToken string `yaml:"bot_token"`
|
|
AdminChatId int64 `yaml:"admin_chat_id"`
|
|
AdminChatTopicId int `yaml:"admin_chat_topic_id"`
|
|
TargetChatId int64 `yaml:"target_chat_id"`
|
|
EntryMessage string `yaml:"entry_message"`
|
|
ApprovalMessage string `yaml:"approval_message"`
|
|
}
|
|
|
|
func (c *Config) LoadConfig() error {
|
|
f, err := os.Open("config.yaml")
|
|
if err != nil {
|
|
c.CreateConfig()
|
|
return fmt.Errorf("config.yaml not found, a new one has been created. Please fill it out and restart the bot")
|
|
}
|
|
defer f.Close()
|
|
|
|
decoder := yaml.NewDecoder(f)
|
|
err = decoder.Decode(c)
|
|
return err
|
|
}
|
|
|
|
func (c *Config) CreateConfig() error {
|
|
f, err := os.Create("config.yaml")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
defaultConfig := Config{
|
|
BotToken: "YOUR_BOT_TOKEN_HERE",
|
|
AdminChatId: 0,
|
|
TargetChatId: 0,
|
|
EntryMessage: "You have requested to join the group, please write a brief message explaining why you want to join.",
|
|
}
|
|
|
|
encoder := yaml.NewEncoder(f)
|
|
err = encoder.Encode(defaultConfig)
|
|
return err
|
|
}
|