make lint happy

This commit is contained in:
fumiama 2021-12-25 13:01:05 +08:00
parent 59fe8a021f
commit eebfa23509
11 changed files with 90 additions and 77 deletions

View File

@ -1,4 +1,4 @@
package plugin_book_review package bookreview
import ( import (
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"

View File

@ -1,4 +1,4 @@
package plugin_book_review package bookreview
import ( import (
"os" "os"

View File

@ -1,7 +1,7 @@
package plugin_book_review package bookreview
type book struct { type book struct {
Id uint64 `db:"id"` ID uint64 `db:"id"`
BookReview string `db:"bookreview"` BookReview string `db:"bookreview"`
} }

View File

@ -7,36 +7,36 @@ import (
"github.com/FloatTech/ZeroBot-Plugin/control" "github.com/FloatTech/ZeroBot-Plugin/control"
) )
const ( const prio = 10
prio = 10
)
var (
engine = control.Register("chouxianghua", &control.Options{
DisableOnDefault: false,
Help: "抽象话\n- 抽象翻译\n",
})
)
func init() { func init() {
engine.OnRegex("^抽象翻译([\u4E00-\u9FA5A-Za-z0-9]{1,25})$").SetBlock(true).SetPriority(prio). control.Register("chouxianghua", &control.Options{
DisableOnDefault: false,
Help: "抽象话\n- 抽象翻译xxx\n",
}).OnRegex("^抽象翻译([\u4E00-\u9FA5A-Za-z0-9]+)$").SetBlock(true).SetPriority(prio).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
cxresult := chouXiang(ctx.State["regex_matched"].([]string)[1]) r := cx(ctx.State["regex_matched"].([]string)[1])
ctx.SendChain(message.Text(cxresult)) ctx.SendChain(message.Text(r))
}) })
} }
func chouXiang(s string) (cxresult string) { func cx(s string) (r string) {
h := []rune(s) h := []rune(s)
for i := 0; i < len(h); i++ { for i := 0; i < len(h); i++ {
if i < len(h)-1 && (getEmojiByPronunciation(getPronunciationByWord(string(h[i])).Pronunciation+getPronunciationByWord(string(h[i+1])).Pronunciation).Emoji != "") { if i < len(h)-1 {
cxresult += getEmojiByPronunciation(getPronunciationByWord(string(h[i])).Pronunciation + getPronunciationByWord(string(h[i+1])).Pronunciation).Emoji e := getEmojiByPronun(getPronunByDWord(h[i], h[i+1]))
i++ if e != "" {
} else if getEmojiByPronunciation(getPronunciationByWord(string(h[i])).Pronunciation).Emoji != "" { r += e
cxresult += getEmojiByPronunciation(getPronunciationByWord(string(h[i])).Pronunciation).Emoji i++
} else { continue
cxresult += string(h[i]) }
} }
e := getEmojiByPronun(getPinyinByWord(string(h[i])))
if e != "" {
r += e
continue
}
r += string(h[i])
} }
return return
} }

View File

