mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-13 02:30:26 +00:00
feat:添加抽象话插件 (#92)
This commit is contained in:
41
plugin_chouxianghua/chouxianghua.go
Normal file
41
plugin_chouxianghua/chouxianghua.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package chouxianghua
|
||||
|
||||
import (
|
||||
"github.com/FloatTech/ZeroBot-Plugin/control"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
)
|
||||
|
||||
const (
|
||||
prio = 10
|
||||
)
|
||||
|
||||
var (
|
||||
engine = control.Register("chouxianghua", &control.Options{
|
||||
DisableOnDefault: false,
|
||||
Help: "抽象话\n- 抽象翻译\n",
|
||||
})
|
||||
)
|
||||
|
||||
func init() {
|
||||
engine.OnRegex("^抽象翻译([\u4E00-\u9FA5A-Za-z0-9]{1,25})$").SetBlock(true).SetPriority(prio).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
cxresult := chouXiang(ctx.State["regex_matched"].([]string)[1])
|
||||
ctx.SendChain(message.Text(cxresult))
|
||||
})
|
||||
}
|
||||
|
||||
func chouXiang(s string) (cxresult 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])
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
35
plugin_chouxianghua/data.go
Normal file
35
plugin_chouxianghua/data.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package chouxianghua
|
||||
|
||||
import (
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/process"
|
||||
"github.com/FloatTech/ZeroBot-Plugin/utils/sql"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
dbpath = "data/ChouXiangHua/"
|
||||
dbfile = dbpath + "cxh.db"
|
||||
)
|
||||
|
||||
var db = &sql.Sqlite{DBPath: dbfile}
|
||||
|
||||
// 加载数据库
|
||||
func init() {
|
||||
go func() {
|
||||
process.SleepAbout1sTo2s()
|
||||
// os.RemoveAll(dbpath)
|
||||
_ = os.MkdirAll(dbpath, 0755)
|
||||
_, _ = file.GetLazyData(dbfile, false, true)
|
||||
err := db.Create("pinyin", &Pinyin{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
n, err := db.Count("pinyin")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log.Printf("[chouxianghua]读取%d条拼音", n)
|
||||
}()
|
||||
}
|
||||
20
plugin_chouxianghua/model.go
Normal file
20
plugin_chouxianghua/model.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package chouxianghua
|
||||
|
||||
type Pinyin struct {
|
||||
Word string `db:"word"`
|
||||
Pronunciation string `db:"pronunciation"`
|
||||
}
|
||||
type Emoji struct {
|
||||
Pronunciation string `db:"pronunciation"`
|
||||
Emoji string `db:"emoji"`
|
||||
}
|
||||
|
||||
func getPronunciationByWord(word string) (p Pinyin) {
|
||||
db.Find("pinyin", &p, "where word = '"+word+"'")
|
||||
return
|
||||
}
|
||||
|
||||
func getEmojiByPronunciation(pronunciation string) (e Emoji) {
|
||||
db.Find("emoji", &e, "where pronunciation = '"+pronunciation+"'")
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user