Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HowToCook function Integration #650

Merged
merged 10 commits into from
Apr 6, 2023
104 changes: 104 additions & 0 deletions plugin/dish/dish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Package dish 程序员做饭指南zbp版,数据来源Anduin2017/HowToCook
package dish

import (
"fmt"
"github.com/sirupsen/logrus"
"strings"
"time"

sql "github.com/FloatTech/sqlite"
ctrl "github.com/FloatTech/zbpctrl"
zero "github.com/wdvxdr1123/ZeroBot"

"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/wdvxdr1123/ZeroBot/message"
)

type dish struct {
ID uint32 `db:"id"`
Name string `db:"name"`
Materials string `db:"materials"`
Steps string `db:"steps"`
}

var (
db = &sql.Sqlite{}
initialized = false
)

func init() {
en := control.Register("dish", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Brief: "程序员做饭指南",
Help: "-怎么做[xxx]|烹饪[xxx]|随机菜谱|随便做点菜",
PublicDataFolder: "Dish",
})

db.DBPath = en.DataFolder() + "dishes.db"

if _, err := en.GetLazyData("dishes.db", true); err != nil {
sunist-c marked this conversation as resolved.
Show resolved Hide resolved
logrus.Warnln("[dish]获取菜谱数据库文件失败")
} else if err = db.Open(time.Hour * 24); err != nil {
logrus.Warnln("[dish]连接菜谱数据库失败")
} else if err = db.Create("dishes", &dish{}); err != nil {
logrus.Warnln("[dish]同步菜谱数据表失败")
} else if count, countErr := db.Count("dishes"); countErr != nil {
sunist-c marked this conversation as resolved.
Show resolved Hide resolved
logrus.Warnln("[dish]统计菜谱数据失败")
} else {
logrus.Infoln("[dish]加载", count, "条菜谱")
initialized = true
}

if !initialized {
logrus.Warnln("[dish]插件未能成功初始化")
}

en.OnPrefixGroup([]string{"怎么做", "烹饪"}).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
if !initialized {
ctx.SendChain(message.Text("客官,本店暂未开业"))
return
}

name := ctx.NickName()
dishName := ctx.State["args"].(string)
sunist-c marked this conversation as resolved.
Show resolved Hide resolved
if strings.Contains(dishName, "'") || strings.Contains(dishName, "\"") || strings.Contains(dishName, "\\") {
sunist-c marked this conversation as resolved.
Show resolved Hide resolved
ctx.SendChain(message.Text("不合法的输入"))
sunist-c marked this conversation as resolved.
Show resolved Hide resolved
} else {
var d dish
sunist-c marked this conversation as resolved.
Show resolved Hide resolved
if err := db.Find("dishes", &d, fmt.Sprintf("WHERE name like %%%s%%", dishName)); err != nil {
ctx.SendChain(message.Text(fmt.Sprintf("未能为客官%s找到%s的做法qwq", name, dishName)))
sunist-c marked this conversation as resolved.
Show resolved Hide resolved
} else {
ctx.SendChain(message.Text(fmt.Sprintf(
sunist-c marked this conversation as resolved.
Show resolved Hide resolved
"已为客官%s找到%s的做法辣!\n"+
"原材料:%s\n"+
"步骤:\n"+
"%s",
name, dishName, d.Materials, d.Steps),
))
}
}
})

en.OnPrefixGroup([]string{"随机菜谱", "随便做点菜"}).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
if !initialized {
ctx.SendChain(message.Text("客官,本店暂未开业"))
return
}

name := ctx.NickName()
var d dish
if err := db.Pick("dishes", &d); err != nil {
ctx.SendChain(message.Text("小店好像出错了,暂时端不出菜来惹"))
} else {
ctx.SendChain(message.Text(fmt.Sprintf(
"已为客官%s送上%s的做法:\n"+
"原材料:%s\n"+
"步骤:\n"+
"%s",
name, d.Name, d.Materials, d.Steps),
))
}
})
}