Code refactor
This commit is contained in:
parent
5e37545782
commit
d2f7297d92
7 changed files with 534 additions and 330 deletions
62
config/config.go
Normal file
62
config/config.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
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"`
|
||||
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: "",
|
||||
DeleteRequestAfterDecision: false,
|
||||
}
|
||||
|
||||
encoder := yaml.NewEncoder(f)
|
||||
err = encoder.Encode(defaultConfig)
|
||||
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 }
|
||||
Loading…
Add table
Add a link
Reference in a new issue