package config 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"` SendApprovalMessage bool `yaml:"send_approval_message"` DeleteRequestAfterDecision bool `yaml:"delete_request_after_decision"` } 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: StringPtr("YOUR_BOT_TOKEN_HERE"), AdminChatId: Int64Ptr(0), AdminChatTopicId: IntPtr(0), TargetChatId: Int64Ptr(0), EntryMessage: "You have requested to join the group, please write a brief message explaining why you want to join.", ApprovalMessage: "", SendApprovalMessage: false, DeleteRequestAfterDecision: false, } encoder := yaml.NewEncoder(f) err = encoder.Encode(defaultConfig) return err } // SaveConfig writes the current Config back to config.yaml, overwriting the file. func (c *Config) SaveConfig() error { f, err := os.OpenFile("config.yaml", os.O_WRONLY|os.O_TRUNC, 0644) if err != nil { return err } defer f.Close() encoder := yaml.NewEncoder(f) err = encoder.Encode(c) return err } // StringPtr returns a pointer to the given string. func StringPtr(s string) *string { return &s } // Int64Ptr returns a pointer to the given int64. func Int64Ptr(i int64) *int64 { return &i } // IntPtr returns a pointer to the given int. func IntPtr(i int) *int { return &i }