This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
forked from Sora233/DDBOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
133 lines (119 loc) · 3.68 KB
/
bot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package DDBOT
import (
_ "embed" // embed the default config file
"fmt"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"github.com/starskim/DDBOT/lsp"
"github.com/starskim/DDBOT/warn"
"github.com/starskim/MiraiGo-Template/bot"
"github.com/starskim/MiraiGo-Template/config"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
_ "github.com/starskim/DDBOT/logging"
_ "github.com/starskim/DDBOT/lsp/acfun"
_ "github.com/starskim/DDBOT/lsp/douyu"
_ "github.com/starskim/DDBOT/lsp/huya"
_ "github.com/starskim/DDBOT/lsp/twitcasting"
_ "github.com/starskim/DDBOT/lsp/weibo"
_ "github.com/starskim/DDBOT/lsp/youtube"
_ "github.com/starskim/DDBOT/msg-marker"
)
// defaultConfig 默认配置文件
//
//go:embed default_application.yaml
var defaultConfig string
// SetUpLog 使用默认的日志格式配置,会写入到logs文件夹内,日志会保留七天
func SetUpLog() {
writer, err := rotatelogs.New(
path.Join("logs", "%Y-%m-%d.log"),
rotatelogs.WithMaxAge(7*24*time.Hour),
rotatelogs.WithRotationTime(24*time.Hour),
)
if err != nil {
logrus.WithError(err).Error("unable to write logs")
return
}
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
PadLevelText: true,
QuoteEmptyFields: true,
})
logrus.AddHook(lfshook.NewHook(writer, &logrus.TextFormatter{
FullTimestamp: true,
PadLevelText: true,
QuoteEmptyFields: true,
ForceQuote: true,
}))
}
// Run 启动bot,这个函数会阻塞直到收到退出信号
func Run() {
if fi, err := os.Stat("device.json"); err != nil {
if os.IsNotExist(err) {
fmt.Println("警告:没有检测到device.json,正在生成,如果是第一次运行,可忽略")
bot.GenRandomDevice()
} else {
warn.Warn(fmt.Sprintf("检查device.json文件失败 - %v", err))
os.Exit(1)
}
} else {
if fi.IsDir() {
warn.Warn("检测到device.json,但目标是一个文件夹!请手动确认并删除该文件夹!")
os.Exit(1)
} else {
fmt.Println("检测到device.json,使用存在的device.json")
}
}
if fi, err := os.Stat("application.yaml"); err != nil {
if os.IsNotExist(err) {
fmt.Println("警告:没有检测到配置文件application.yaml,正在生成,如果是第一次运行,可忽略")
sb := strings.Builder{}
sb.WriteString(defaultConfig)
if err := os.WriteFile("application.yaml", []byte(sb.String()), 0755); err != nil {
warn.Warn(fmt.Sprintf("application.yaml生成失败 - %v", err))
os.Exit(1)
} else {
fmt.Println("最小配置application.yaml已生成,请按需修改,如需高级配置请查看帮助文档")
}
} else {
warn.Warn(fmt.Sprintf("检查application.yaml文件失败 - %v", err))
os.Exit(1)
}
} else {
if fi.IsDir() {
warn.Warn("检测到application.yaml,但目标是一个文件夹!请手动确认并删除该文件夹!")
os.Exit(1)
} else {
fmt.Println("检测到application.yaml,使用存在的application.yaml")
}
}
config.GlobalConfig.SetConfigName("application")
config.GlobalConfig.SetConfigType("yaml")
config.GlobalConfig.AddConfigPath(".")
config.GlobalConfig.AddConfigPath("./config")
err := config.GlobalConfig.ReadInConfig()
if err != nil {
warn.Warn(fmt.Sprintf("读取配置文件失败!请检查配置文件格式是否正确 - %v", err))
os.Exit(1)
}
config.GlobalConfig.WatchConfig()
config.Base()
// 快速初始化
bot.Init()
// 初始化 Modules
bot.StartService()
// 登录
bot.Login()
// 刷新好友列表,群列表
bot.RefreshList()
lsp.Instance.PostStart(bot.Instance)
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
bot.Stop()
}