Merge pull request #67 from Yiwen-Chan/master

改进 fortune 插件
This commit is contained in:
Kanri 2021-10-06 13:15:04 +08:00 committed by GitHub
commit 40a8473ce0
2 changed files with 11 additions and 9 deletions

View File

@ -109,8 +109,10 @@ zerobot [-d] [-g] qq1 qq2 qq3 ...
- [x] 设置随机图片网址[url] - [x] 设置随机图片网址[url]
- [x] 太涩了(撤回最近发的图) - [x] 太涩了(撤回最近发的图)
- [x] 评价图片(发送一张图片让bot评分) - [x] 评价图片(发送一张图片让bot评分)
- **每日运势** `github.com/FloatTech/ZeroBot-Plugin/plugin_fortune`
- [x] 运势
- **浅草寺求签** `github.com/FloatTech/ZeroBot-Plugin/plugin_omikuji` - **浅草寺求签** `github.com/FloatTech/ZeroBot-Plugin/plugin_omikuji`
- [x] 求签|运势|占卜 - [x] 求签|占卜
- **bilibili** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_bilibili"` - **bilibili** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_bilibili"`
- [x] >vup info [名字|uid] - [x] >vup info [名字|uid]
- [x] >user info [名字|uid] - [x] >user info [名字|uid]

View File

@ -7,7 +7,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"image/jpeg" "image/jpeg"
"io" "io"
"io/ioutil" "io/ioutil"
@ -29,6 +28,8 @@ import (
var ( var (
// 底图缓存位置 // 底图缓存位置
base = "data/fortune/" base = "data/fortune/"
// 素材下载网站
site = "https://pan.dihe.moe/fortune/"
// int64 群号 string 底图类型 // int64 群号 string 底图类型
// 底图类型列表:车万 DC4 爱因斯坦 星空列车 樱云之恋 富婆妹 李清歌 // 底图类型列表:车万 DC4 爱因斯坦 星空列车 樱云之恋 富婆妹 李清歌
// 公主连结 原神 明日方舟 碧蓝航线 碧蓝幻想 战双 阴阳师 // 公主连结 原神 明日方舟 碧蓝航线 碧蓝幻想 战双 阴阳师
@ -40,7 +41,7 @@ var (
func init() { func init() {
// 插件主体 // 插件主体
control.Register("runcode", &control.Options{ control.Register("fortune", &control.Options{
DisableOnDefault: false, DisableOnDefault: false,
Help: "每日运势: \n" + Help: "每日运势: \n" +
"- 运势", "- 运势",
@ -49,7 +50,7 @@ func init() {
// 检查签文文件是否存在 // 检查签文文件是否存在
if _, err := os.Stat(base + "运势签文.json"); err != nil && !os.IsExist(err) { if _, err := os.Stat(base + "运势签文.json"); err != nil && !os.IsExist(err) {
ctx.SendChain(message.Text("正在下载签文文件,请稍后...")) ctx.SendChain(message.Text("正在下载签文文件,请稍后..."))
_, err := download("https://pan.dihe.moe/fortune/运势签文.json", base) _, err := download(site+"运势签文.json", base)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
@ -59,7 +60,7 @@ func init() {
// 检查字体文件是否存在 // 检查字体文件是否存在
if _, err := os.Stat(base + "sakura.ttf"); err != nil && !os.IsExist(err) { if _, err := os.Stat(base + "sakura.ttf"); err != nil && !os.IsExist(err) {
ctx.SendChain(message.Text("正在下载字体文件,请稍后...")) ctx.SendChain(message.Text("正在下载字体文件,请稍后..."))
_, err := download("https://pan.dihe.moe/fortune/sakura.ttf", base) _, err := download(site+"sakura.ttf", base)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
@ -76,7 +77,7 @@ func init() {
// 检查背景图片是否存在 // 检查背景图片是否存在
if _, err := os.Stat(base + kind); err != nil && !os.IsExist(err) { if _, err := os.Stat(base + kind); err != nil && !os.IsExist(err) {
ctx.SendChain(message.Text("正在下载背景图片,请稍后...")) ctx.SendChain(message.Text("正在下载背景图片,请稍后..."))
file, err := download("https://pan.dihe.moe/fortune/"+kind+".zip", base) file, err := download(site+kind+".zip", base)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
@ -139,8 +140,6 @@ func download(link, dest string) (string, error) {
length, _ := strconv.Atoi(resp.Header.Get("Content-Length")) length, _ := strconv.Atoi(resp.Header.Get("Content-Length"))
data, _ := ioutil.ReadAll(resp.Body) data, _ := ioutil.ReadAll(resp.Body)
if length > len(data) { if length > len(data) {
fmt.Println(len(data))
fmt.Println(length)
return "", errors.New("download not complete") return "", errors.New("download not complete")
} }
// 获取文件名 // 获取文件名
@ -301,7 +300,7 @@ func draw(background, title, text string) ([]byte, error) {
} }
} }
} }
// 保存 // 转成 base64
buffer := new(bytes.Buffer) buffer := new(bytes.Buffer)
encoder := base64.NewEncoder(base64.StdEncoding, buffer) encoder := base64.NewEncoder(base64.StdEncoding, buffer)
var opt jpeg.Options var opt jpeg.Options
@ -310,5 +309,6 @@ func draw(background, title, text string) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
encoder.Close()
return buffer.Bytes(), nil return buffer.Bytes(), nil
} }