init.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package core
  2. import (
  3. "bufio"
  4. "os"
  5. "regexp"
  6. "time"
  7. )
  8. var Duration time.Duration
  9. func init() {
  10. killp()
  11. _, err := os.Stat("/etc/sillyGirl/")
  12. if err != nil {
  13. os.MkdirAll("/etc/sillyGirl/", os.ModePerm)
  14. }
  15. for _, arg := range os.Args {
  16. if arg == "-d" {
  17. initStore()
  18. Daemon()
  19. }
  20. }
  21. initStore()
  22. ReadYaml("conf/", &Config, "https://raw.githubusercontent.com/cdle/sillyGirl/main/conf/demo_config.yaml")
  23. InitReplies()
  24. initToHandleMessage()
  25. file, err := os.Open("/etc/sillyGirl/sets.conf")
  26. if err == nil {
  27. scanner := bufio.NewScanner(file)
  28. for scanner.Scan() {
  29. line := scanner.Text()
  30. if v := regexp.MustCompile(`^\s*set\s+(\S+)\s+(\S+)\s+(\S+.*)`).FindStringSubmatch(line); len(v) > 0 {
  31. b := Bucket(v[1])
  32. if b.Get(v[2]) != v[3] {
  33. b.Set(v[2], v[3])
  34. }
  35. }
  36. }
  37. file.Close()
  38. }
  39. initSys()
  40. Duration = time.Duration(sillyGirl.GetInt("duration", 5)) * time.Second
  41. sillyGirl.Set("started_at", time.Now().Format("2006-01-02 15:04:05"))
  42. api_key := sillyGirl.Get("api_key")
  43. if api_key == "" {
  44. api_key := time.Now().UnixNano()
  45. sillyGirl.Set("api_key", api_key)
  46. }
  47. if sillyGirl.Get("uuid") == "" {
  48. sillyGirl.Set("uuid", GetUUID())
  49. }
  50. }