From dd32aec4625caa8951efab55a1867fe31aaaa4f6 Mon Sep 17 00:00:00 2001 From: fumiama Date: Thu, 28 Oct 2021 17:05:59 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=96=B0=E5=A2=9E=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E7=BF=BB=E8=AF=91=20by=20@sky-rainy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ main.go | 9 +++--- plugin_translation/tl.go | 63 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 plugin_translation/tl.go diff --git a/README.md b/README.md index 70211a89..f1fb6e99 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,8 @@ zerobot -h -t token -u url [-d|w] [-g] qq1 qq2 qq3 ... - **投胎** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_reborn"` - [x] reborn - 注:本插件来源于[tgbot](https://github.com/YukariChiba/tgbot/blob/main/modules/Reborn.py) +- **翻译** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_translation"` + - [x] >TL 你好 - **TODO...** ## 使用方法 diff --git a/main.go b/main.go index 44f2af56..f2eefb5a 100644 --- a/main.go +++ b/main.go @@ -18,10 +18,11 @@ import ( _ "github.com/FloatTech/ZeroBot-Plugin/plugin_qingyunke" // 青云客 // 实用类 - _ "github.com/FloatTech/ZeroBot-Plugin/plugin_github" // 搜索GitHub仓库 - _ "github.com/FloatTech/ZeroBot-Plugin/plugin_manager" // 群管 - _ "github.com/FloatTech/ZeroBot-Plugin/plugin_nbnhhsh" // 拼音首字母缩写释义工具 - _ "github.com/FloatTech/ZeroBot-Plugin/plugin_runcode" // 在线运行代码 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_github" // 搜索GitHub仓库 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_manager" // 群管 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_nbnhhsh" // 拼音首字母缩写释义工具 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_runcode" // 在线运行代码 + _ "github.com/FloatTech/ZeroBot-Plugin/plugin_translation" // 在线运行代码 // 娱乐类 _ "github.com/FloatTech/ZeroBot-Plugin-Gif" // 制图 diff --git a/plugin_translation/tl.go b/plugin_translation/tl.go new file mode 100644 index 00000000..b7d846bd --- /dev/null +++ b/plugin_translation/tl.go @@ -0,0 +1,63 @@ +package translation + +import ( + "errors" + "fmt" + "io/ioutil" + "net/http" + "time" + + "github.com/tidwall/gjson" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/extension/rate" + "github.com/wdvxdr1123/ZeroBot/message" + + "github.com/FloatTech/ZeroBot-Plugin/control" +) + +var ( + prio = 100 + bucket = rate.NewManager(time.Minute, 20) // 接口回复 +) + +func tl(d string) ([]byte, error) { + url := "https://api.cloolc.club/fanyi?data=" + d + resp, err := http.Get(url) + if err != nil { + fmt.Println(err) + } + data, err := ioutil.ReadAll(resp.Body) + _ = resp.Body.Close() + if err != nil { + return nil, err + } + if code := resp.StatusCode; code != 200 { + // 如果返回不是200则立刻抛出错误 + errmsg := fmt.Sprintf("code %d", code) + return nil, errors.New(errmsg) + } + return data, err +} + +func init() { + control.Register("translation", &control.Options{ + DisableOnDefault: false, + Help: "翻译\n" + + ">TL 你好", + }).OnRegex(`^>TL\s(-.{1,10}? )?(.*)$`).SetBlock(true).SetPriority(prio). + Handle(func(ctx *zero.Ctx) { + if !bucket.Load(ctx.Event.UserID).Acquire() { + // 频繁触发,不回复 + return + } + msg := []string{ctx.State["regex_matched"].([]string)[2]} + rely, err := tl(msg[0]) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + } + info := gjson.ParseBytes(rely) + repo := info.Get("data.0") + time.Sleep(time.Second) + ctx.SendChain(message.Text(repo.Get("value.0"))) + }) +}