feat: 添加疯狂星期四文案功能
- 新增 kfccrazy 包,包含随机输出和添加疯狂星期四文案的功能 - 在 router.go 中重新启用重启机器人处理器的路由 - 创建 kfc_crazy 数据库表以存储文案内容
This commit is contained in:
parent
8f06631266
commit
a1a64334c8
139
handler/kfccrazy/kfc.go
Normal file
139
handler/kfccrazy/kfc.go
Normal file
@ -0,0 +1,139 @@
|
||||
package kfccrazy
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.lxtend.com/qqbot/constants"
|
||||
"git.lxtend.com/qqbot/handler"
|
||||
"git.lxtend.com/qqbot/model"
|
||||
"git.lxtend.com/qqbot/sqlite3"
|
||||
"git.lxtend.com/qqbot/util"
|
||||
)
|
||||
|
||||
func init() {
|
||||
handler.RegisterHelpInform("疯狂星期四", "疯狂星期四", "随机输出疯狂星期四文案")
|
||||
handler.RegisterHandler("疯狂星期四", pickKfc, constants.LEVEL_USER)
|
||||
handler.RegisterHelpInform("疯狂星期四文案添加", "疯狂星期四文案添加", "添加疯狂星期四文案")
|
||||
handler.RegisterHandler("疯狂星期四文案添加", storeKfc, constants.LEVEL_USER)
|
||||
|
||||
// 创建疯狂星期四文案表
|
||||
createTableSQL := `CREATE TABLE IF NOT EXISTS kfc_crazy (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
content TEXT NOT NULL,
|
||||
created_by TEXT NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);`
|
||||
sqlite3.TryCreateTable(createTableSQL)
|
||||
}
|
||||
|
||||
func storeKfc(msg model.Message) (reply model.Reply) {
|
||||
// 分割消息获取文案内容
|
||||
tokens := util.SplitN(msg.RawMsg, 2)
|
||||
if len(tokens) < 2 {
|
||||
return model.Reply{
|
||||
ReplyMsg: "请输入要添加的疯狂星期四文案",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
content := strings.TrimSpace(tokens[1])
|
||||
if content == "" {
|
||||
return model.Reply{
|
||||
ReplyMsg: "文案内容不能为空",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
// 存储文案到数据库
|
||||
tx, err := sqlite3.GetTran()
|
||||
if err != nil {
|
||||
return model.Reply{
|
||||
ReplyMsg: "数据库错误",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
_, err = tx.Exec("INSERT INTO kfc_crazy (content, created_by) VALUES (?, ?)", content, msg.UserId)
|
||||
if err != nil {
|
||||
return model.Reply{
|
||||
ReplyMsg: "保存文案失败",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return model.Reply{
|
||||
ReplyMsg: "保存文案失败",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
return model.Reply{
|
||||
ReplyMsg: "文案添加成功",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
func pickKfc(msg model.Message) (reply model.Reply) {
|
||||
// 从数据库随机获取一条文案
|
||||
tx, err := sqlite3.GetTran()
|
||||
if err != nil {
|
||||
return model.Reply{
|
||||
ReplyMsg: "数据库错误",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
var count int
|
||||
err = tx.Get(&count, "SELECT COUNT(*) FROM kfc_crazy")
|
||||
if err != nil {
|
||||
return model.Reply{
|
||||
ReplyMsg: "获取文案失败",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
return model.Reply{
|
||||
ReplyMsg: "还没有任何疯狂星期四文案,快来添加吧!",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
var content string
|
||||
err = tx.Get(&content, "SELECT content FROM kfc_crazy ORDER BY RANDOM() LIMIT 1")
|
||||
if err != nil {
|
||||
return model.Reply{
|
||||
ReplyMsg: "获取文案失败",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return model.Reply{
|
||||
ReplyMsg: "获取文案失败",
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
||||
|
||||
return model.Reply{
|
||||
ReplyMsg: content,
|
||||
ReferOriginMsg: true,
|
||||
FromMsg: msg,
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@ import (
|
||||
_ "git.lxtend.com/qqbot/handler/headmaster"
|
||||
_ "git.lxtend.com/qqbot/handler/help"
|
||||
_ "git.lxtend.com/qqbot/handler/jrrp"
|
||||
_ "git.lxtend.com/qqbot/handler/kfccrazy"
|
||||
_ "git.lxtend.com/qqbot/handler/kw"
|
||||
_ "git.lxtend.com/qqbot/handler/newbond"
|
||||
_ "git.lxtend.com/qqbot/handler/restart"
|
||||
|
@ -27,6 +27,6 @@ func startRouter() {
|
||||
gitEngine := ginServer.Group("/git")
|
||||
gitEngine.GET("/pull", restart.PullCodeHandler)
|
||||
gitEngine.GET("/build", restart.BuildBotHandler)
|
||||
// gitEngine.GET("/restart", restart.RestartBotHandler)
|
||||
gitEngine.GET("/restart", restart.RestartBotHandler)
|
||||
go ginServer.Run(":3434")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user