Skip to content

Commit

Permalink
增加公招查询功能
Browse files Browse the repository at this point in the history
增加解析b站视频app卡片功能
增加查询bot内存占用功能
增加自动同意好友请求功能
新增群发消息功能
修复十连抽卡时,sql过慢的问题
  • Loading branch information
yuanyan3060 committed Dec 10, 2021
1 parent 96c564e commit 5462a53
Show file tree
Hide file tree
Showing 21 changed files with 1,102 additions and 123 deletions.
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CommandPrefix: ""
SuperUsers: [""]
WS: "ws://127.0.0.1:6700"
SuperUsers: ["1846865993"]
WS: "ws://192.168.0.104:8080"
AccessToken: "114514"
26 changes: 23 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,44 @@ go 1.17

require (
github.com/fogleman/gg v1.3.0
github.com/sirupsen/logrus v1.8.1
github.com/wdvxdr1123/ZeroBot v1.4.1
golang.org/x/image v0.0.0-20211028202545-6944b10bf410
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gorm.io/driver/sqlite v1.2.4
gorm.io/gorm v1.22.3
)

require (
github.com/fumiama/cron v1.3.0 // indirect
github.com/fumiama/go-base16384 v1.2.1 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.2 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/logoove/sqlite v1.13.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-sqlite3 v1.14.9 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/tidwall/gjson v1.11.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
golang.org/x/mod v0.3.0 // indirect
golang.org/x/sys v0.0.0-20210902050250-f475640dd07b // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
lukechampine.com/uint128 v1.1.1 // indirect
modernc.org/cc/v3 v3.34.0 // indirect
modernc.org/ccgo/v3 v3.11.2 // indirect
modernc.org/libc v1.11.3 // indirect
modernc.org/mathutil v1.4.1 // indirect
modernc.org/memory v1.0.5 // indirect
modernc.org/opt v0.1.1 // indirect
modernc.org/sqlite v1.13.0 // indirect
modernc.org/strutil v1.1.1 // indirect
modernc.org/token v1.0.0 // indirect
)
178 changes: 176 additions & 2 deletions go.sum

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
_ "SkadiBot/plugins/arknights"
_ "SkadiBot/plugins/bilibili"
_ "SkadiBot/plugins/group"
_ "SkadiBot/plugins/normal"
"fmt"
Expand All @@ -11,6 +12,8 @@ import (
"gopkg.in/yaml.v3"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"os"
"regexp"
"strings"
)
Expand All @@ -36,7 +39,16 @@ func (hook *Base64Hook) Fire(entry *log.Entry) error {
func (hook *Base64Hook) Levels() []log.Level {
return log.AllLevels
}

func main() {
err := os.Setenv("HTTP_PROXY", "")
if err != nil {
return
}
err = os.Setenv("HTTPS_PROXY", "")
if err != nil {
return
}
fmt.Println("交流群群号:258140966")
fmt.Println("仓库地址:https://github.com/yuanyan3060/SkadiBot")
rep, err := http.Get("https://cdn.jsdelivr.net/gh/yuanyan3060/SkadiBot/LICENSE")
Expand Down
43 changes: 43 additions & 0 deletions plugins/arknights/checkin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package arknights

import (
"fmt"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"gorm.io/gorm"
"time"
)

func init() {
zero.OnCommand("签到").
Handle(func(ctx *zero.Ctx) {
var user User
err := DB.First(&user, "qq = ?", ctx.Event.UserID).Error
if err == gorm.ErrRecordNotFound {
user = User{
QQ: ctx.Event.UserID,
Chars: "{}",
TenGachaTickets: 0,
LastCheckInTime: time.Unix(0, 0),
}
}
if user.isCanCheckIn() {
user.LastCheckInTime = time.Now()
user.TenGachaTickets = 10
ctx.SendChain(
message.Text("签到获得10十连券"),
)
err = DB.Save(&user).Error
err := DB.First(&user, "qq = ?", ctx.Event.UserID).Error
fmt.Println(user.TenGachaTickets)
if err != nil {
fmt.Println(err)
return
}
} else {
ctx.SendChain(
message.Text("今日已签过到"),
)
}
})
}
61 changes: 33 additions & 28 deletions plugins/arknights/gacha.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,25 @@ import (
var CharTable map[string]CharData
var Rarity2CharName [][]string

func getProfessionImage(profession string) (image.Image, error) {
func getProfessionImage(profession string) (*image.Image, error) {
professionImagePath := fmt.Sprintf("data/static/profession_img/%s.png", profession)
professionImage, err := gg.LoadImage(professionImagePath)
if err != nil {
return nil, err
}
return professionImage, nil
return &professionImage, nil
}

func getRarityImage(rarity int8) (image.Image, error) {
func getRarityImage(rarity int8) (*image.Image, error) {
rarityImagePath := fmt.Sprintf("data/static/gacha_rarity_img/%d.png", rarity)
rarityImage, err := gg.LoadImage(rarityImagePath)
if err != nil {
return nil, err
}
return rarityImage, nil
return &rarityImage, nil
}

func getRarityBackImage(rarity int8, index int) (image.Image, error) {

func getRarityBackImage(rarity int8, index int) (*image.Image, error) {
rarityImage, err := getRarityImage(rarity)
if err != nil {
return nil, err
Expand All @@ -48,16 +47,16 @@ func getRarityBackImage(rarity int8, index int) (image.Image, error) {
rarityBackImageRGBA := rarityBackRGBA.SubImage(image.Rect(27+index*123, 0, 149+index*123, 720))
rarityBackImageCrop := gg.NewContextForImage(rarityBackImageRGBA)
rarityBackImage := rarityBackImageCrop.Image()
return rarityBackImage, nil
return &rarityBackImage, nil
}

func imageToRGBA(src image.Image) *image.RGBA {
if dst, ok := src.(*image.RGBA); ok {
func imageToRGBA(src *image.Image) *image.RGBA {
if dst, ok := (*src).(*image.RGBA); ok {
return dst
}
b := src.Bounds()
b := (*src).Bounds()
dst := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(dst, dst.Bounds(), src, b.Min, draw.Src)
draw.Draw(dst, dst.Bounds(), *src, b.Min, draw.Src)
return dst
}

Expand Down Expand Up @@ -134,33 +133,34 @@ func drawGachaImage(charNames []string) ([]byte, error) {
return nil, err
}

background.DrawImage(rarityBackImage, 0, 0)
background.DrawImage(rarityImage, 27+index*123, 0)
background.DrawImage(*rarityBackImage, 0, 0)
background.DrawImage(*rarityImage, 27+index*123, 0)
background.DrawImage(charImage, 27+index*123, 175)
background.DrawImage(professionImage, 34+int(math.Round(float64(index)*122.5)), 490)
background.DrawImage(*professionImage, 34+int(math.Round(float64(index)*122.5)), 490)
}
return utils.Image2Base64(background.Image()), nil
}

func init() {
zero.OnCommand("十连").
Handle(func(ctx *zero.Ctx) {
rollResult := rollGacha()
i, err := drawGachaImage(rollResult)
if err != nil {
return
}
sendBase64 := "base64://" + helper.BytesToString(i)

var user User
err = DB.First(&user, "qq = ?", ctx.Event.UserID).Error
err := DB.First(&user, "qq = ?", ctx.Event.UserID).Error
if err == gorm.ErrRecordNotFound {
user = User{
QQ: ctx.Event.UserID,
Chars: "{}",
QQ: ctx.Event.UserID,
Chars: "{}",
TenGachaTickets: 0,
LastCheckInTime: time.Unix(0, 0),
}
DB.Save(&user)
}
if user.TenGachaTickets <= 0 {
ctx.SendChain(
message.Text("十连寻访凭证不足"),
)
return
}
rollResult := rollGacha()
oldCharDict := make(map[string]int)
err = json.Unmarshal([]byte(user.Chars), &oldCharDict)
if err != nil {
Expand All @@ -178,9 +178,14 @@ func init() {
if err != nil {
return
}
start := time.Now()
DB.Model(&user).Where("qq = ?", ctx.Event.UserID).Update("chars", string(CharsBytes))
fmt.Println(time.Now().Sub(start))
user.Chars = string(CharsBytes)
user.TenGachaTickets -= 1
err = DB.Save(&user).Error
i, err := drawGachaImage(rollResult)
if err != nil {
return
}
sendBase64 := "base64://" + helper.BytesToString(i)
ctx.SendChain(
message.Image(sendBase64),
message.Text(gachaTextBuild(rollResult)),
Expand Down
22 changes: 17 additions & 5 deletions plugins/arknights/models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package arknights

import (
"SkadiBot/plugins/utils"
"encoding/json"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
Expand All @@ -19,11 +20,21 @@ type CharData struct {
}

type User struct {
gorm.Model
QQ int64 `gorm:"primary_key"`
QQ int64 `gorm:"primaryKey"`
LastCheckInTime time.Time
TenGachaTickets int
Chars string
tenGachaTickets int
lastCheckInTime float64
}

func (user *User) isCanCheckIn() bool {
duration := -4 * 60 * 60 * time.Second
lastTime := user.LastCheckInTime.In(utils.CstZone).Add(duration)
now := time.Now().In(utils.CstZone).Add(duration)
nowYear, nowMonth, nowDay := now.Date()
lastTimeYear, lastTimeMonth, lastTimeDay := lastTime.Date()
/*fmt.Println(nowYear, nowMonth, nowDay, now.Format("2006-01-02 15:04:05"))
fmt.Println(lastTimeYear, lastTimeMonth, lastTimeDay, lastTime.Format("2006-01-02 15:04:05"))*/
return nowDay != lastTimeDay || nowMonth != lastTimeMonth || nowYear != lastTimeYear
}

var DB *gorm.DB
Expand Down Expand Up @@ -51,9 +62,10 @@ func init() {
//设定慢查询时间阈值为800ms
SlowThreshold: 800 * time.Millisecond,
//设置日志级别,只有Warn和Info级别会输出慢查询日志
LogLevel: logger.Warn,
LogLevel: logger.Info,
},
)

DB, err = gorm.Open(sqlite.Open("arknights.db"), &gorm.Config{
Logger: slowLogger,
})
Expand Down
Loading

0 comments on commit 5462a53

Please sign in to comment.