mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-18 20:50:12 +08:00
make lint happy
This commit is contained in:
parent
59fe8a021f
commit
eebfa23509
@ -1,4 +1,4 @@
|
||||
package plugin_book_review
|
||||
package bookreview
|
||||
|
||||
import (
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package plugin_book_review
|
||||
package bookreview
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package plugin_book_review
|
||||
package bookreview
|
||||
|
||||
type book struct {
|
||||
Id uint64 `db:"id"`
|
||||
ID uint64 `db:"id"`
|
||||
BookReview string `db:"bookreview"`
|
||||
}
|
||||
|
||||
|
||||
@ -7,36 +7,36 @@ import (
|
||||
"github.com/FloatTech/ZeroBot-Plugin/control"
|
||||
)
|
||||
|
||||
const (
|
||||
prio = 10
|
||||
)
|
||||
|
||||
var (
|
||||
engine = control.Register("chouxianghua", &control.Options{
|
||||
DisableOnDefault: false,
|
||||
Help: "抽象话\n- 抽象翻译\n",
|
||||
})
|
||||
)
|
||||
const prio = 10
|
||||
|
||||
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) {
|
||||
cxresult := chouXiang(ctx.State["regex_matched"].([]string)[1])
|
||||
ctx.SendChain(message.Text(cxresult))
|
||||
r := cx(ctx.State["regex_matched"].([]string)[1])
|
||||
ctx.SendChain(message.Text(r))
|
||||
})
|
||||
}
|
||||
|
||||
func chouXiang(s string) (cxresult string) {
|
||||
func cx(s string) (r string) {
|
||||
h := []rune(s)
|
||||
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 != "") {
|
||||
cxresult += getEmojiByPronunciation(getPronunciationByWord(string(h[i])).Pronunciation + getPronunciationByWord(string(h[i+1])).Pronunciation).Emoji
|
||||
i++
|
||||
} else if getEmojiByPronunciation(getPronunciationByWord(string(h[i])).Pronunciation).Emoji != "" {
|
||||
cxresult += getEmojiByPronunciation(getPronunciationByWord(string(h[i])).Pronunciation).Emoji
|
||||
} else {
|
||||
cxresult += string(h[i])
|
||||
if i < len(h)-1 {
|
||||
e := getEmojiByPronun(getPronunByDWord(h[i], h[i+1]))
|
||||
if e != "" {
|
||||
r += e
|
||||
i++
|
||||
continue
|
||||
}
|
||||
}
|
||||
e := getEmojiByPronun(getPinyinByWord(string(h[i])))
|
||||
if e != "" {
|
||||
r += e
|
||||
continue
|
||||
}
|
||||
r += string(h[i])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ func init() {
|
||||
// os.RemoveAll(dbpath)
|
||||
_ = os.MkdirAll(dbpath, 0755)
|
||||
_, _ = file.GetLazyData(dbfile, false, true)
|
||||
err := db.Create("pinyin", &Pinyin{})
|
||||
err := db.Create("pinyin", &pinyin{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@ -1,20 +1,26 @@
|
||||
package chouxianghua
|
||||
|
||||
type Pinyin struct {
|
||||
Word string `db:"word"`
|
||||
Pronunciation string `db:"pronunciation"`
|
||||
type pinyin struct {
|
||||
Word string `db:"word"`
|
||||
Pronun string `db:"pronunciation"`
|
||||
}
|
||||
type Emoji struct {
|
||||
Pronunciation string `db:"pronunciation"`
|
||||
Emoji string `db:"emoji"`
|
||||
type emoji struct {
|
||||
Pronun string `db:"pronunciation"`
|
||||
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+"'")
|
||||
return
|
||||
return p.Pronun
|
||||
}
|
||||
|
||||
func getEmojiByPronunciation(pronunciation string) (e Emoji) {
|
||||
db.Find("emoji", &e, "where pronunciation = '"+pronunciation+"'")
|
||||
return
|
||||
func getPronunByDWord(w0, w1 rune) string {
|
||||
return getPinyinByWord(string(w0)) + getPinyinByWord(string(w1))
|
||||
}
|
||||
|
||||
func getEmojiByPronun(pronun string) string {
|
||||
var e emoji
|
||||
db.Find("emoji", &e, "where pronunciation = '"+pronun+"'")
|
||||
return e.Emoji
|
||||
}
|
||||
|
||||
@ -96,6 +96,7 @@ func init() { // 插件主体
|
||||
}
|
||||
|
||||
// notnull 如果传入文本为空,则返回默认值
|
||||
//nolint: unparam
|
||||
func notnull(text, defstr string) string {
|
||||
if text == "" {
|
||||
return defstr
|
||||
|
||||
@ -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())
|
||||
}
|
||||
|
||||
// GetTimerInfo 获得标准化 ID
|
||||
// GetTimerID 获得标准化 ID
|
||||
func (ts *Timer) GetTimerID() uint32 {
|
||||
key := ts.GetTimerInfo()
|
||||
m := md5.Sum(helper.StringToBytes(key))
|
||||
@ -155,13 +155,12 @@ func chineseNum2Int(rs []rune) int {
|
||||
func chineseChar2Int(c rune) int {
|
||||
if c == rune('日') || c == rune('天') { // 周日/周天
|
||||
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
|
||||
}
|
||||
|
||||
@ -1,11 +1,10 @@
|
||||
package timer
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
||||
)
|
||||
|
||||
// Timer 计时器
|
||||
type Timer struct {
|
||||
Id uint32 `db:"id"`
|
||||
En1Month4Day5Week3Hour5Min6 int32 `db:"emdwhm"`
|
||||
@ -20,7 +19,9 @@ func (t *Timer) InsertInto(db *sql.Sqlite) error {
|
||||
return db.Insert("timer", t)
|
||||
}
|
||||
|
||||
/*
|
||||
func getTimerFrom(db *sql.Sqlite, id uint32) (t Timer, err error) {
|
||||
err = db.Find("timer", &t, "where id = "+strconv.Itoa(int(id)))
|
||||
return
|
||||
}
|
||||
*/
|
||||
|
||||
@ -6,12 +6,14 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/logoove/sqlite"
|
||||
_ "github.com/logoove/sqlite" // use sql
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// SleepDB 睡眠数据库
|
||||
type SleepDB gorm.DB
|
||||
|
||||
// Initialize 初始化
|
||||
func Initialize(dbpath string) *SleepDB {
|
||||
var err error
|
||||
if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) {
|
||||
@ -30,6 +32,7 @@ func Initialize(dbpath string) *SleepDB {
|
||||
return (*SleepDB)(gdb)
|
||||
}
|
||||
|
||||
// Open 打开
|
||||
func Open(dbpath string) (*SleepDB, error) {
|
||||
db, err := gorm.Open("sqlite3", dbpath)
|
||||
if err != nil {
|
||||
@ -39,24 +42,27 @@ func Open(dbpath string) (*SleepDB, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Close 关闭
|
||||
func (sdb *SleepDB) Close() error {
|
||||
db := (*gorm.DB)(sdb)
|
||||
return db.Close()
|
||||
}
|
||||
|
||||
// SleepManage 睡眠信息
|
||||
type SleepManage struct {
|
||||
gorm.Model
|
||||
GroupId int64 `gorm:"column:group_id"`
|
||||
UserId int64 `gorm:"column:user_id"`
|
||||
GroupID int64 `gorm:"column:group_id"`
|
||||
UserID int64 `gorm:"column:user_id"`
|
||||
SleepTime time.Time `gorm:"column:sleep_time"`
|
||||
}
|
||||
|
||||
// TableName 表名
|
||||
func (SleepManage) TableName() string {
|
||||
return "sleep_manage"
|
||||
}
|
||||
|
||||
// 更新睡眠时间
|
||||
func (sdb *SleepDB) Sleep(groupId, userId int64) (position int, awakeTime time.Duration) {
|
||||
// Sleep 更新睡眠时间
|
||||
func (sdb *SleepDB) Sleep(gid, uid int64) (position int, awakeTime time.Duration) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
now := time.Now()
|
||||
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()))
|
||||
}
|
||||
st := SleepManage{
|
||||
GroupId: groupId,
|
||||
UserId: userId,
|
||||
GroupID: gid,
|
||||
UserID: uid,
|
||||
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...
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
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 {
|
||||
log.Println("sleeptime为", st)
|
||||
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{}{
|
||||
"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
|
||||
}
|
||||
|
||||
// 更新起床时间
|
||||
func (sdb *SleepDB) GetUp(groupId, userId int64) (position int, sleepTime time.Duration) {
|
||||
// GetUp 更新起床时间
|
||||
func (sdb *SleepDB) GetUp(gid, uid int64) (position int, sleepTime time.Duration) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
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()))
|
||||
st := SleepManage{
|
||||
GroupId: groupId,
|
||||
UserId: userId,
|
||||
GroupID: gid,
|
||||
UserID: uid,
|
||||
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...
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
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 {
|
||||
log.Println("sleeptime为", st)
|
||||
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{}{
|
||||
"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
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/logoove/sqlite"
|
||||
_ "github.com/logoove/sqlite" // use sql
|
||||
"github.com/sirupsen/logrus"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
@ -106,22 +106,22 @@ func init() {
|
||||
} else {
|
||||
tc := db.GetThirdCategory(firstIndex, secondIndex, thirdIndex)
|
||||
reg := regexp.MustCompile(regStr)
|
||||
recordUrl := tc.ThirdCategoryPath
|
||||
if recordUrl == "" {
|
||||
recURL := tc.ThirdCategoryPath
|
||||
if recURL == "" {
|
||||
ctx.SendChain(message.Reply(e.MessageID), message.Text("没有内容请重新选择,三次输入错误,指令可退出重输"))
|
||||
ctx.SendChain(message.Reply(e.MessageID), message.Text(db.GetAllFirstCategoryMessage()))
|
||||
errorCount++
|
||||
step = 1
|
||||
} else {
|
||||
if reg.MatchString(recordUrl) {
|
||||
if reg.MatchString(recURL) {
|
||||
// log.Println(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]))
|
||||
recordUrl = strings.ReplaceAll(recordUrl, "+", "%20")
|
||||
recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1]))
|
||||
recURL = strings.ReplaceAll(recURL, "+", "%20")
|
||||
// log.Println(recordUrl)
|
||||
}
|
||||
ctx.SendChain(message.Reply(e.MessageID), message.Text("请欣赏《"+tc.ThirdCategoryName+"》"))
|
||||
ctx.SendChain(message.Record(recordUrl))
|
||||
ctx.SendChain(message.Record(recURL))
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -145,16 +145,16 @@ func init() {
|
||||
fc := db.GetFirstCategoryByFirstUid(tc.FirstCategoryUid)
|
||||
if (tc != model.ThirdCategory{}) && (fc != model.FirstCategory{}) {
|
||||
reg := regexp.MustCompile(regStr)
|
||||
recordUrl := tc.ThirdCategoryPath
|
||||
if reg.MatchString(recordUrl) {
|
||||
recURL := tc.ThirdCategoryPath
|
||||
if reg.MatchString(recURL) {
|
||||
// log.Println(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]))
|
||||
recordUrl = strings.ReplaceAll(recordUrl, "+", "%20")
|
||||
recURL = strings.ReplaceAll(recURL, reg.FindStringSubmatch(recURL)[1], url.QueryEscape(reg.FindStringSubmatch(recURL)[1]))
|
||||
recURL = strings.ReplaceAll(recURL, "+", "%20")
|
||||
// log.Println(recordUrl)
|
||||
}
|
||||
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()
|
||||
})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user