@ -24,7 +24,7 @@ func init() {
// os.RemoveAll(dbpath) // os.RemoveAll(dbpath)
_ = os.MkdirAll(dbpath, 0755) _ = os.MkdirAll(dbpath, 0755)
_, _ = file.GetLazyData(dbfile, false, true) _, _ = file.GetLazyData(dbfile, false, true)
err := db.Create("pinyin", &Pinyin{}) err := db.Create("pinyin", &pinyin{})
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -1,20 +1,26 @@
package chouxianghua package chouxianghua
type Pinyin struct { type pinyin struct {
Word string `db:"word"` Word string `db:"word"`
Pronunciation string `db:"pronunciation"` Pronun string `db:"pronunciation"`
} }
type Emoji struct { type emoji struct {
Pronunciation string `db:"pronunciation"` Pronun string `db:"pronunciation"`
Emoji string `db:"emoji"` Emoji string `db:"emoji"`
} }
func getPronunciationByWord(word string) (p Pinyin) { func getPinyinByWord(word string) string {
var p pinyin
db.Find("pinyin", &p, "where word = '"+word+"'") db.Find("pinyin", &p, "where word = '"+word+"'")
return return p.Pronun
} }
func getEmojiByPronunciation(pronunciation string) (e Emoji) { func getPronunByDWord(w0, w1 rune) string {
db.Find("emoji", &e, "where pronunciation = '"+pronunciation+"'") return getPinyinByWord(string(w0)) + getPinyinByWord(string(w1))
return }
func getEmojiByPronun(pronun string) string {
var e emoji
db.Find("emoji", &e, "where pronunciation = '"+pronun+"'")
return e.Emoji
} }

View File

@ -96,6 +96,7 @@ func init() { // 插件主体
} }
// notnull 如果传入文本为空,则返回默认值 // notnull 如果传入文本为空,则返回默认值
//nolint: unparam
func notnull(text, defstr string) string { func notnull(text, defstr string) string {
if text == "" { if text == "" {
return defstr return defstr

View File

@ -21,7 +21,7 @@ func (ts *Timer) GetTimerInfo() string {
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())
} }
// GetTimerInfo 获得标准化 ID // GetTimerID 获得标准化 ID
func (ts *Timer) GetTimerID() uint32 { func (ts *Timer) GetTimerID() uint32 {
key := ts.GetTimerInfo() key := ts.GetTimerInfo()
m := md5.Sum(helper.StringToBytes(key)) m := md5.Sum(helper.StringToBytes(key))
@ -155,13 +155,12 @@ func chineseNum2Int(rs []rune) int {
func chineseChar2Int(c rune) int { func chineseChar2Int(c rune) int {
if c == rune('日') || c == rune('天') { // 周日/周天 if c == rune('日') || c == rune('天') { // 周日/周天
return 7 return 7
} else {
match := []rune("零一二三四五六七八九十")
for i, m := range match {
if c == m {
return i
}
}
return 0
} }
match := []rune("零一二三四五六七八九十")
for i, m := range match {
if c == m {
return i
}
}
return 0
} }

View File

@ -1,11 +1,10 @@
package timer package timer
import ( import (
"strconv"
"github.com/FloatTech/ZeroBot-Plugin/utils/sql" "github.com/FloatTech/ZeroBot-Plugin/utils/sql"
) )
// Timer 计时器
type Timer struct { type Timer struct {
Id uint32 `db:"id"` Id uint32 `db:"id"`
En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"` En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"`
@ -20,7 +19,9 @@ func (t *Timer) InsertInto(db *sql.Sqlite) error {
return db.Insert("timer", t) return db.Insert("timer", t)
} }
/*
func getTimerFrom(db *sql.Sqlite, id uint32) (t Timer, err error) { func getTimerFrom(db *sql.Sqlite, id uint32) (t Timer, err error) {
err = db.Find("timer", &t, "where id = "+strconv.Itoa(int(id))) err = db.Find("timer", &t, "where id = "+strconv.Itoa(int(id)))
return return
} }
*/

View File

@ -6,12 +6,14 @@ import (
"time" "time"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
_ "github.com/logoove/sqlite" _ "github.com/logoove/sqlite" // use sql
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// SleepDB 睡眠数据库
type SleepDB gorm.DB type SleepDB gorm.DB
// Initialize 初始化
func Initialize(dbpath string) *SleepDB { func Initialize(dbpath string) *SleepDB {
var err error var err error
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) { if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
@ -30,6 +32,7 @@ func Initialize(dbpath string) *SleepDB {
return (*SleepDB)(gdb) return (*SleepDB)(gdb)
} }
// Open 打开
func Open(dbpath string) (*SleepDB, error) { func Open(dbpath string) (*SleepDB, error) {
db, err := gorm.Open("sqlite3", dbpath) db, err := gorm.Open("sqlite3", dbpath)
if err != nil { if err != nil {
@ -39,24 +42,27 @@ func Open(dbpath string) (*SleepDB, error) {
} }
} }
// Close 关闭
func (sdb *SleepDB) Close() error { func (sdb *SleepDB) Close() error {
db := (*gorm.DB)(sdb) db := (*gorm.DB)(sdb)
return db.Close() return db.Close()
} }
// SleepManage 睡眠信息
type SleepManage struct { type SleepManage struct {
gorm.Model gorm.Model
GroupId int64 `gorm:"column:group_id"` GroupID int64 `gorm:"column:group_id"`
UserId int64 `gorm:"column:user_id"` UserID int64 `gorm:"column:user_id"`
SleepTime time.Time `gorm:"column:sleep_time"` SleepTime time.Time `gorm:"column:sleep_time"`
} }
// TableName 表名
func (SleepManage) TableName() string { func (SleepManage) TableName() string {
return "sleep_manage" return "sleep_manage"
} }
// 更新睡眠时间 // Sleep 更新睡眠时间
func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.Duration) { func (sdb *SleepDB) Sleep(gid, uid int64) (position int, awakeTime time.Duration) {
db := (*gorm.DB)(sdb) db := (*gorm.DB)(sdb)
now := time.Now() now := time.Now()
var today time.Time var today time.Time
@ -66,11 +72,11 @@ func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.D
today = now.Add(-time.Hour*time.Duration(3+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second())) today = now.Add(-time.Hour*time.Duration(3+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
} }
st := SleepManage{ st := SleepManage{
GroupId: groupId, GroupID: gid,
UserId: userId, UserID: uid,
SleepTime: now, SleepTime: now,
} }
if err := db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).First(&st).Error; err != nil { if err := db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).First(&st).Error; err != nil {
// error handling... // error handling...
if gorm.IsRecordNotFoundError(err) { if gorm.IsRecordNotFoundError(err) {
db.Debug().Model(&SleepManage{}).Create(&st) // newUser not user db.Debug().Model(&SleepManage{}).Create(&st) // newUser not user
@ -78,26 +84,26 @@ func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.D
} else { } else {
log.Println("sleeptime为", st) log.Println("sleeptime为", st)
awakeTime = now.Sub(st.SleepTime) awakeTime = now.Sub(st.SleepTime)
db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).Update( db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).Update(
map[string]interface{}{ map[string]interface{}{
"sleep_time": now, "sleep_time": now,
}) })
} }
db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", groupId, now, today).Count(&position) db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", gid, now, today).Count(&position)
return position, awakeTime return position, awakeTime
} }
// 更新起床时间 // GetUp 更新起床时间
func (sdb *SleepDB) GetUp(groupId, userId int64) (position int, sleepTime time.Duration) { func (sdb *SleepDB) GetUp(gid, uid int64) (position int, sleepTime time.Duration) {
db := (*gorm.DB)(sdb) db := (*gorm.DB)(sdb)
now := time.Now() now := time.Now()
today := now.Add(-time.Hour*time.Duration(-6+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second())) today := now.Add(-time.Hour*time.Duration(-6+now.Hour()) - time.Minute*time.Duration(now.Minute()) - time.Second*time.Duration(now.Second()))
st := SleepManage{ st := SleepManage{
GroupId: groupId, GroupID: gid,
UserId: userId, UserID: uid,
SleepTime: now, SleepTime: now,
} }
if err := db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).First(&st).Error; err != nil { if err := db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).First(&st).Error; err != nil {
// error handling... // error handling...
if gorm.IsRecordNotFoundError(err) { if gorm.IsRecordNotFoundError(err) {
db.Debug().Model(&SleepManage{}).Create(&st) // newUser not user db.Debug().Model(&SleepManage{}).Create(&st) // newUser not user
@ -105,11 +111,11 @@ func (sdb *SleepDB) GetUp(groupId, userId int64) (position int, sleepTime time.D
} else { } else {
log.Println("sleeptime为", st) log.Println("sleeptime为", st)
sleepTime = now.Sub(st.SleepTime) sleepTime = now.Sub(st.SleepTime)
db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", groupId, userId).Update( db.Debug().Model(&SleepManage{}).Where("group_id = ? and user_id = ?", gid, uid).Update(
map[string]interface{}{ map[string]interface{}{
"sleep_time": now, "sleep_time": now,
}) })
} }
db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", groupId, now, today).Count(&position) db.Debug().Model(&SleepManage{}).Where("group_id = ? and sleep_time <= ? and sleep_time >= ?", gid, now, today).Count(&position)
return position, sleepTime return position, sleepTime
} }

View File

@ -7,7 +7,7 @@ import (
"strings" "strings"
"time" "time"
_ "github.com/logoove/sqlite" _ "github.com/logoove/sqlite" // use sql
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
@ -106,22 +106,22 @@ func init() {
} else { } else {
tc := db.GetThirdCategory(firstIndex, secondIndex, thirdIndex) tc := db.GetThirdCategory(firstIndex, secondIndex, thirdIndex)
reg := regexp.MustCompile(regStr) reg := regexp.MustCompile(regStr)
recordUrl := tc.ThirdCategoryPath recURL := tc.ThirdCategoryPath
if recordUrl == "" { if recURL == "" {
ctx.SendChain(message.Reply(e.MessageID), message.Text("没有内容请重新选择,三次输入错误,指令可退出重输")) ctx.SendChain(message.Reply(e.MessageID), message.Text("没有内容请重新选择,三次输入错误,指令可退出重输"))
ctx.SendChain(message.Reply(e.MessageID), message.Text(db.GetAllFirstCategoryMessage())) ctx.SendChain(message.Reply(e.MessageID), message.Text(db.GetAllFirstCategoryMessage()))
errorCount++ errorCount++
step = 1 step = 1
} else { } else {
if reg.MatchString(recordUrl) { if reg.MatchString(recURL) {
// log.Println(reg.FindStringSubmatch(recordUrl)[1]) // log.Println(reg.FindStringSubmatch(recordUrl)[1])
// log.Println(url.QueryEscape(reg.FindStringSubmatch(recordUrl)[1])) // log.Println(url.QueryEscape(reg.FindStringSubmatch(recordUrl)[1]))
recordUrl = strings.ReplaceAll(recordUrl, reg.FindStringSubmatch(recordUrl)[1], url.QueryEscape(reg.FindStringSubmatch(recordUrl)[1])) recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1]))
recordUrl = strings.ReplaceAll(recordUrl, "+", "%20") recURL = strings.ReplaceAll(recURL, "+", "%20")
// log.Println(recordUrl) // log.Println(recordUrl)
} }
ctx.SendChain(message.Reply(e.MessageID), message.Text("请欣赏《"+tc.ThirdCategoryName+"》")) ctx.SendChain(message.Reply(e.MessageID), message.Text("请欣赏《"+tc.ThirdCategoryName+"》"))
ctx.SendChain(message.Record(recordUrl)) ctx.SendChain(message.Record(recURL))
return return
} }
} }
@ -145,16 +145,16 @@ func init() {
fc := db.GetFirstCategoryByFirstUid(tc.FirstCategoryUid) fc := db.GetFirstCategoryByFirstUid(tc.FirstCategoryUid)
if (tc != model.ThirdCategory{}) && (fc != model.FirstCategory{}) { if (tc != model.ThirdCategory{}) && (fc != model.FirstCategory{}) {
reg := regexp.MustCompile(regStr) reg := regexp.MustCompile(regStr)
recordUrl := tc.ThirdCategoryPath recURL := tc.ThirdCategoryPath
if reg.MatchString(recordUrl) { if reg.MatchString(recURL) {
// log.Println(reg.FindStringSubmatch(recordUrl)[1]) // log.Println(reg.FindStringSubmatch(recordUrl)[1])
// log.Println(url.QueryEscape(reg.FindStringSubmatch(recordUrl)[1])) // log.Println(url.QueryEscape(reg.FindStringSubmatch(recordUrl)[1]))
recordUrl = strings.ReplaceAll(recordUrl, reg.FindStringSubmatch(recordUrl)[1], url.QueryEscape(reg.FindStringSubmatch(recordUrl)[1])) recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1]))
recordUrl = strings.ReplaceAll(recordUrl, "+", "%20") recURL = strings.ReplaceAll(recURL, "+", "%20")
// log.Println(recordUrl) // log.Println(recordUrl)
} }
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请欣赏"+fc.FirstCategoryName+"的《"+tc.ThirdCategoryName+"》")) ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("请欣赏"+fc.FirstCategoryName+"的《"+tc.ThirdCategoryName+"》"))
ctx.SendChain(message.Record(recordUrl)) ctx.SendChain(message.Record(recURL))
} }
db.Close() db.Close()
}) })