-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
main.go
197 lines (172 loc) · 5.06 KB
/
main.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"fmt"
"github.com/Logiase/MiraiGo-Template/bot"
"github.com/Logiase/MiraiGo-Template/config"
"github.com/Logiase/MiraiGo-Template/utils"
"github.com/Sora233/DDBOT/lsp"
"github.com/Sora233/DDBOT/lsp/bilibili"
localdb "github.com/Sora233/DDBOT/lsp/buntdb"
"github.com/Sora233/DDBOT/lsp/permission"
"github.com/alecthomas/kong"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
"os"
"os/signal"
"path"
"runtime"
"strings"
"syscall"
"time"
_ "github.com/Sora233/DDBOT/logging"
_ "github.com/Sora233/DDBOT/lsp"
_ "github.com/Sora233/DDBOT/miraigo-logging"
_ "github.com/Sora233/DDBOT/msg-marker"
_ "net/http/pprof"
)
func init() {
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,
}))
}
func main() {
var cli struct {
Play bool `optional:"" help:"运行play函数,适用于测试和开发"`
Debug bool `optional:"" help:"启动debug模式"`
SetAdmin int64 `optional:"" xor:"c" help:"设置admin权限"`
Version bool `optional:"" xor:"c" short:"v" help:"打印版本信息"`
SyncBilibili bool `optional:"" xor:"c" help:"同步b站帐号的关注,适用于更换或迁移b站帐号的时候"`
}
kong.Parse(&cli)
if cli.Version {
fmt.Printf("Tags: %v\n", Tags)
fmt.Printf("COMMIT_ID: %v\n", CommitId)
fmt.Printf("BUILD_TIME: %v\n", BuildTime)
os.Exit(0)
}
if b, _ := utils.FileExist("device.json"); !b {
fmt.Println("警告:没有检测到device.json,正在生成,如果是第一次运行,可忽略")
bot.GenRandomDevice()
} else {
fmt.Println("检测到device.json,使用存在的device.json")
}
if cli.SetAdmin != 0 {
if err := localdb.InitBuntDB(""); err != nil {
fmt.Printf("初始化Buntdb失败 %v\n", err)
os.Exit(1)
}
defer localdb.Close()
sm := permission.NewStateManager()
err := sm.GrantRole(cli.SetAdmin, permission.Admin)
if err != nil {
fmt.Printf("设置Admin权限失败 %v\n", err)
}
return
}
if cli.SyncBilibili {
config.Init()
if err := localdb.InitBuntDB(""); err != nil {
fmt.Printf("初始化buntdb失败 %v \n", err)
return
}
defer localdb.Close()
c := bilibili.NewConcern(nil)
if err := c.StateManager.Start(); err != nil {
fmt.Printf("启动bilibili StateManager失败 %v\n", err)
}
bilibili.Init()
c.SyncSub()
return
}
if b, _ := utils.FileExist("application.yaml"); !b {
fmt.Println("警告:没有检测到配置文件application.yaml,正在生成,如果是第一次运行,可忽略")
if err := ioutil.WriteFile("application.yaml", []byte(exampleConfig), 0755); err != nil {
fmt.Printf("application.yaml生成失败 - %v\n", err)
} else {
fmt.Println("最小配置application.yaml已生成,请按需修改,如需高级配置请查看帮助文档")
}
}
if Tags != "UNKNOWN" {
fmt.Printf("DDBOT版本:Release版本【%v】\n", Tags)
} else {
if CommitId == "UNKNOWN" {
fmt.Println("DDBOT版本:编译版本未知")
} else {
fmt.Printf("DDBOT版本:编译版本【%v-%v】\n", BuildTime, CommitId)
}
}
fmt.Println("DDBOT唯一指定交流群:755612788")
config.Init()
// 快速初始化
bot.Init()
if cli.Debug {
lsp.Debug = true
go http.ListenAndServe("localhost:6060", nil)
}
if cli.Play {
play()
return
}
// 初始化 Modules
bot.StartService()
go CheckUpdate()
// 使用协议
// 不同协议可能会有部分功能无法使用
// 在登陆前切换协议
bot.UseProtocol(bot.AndroidPhone)
// 登录
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()
}
var exampleConfig = func() string {
s := `
# 注意,填写时请把井号及后面的内容删除,并且冒号后需要加一个空格
bot:
account: # 你bot的qq号,不填则使用扫码登陆
password: # 你bot的qq密码
onJoinGroup:
rename: "【bot】" # BOT进群后自动改名,默认改名为“【bot】”,如果留空则不自动改名
# b站相关的功能需要一个b站账号,建议使用小号
# bot将使用您b站帐号的以下功能:
# 关注用户 / 取消关注用户 / 查看关注列表
# 请注意,订阅一个账号后,此处使用的b站账号将自动关注该账号
bilibili:
account: # 你的b站账号
password: # 你的b站密码
interval: 15s
concern:
emitInterval: 5s
logLevel: info
`
// win上用记事本打开不会正确换行
if runtime.GOOS == "windows" {
s = strings.ReplaceAll(s, "\n", "\r\n")
}
return s
}()