mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-20 22:40:23 +08:00
✏️ 修复 manager 定时器错误
同时 make lint happy
This commit is contained in:
parent
1682c321d3
commit
cb44758036
@ -1,3 +1,4 @@
|
||||
// Package bookreview 书评
|
||||
package bookreview
|
||||
|
||||
import (
|
||||
|
||||
@ -7,11 +7,11 @@ type book struct {
|
||||
|
||||
// 暂时随机选择一个书评
|
||||
func getBookReviewByKeyword(keyword string) (b book) {
|
||||
db.Find("bookreview", &b, "where bookreview LIKE '%"+keyword+"%'")
|
||||
_ = db.Find("bookreview", &b, "where bookreview LIKE '%"+keyword+"%'")
|
||||
return
|
||||
}
|
||||
|
||||
func getRandomBookReview() (b book) {
|
||||
db.Pick("bookreview", &b)
|
||||
_ = db.Pick("bookreview", &b)
|
||||
return
|
||||
}
|
||||
|
||||
@ -20,7 +20,10 @@ func initChatList(postinit func()) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
json.Unmarshal(data, &kimomap)
|
||||
err = json.Unmarshal(data, &kimomap)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for k := range kimomap {
|
||||
chatList = append(chatList, k)
|
||||
}
|
||||
|
||||
@ -66,11 +66,15 @@ func init() {
|
||||
if ok {
|
||||
c, ok := control.Lookup("fortune")
|
||||
if ok {
|
||||
c.SetData(gid, int64(i)&0xff)
|
||||
err = c.SetData(gid, int64(i)&0xff)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("设置失败:", err))
|
||||
return
|
||||
}
|
||||
ctx.SendChain(message.Text("设置成功~"))
|
||||
return
|
||||
}
|
||||
ctx.SendChain(message.Text("设置失败!"))
|
||||
ctx.SendChain(message.Text("设置失败: 找不到插件"))
|
||||
return
|
||||
}
|
||||
ctx.SendChain(message.Text("没有这个底图哦~"))
|
||||
|
||||
@ -314,7 +314,7 @@ func init() { // 插件主体
|
||||
engine.OnRegex(`^取消在"(.*)"的提醒`, zero.AdminPermission, zero.OnlyGroup).SetBlock(true).SetPriority(40).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
dateStrs := ctx.State["regex_matched"].([]string)
|
||||
ts := timer.Timer{Cron: dateStrs[1], GrpId: ctx.Event.GroupID}
|
||||
ts := timer.Timer{Cron: dateStrs[1], GrpID: ctx.Event.GroupID}
|
||||
ti := ts.GetTimerID()
|
||||
ok := clock.CancelTimer(ti)
|
||||
if ok {
|
||||
|
||||
@ -8,9 +8,9 @@ import (
|
||||
func (ts *Timer) sendmsg(grp int64, ctx *zero.Ctx) {
|
||||
ctx.Event = new(zero.Event)
|
||||
ctx.Event.GroupID = grp
|
||||
if ts.Url == "" {
|
||||
if ts.URL == "" {
|
||||
ctx.SendChain(atall, message.Text(ts.Alert))
|
||||
} else {
|
||||
ctx.SendChain(atall, message.Text(ts.Alert), message.Image(ts.Url).Add("cache", "0"))
|
||||
ctx.SendChain(atall, message.Text(ts.Alert), message.Image(ts.URL).Add("cache", "0"))
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,9 +16,9 @@ import (
|
||||
// GetTimerInfo 获得标准化定时字符串
|
||||
func (ts *Timer) GetTimerInfo() string {
|
||||
if ts.Cron != "" {
|
||||
return fmt.Sprintf("[%d]%s", ts.GrpId, ts.Cron)
|
||||
return fmt.Sprintf("[%d]%s", ts.GrpID, ts.Cron)
|
||||
}
|
||||
return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", ts.GrpId, ts.Month(), ts.Day(), ts.Week(), ts.Hour(), ts.Minute())
|
||||
return fmt.Sprintf("[%d]%d月%d日%d周%d:%d", ts.GrpID, ts.Month(), ts.Day(), ts.Week(), ts.Hour(), ts.Minute())
|
||||
}
|
||||
|
||||
// GetTimerID 获得标准化 ID
|
||||
@ -33,9 +33,9 @@ func GetFilledCronTimer(croncmd string, alert string, img string, botqq, gid int
|
||||
var ts Timer
|
||||
ts.Alert = alert
|
||||
ts.Cron = croncmd
|
||||
ts.Url = img
|
||||
ts.Selfid = botqq
|
||||
ts.GrpId = gid
|
||||
ts.URL = img
|
||||
ts.SelfID = botqq
|
||||
ts.GrpID = gid
|
||||
return &ts
|
||||
}
|
||||
|
||||
@ -105,10 +105,10 @@ func GetFilledTimer(dateStrs []string, botqq, grp int64, matchDateOnly bool) *Ti
|
||||
if !matchDateOnly {
|
||||
urlStr := dateStrs[5]
|
||||
if urlStr != "" { // 是图片url
|
||||
ts.Url = urlStr[3:] // utf-8下用为3字节
|
||||
logrus.Println("[群管]" + ts.Url)
|
||||
if !strings.HasPrefix(ts.Url, "http") {
|
||||
ts.Url = "illegal"
|
||||
ts.URL = urlStr[3:] // utf-8下用为3字节
|
||||
logrus.Println("[群管]" + ts.URL)
|
||||
if !strings.HasPrefix(ts.URL, "http") {
|
||||
ts.URL = "illegal"
|
||||
logrus.Println("[群管]url非法!")
|
||||
return &ts
|
||||
}
|
||||
@ -116,8 +116,8 @@ func GetFilledTimer(dateStrs []string, botqq, grp int64, matchDateOnly bool) *Ti
|
||||
ts.Alert = dateStrs[6]
|
||||
ts.SetEn(true)
|
||||
}
|
||||
ts.Selfid = botqq
|
||||
ts.GrpId = grp
|
||||
ts.SelfID = botqq
|
||||
ts.GrpID = grp
|
||||
return &ts
|
||||
}
|
||||
|
||||
|
||||
@ -152,11 +152,11 @@ func (ts *Timer) nextWakeTime() (date time.Time) {
|
||||
func (ts *Timer) judgeHM() {
|
||||
if ts.Hour() < 0 || ts.Hour() == time.Now().Hour() {
|
||||
if ts.Minute() < 0 || ts.Minute() == time.Now().Minute() {
|
||||
if ts.Selfid != 0 {
|
||||
ts.sendmsg(ts.GrpId, zero.GetBot(ts.Selfid))
|
||||
if ts.SelfID != 0 {
|
||||
ts.sendmsg(ts.GrpID, zero.GetBot(ts.SelfID))
|
||||
} else {
|
||||
zero.RangeBot(func(id int64, ctx *zero.Ctx) (_ bool) {
|
||||
ts.sendmsg(ts.GrpId, ctx)
|
||||
ts.sendmsg(ts.GrpID, ctx)
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
@ -6,15 +6,16 @@ import (
|
||||
|
||||
// Timer 计时器
|
||||
type Timer struct {
|
||||
Id uint32 `db:"id"`
|
||||
ID uint32 `db:"id"`
|
||||
En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"`
|
||||
Selfid int64 `db:"sid"`
|
||||
GrpId int64 `db:"gid"`
|
||||
SelfID int64 `db:"sid"`
|
||||
GrpID int64 `db:"gid"`
|
||||
Alert string `db:"alert"`
|
||||
Cron string `db:"cron"`
|
||||
Url string `db:"url"`
|
||||
URL string `db:"url"`
|
||||
}
|
||||
|
||||
// InsertInto 插入自身
|
||||
func (t *Timer) InsertInto(db *sql.Sqlite) error {
|
||||
return db.Insert("timer", t)
|
||||
}
|
||||
|
||||
@ -12,10 +12,10 @@ import (
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
||||
)
|
||||
|
||||
// Clock 时钟
|
||||
type Clock struct {
|
||||
db *sql.Sqlite
|
||||
timers *(map[uint32]*Timer)
|
||||
@ -37,10 +37,12 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// NewClock 添加一个新时钟
|
||||
func NewClock(db *sql.Sqlite) (c Clock) {
|
||||
c.loadTimers(db)
|
||||
c.cron = cron.New()
|
||||
c.entries = make(map[uint32]cron.EntryID)
|
||||
c.timers = &map[uint32]*Timer{}
|
||||
c.cron.Start()
|
||||
return
|
||||
}
|
||||
@ -50,9 +52,9 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
|
||||
var key uint32
|
||||
if save {
|
||||
key = ts.GetTimerID()
|
||||
ts.Id = key
|
||||
ts.ID = key
|
||||
} else {
|
||||
key = ts.Id
|
||||
key = ts.ID
|
||||
}
|
||||
t, ok := c.GetTimer(key)
|
||||
if t != ts && ok { // 避免重复注册定时器
|
||||
@ -61,16 +63,16 @@ func (c *Clock) RegisterTimer(ts *Timer, save bool) bool {
|
||||
logrus.Println("[群管]注册计时器", key)
|
||||
if ts.Cron != "" {
|
||||
var ctx *zero.Ctx
|
||||
if ts.Selfid != 0 {
|
||||
ctx = zero.GetBot(ts.Selfid)
|
||||
if ts.SelfID != 0 {
|
||||
ctx = zero.GetBot(ts.SelfID)
|
||||
} else {
|
||||
zero.RangeBot(func(id int64, c *zero.Ctx) bool {
|
||||
ctx = c
|
||||
ts.Selfid = id
|
||||
ts.SelfID = id
|
||||
return false
|
||||
})
|
||||
}
|
||||
eid, err := c.cron.AddFunc(ts.Cron, func() { ts.sendmsg(ts.GrpId, ctx) })
|
||||
eid, err := c.cron.AddFunc(ts.Cron, func() { ts.sendmsg(ts.GrpID, ctx) })
|
||||
if err == nil {
|
||||
c.entmu.Lock()
|
||||
c.entries[key] = eid
|
||||
@ -135,7 +137,7 @@ func (c *Clock) ListTimers(grpID int64) []string {
|
||||
c.timersmu.RLock()
|
||||
keys := make([]string, 0, len(*c.timers))
|
||||
for _, v := range *c.timers {
|
||||
if v.GrpId == grpID {
|
||||
if v.GrpID == grpID {
|
||||
k := v.GetTimerInfo()
|
||||
start := strings.Index(k, "]")
|
||||
msg := strings.ReplaceAll(k[start+1:]+"\n", "-1", "每")
|
||||
@ -147,11 +149,11 @@ func (c *Clock) ListTimers(grpID int64) []string {
|
||||
}
|
||||
c.timersmu.RUnlock()
|
||||
return keys
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetTimer 获得定时器
|
||||
func (c *Clock) GetTimer(key uint32) (t *Timer, ok bool) {
|
||||
c.timersmu.RLock()
|
||||
t, ok = (*c.timers)[key]
|
||||
@ -159,25 +161,24 @@ func (c *Clock) GetTimer(key uint32) (t *Timer, ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// AddTimer 添加定时器
|
||||
func (c *Clock) AddTimer(t *Timer) (err error) {
|
||||
c.timersmu.Lock()
|
||||
(*c.timers)[t.Id] = t
|
||||
(*c.timers)[t.ID] = t
|
||||
err = c.db.Insert("timer", t)
|
||||
c.timersmu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Clock) loadTimers(db *sql.Sqlite) {
|
||||
if file.IsExist(db.DBPath) {
|
||||
c.db = db
|
||||
err := c.db.Create("timer", &Timer{})
|
||||
if err == nil {
|
||||
var t Timer
|
||||
c.db.FindFor("timer", &t, "", func() error {
|
||||
_ = c.db.FindFor("timer", &t, "", func() error {
|
||||
tescape := t
|
||||
go c.RegisterTimer(&tescape, false)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@ -22,3 +23,11 @@ func TestNextWakeTime(t *testing.T) {
|
||||
t.Log(t1)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
func TestClock(t *testing.T) {
|
||||
db := &sql.Sqlite{DBPath: "test.db"}
|
||||
c := NewClock(db)
|
||||
c.AddTimer(GetFilledTimer([]string{"", "12", "-1", "12", "0", "", "test"}, 0, 0, false))
|
||||
t.Log(c.ListTimers(0))
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
@ -81,7 +81,11 @@ func init() {
|
||||
url := ctx.State["image_url"].([]string)[0]
|
||||
grpfolder := base + "/" + strconv.FormatInt(ctx.Event.GroupID, 36)
|
||||
if file.IsNotExist(grpfolder) {
|
||||
os.Mkdir(grpfolder, 0755)
|
||||
err = os.Mkdir(grpfolder, 0755)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("错误:", err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
err = file.DownloadTo(url, grpfolder+"/"+name, true)
|
||||
if err == nil {
|
||||
|
||||
@ -3,7 +3,7 @@ package omikuji
|
||||
import "strconv"
|
||||
|
||||
type kuji struct {
|
||||
Id uint8 `db:"id"`
|
||||
ID uint8 `db:"id"`
|
||||
Text string `db:"text"`
|
||||
}
|
||||
|
||||
|
||||
@ -16,6 +16,7 @@ var (
|
||||
)
|
||||
|
||||
// DownloadTo 下载到路径
|
||||
//nolint: bodyclose
|
||||
func DownloadTo(url, file string, chkcrt bool) error {
|
||||
var resp *http.Response
|
||||
var err error
|
||||
|
||||
Loading…
Reference in New Issue
Block a user