🎨 改进代码结构

This commit is contained in:
Yiwen-Chan 2021-04-12 18:48:12 +08:00
parent 45a2808c13
commit f19721a683
7 changed files with 132 additions and 116 deletions

View File

@ -1,15 +1,31 @@
package chat package chat
import ( import (
"math/rand"
"time" "time"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate" "github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
) )
var poke = rate.NewManager(time.Minute*5, 8) // 戳一戳 var poke = rate.NewManager(time.Minute*5, 8) // 戳一戳
func init() { // 插件主体 func init() { // 插件主体
var NICKNAME = zero.BotConfig.NickName[0]
// 被喊名字
zero.OnFullMatchGroup(zero.BotConfig.NickName).SetBlock(false).FirstPriority().
Handle(func(ctx *zero.Ctx) {
time.Sleep(time.Second * 1)
ctx.SendChain(message.Text(
[]string{
NICKNAME + "在此,有何贵干~",
"(っ●ω●)っ在~",
"这里是" + NICKNAME + "(っ●ω●)っ",
NICKNAME + "不在呢~",
}[rand.Intn(4)],
))
})
// 戳一戳 // 戳一戳
zero.On("notice/notify/poke", zero.OnlyToMe).SetBlock(false).FirstPriority(). zero.On("notice/notify/poke", zero.OnlyToMe).SetBlock(false).FirstPriority().
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
@ -17,11 +33,11 @@ func init() { // 插件主体
case poke.Load(ctx.Event.UserID).AcquireN(3): case poke.Load(ctx.Event.UserID).AcquireN(3):
// 5分钟共8块命令牌 一次消耗3块命令牌 // 5分钟共8块命令牌 一次消耗3块命令牌
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
ctx.Send("请不要戳我 >_<") ctx.SendChain(message.Text("请不要戳", NICKNAME, " >_<"))
case poke.Load(ctx.Event.UserID).Acquire(): case poke.Load(ctx.Event.UserID).Acquire():
// 5分钟共8块命令牌 一次消耗1块命令牌 // 5分钟共8块命令牌 一次消耗1块命令牌
time.Sleep(time.Second * 1) time.Sleep(time.Second * 1)
ctx.Send("喂(#`O) 戳我干嘛!") ctx.SendChain(message.Text("喂(#`O) 戳", NICKNAME, "干嘛!"))
default: default:
// 频繁触发,不回复 // 频繁触发,不回复
} }

View File

@ -1,6 +1,7 @@
package github package github
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -9,71 +10,81 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
) )
func init() { // 插件主体 func init() { // 插件主体
zero.OnRegex(`>G\s(.*)`).SetBlock(true).FirstPriority(). zero.OnRegex(`>G\s(.*)`).SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
api, _ := url.Parse("https://api.github.com/search/repositories") // 发送请求
params := url.Values{ header := http.Header{
"q": []string{ctx.State["regex_matched"].([]string)[1]}, "User-Agent": []string{"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"},
} }
api.RawQuery = params.Encode() api, _ := url.Parse("https://api.github.com/search/repositories")
link := api.String() api.RawQuery = url.Values{
"q": []string{ctx.State["regex_matched"].([]string)[1]},
}.Encode()
body, err := netGet(api.String(), header)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
}
// 解析请求
info := gjson.ParseBytes(body)
if info.Get("total_count").Int() == 0 {
ctx.SendChain(message.Text("ERROR: 没有找到这样的仓库"))
return
}
repo := info.Get("items.0")
// 发送结果
ctx.SendChain(
message.Text(
repo.Get("full_name").Str, "\n",
"Description: ",
repo.Get("description").Str, "\n",
"Star/Fork/Issue: ",
repo.Get("watchers").Int(), "/", repo.Get("forks").Int(), "/", repo.Get("open_issues").Int(), "\n",
"Language: ",
notnull(repo.Get("language").Str, "None"), "\n",
"License: ",
notnull(strings.ToUpper(repo.Get("license.key").Str), "None"), "\n",
"Last pushed: ",
repo.Get("pushed_at").Str, "\n",
"Jump: ",
repo.Get("html_url").Str, "\n",
),
)
})
}
// notnull 如果传入文本为空,则返回默认值
func notnull(text, default_ string) string {
if text == "" {
return default_
}
return text
}
// netGet 返回请求结果
func netGet(dest string, header http.Header) ([]byte, error) {
client := &http.Client{} client := &http.Client{}
req, err := http.NewRequest("GET", link, nil) req, err := http.NewRequest("GET", dest, nil)
if err != nil { if err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err)) return nil, err
return
} }
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36") req.Header = header
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err)) return nil, err
return
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err)) return nil, err
return
} }
if code := resp.StatusCode; code != 200 { if code := resp.StatusCode; code != 200 {
// 如果返回不是200则立刻抛出错误 // 如果返回不是200则立刻抛出错误
ctx.Send(fmt.Sprintf("ERROR: code %d", code)) return nil, errors.New(fmt.Sprintf("code %d", code))
return
} }
count := gjson.ParseBytes(body).Get("total_count").Int() return body, nil
if count == 0 {
ctx.Send("没有找到这样的仓库")
return
}
repo := gjson.ParseBytes(body).Get("items.0")
language := repo.Get("language").Str
if language == "" {
language = "None"
}
license := strings.ToUpper(repo.Get("license.key").Str)
if license == "" {
license = "None"
}
id := ctx.Send(fmt.Sprintf(
"%s: \nDescription: %s\nStar/Fork/Issue: %d/%d/%d\nLanguage: %s\nLicense: %s\nLast pushed: %s\nJump: %s",
repo.Get("full_name").Str,
repo.Get("description").Str,
repo.Get("watchers").Int(),
repo.Get("forks").Int(),
repo.Get("open_issues").Int(),
language,
license,
repo.Get("updated_at").Str,
repo.Get("html_url").Str,
))
if id == 0 {
ctx.Send("ERROR: 可能被风控,发送失败")
}
})
} }

View File

@ -35,7 +35,7 @@ func main() {
======================================================== ========================================================
`) `)
zero.Run(zero.Config{ zero.Run(zero.Config{
NickName: []string{"bot"}, NickName: []string{"椛椛"},
CommandPrefix: "/", CommandPrefix: "/",
SuperUsers: []string{"825111790", "213864964"}, // 必须修改,否则无权限 SuperUsers: []string{"825111790", "213864964"}, // 必须修改,否则无权限
Driver: []zero.Driver{ Driver: []zero.Driver{

View File

@ -1,20 +1,20 @@
package manager package manager
import ( import (
"strconv"
"strings" "strings"
"github.com/Yiwen-Chan/ZeroBot-Plugin/manager/utils"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
) )
func init() { // 插件主体 func init() { // 插件主体
// 菜单 // 菜单
zero.OnFullMatch("群管系统").SetBlock(true).SetPriority(40). zero.OnFullMatch("群管系统", zero.AdminPermission).SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.Send(`====群管==== ctx.Send(`====群管====
- 禁言@QQ 1 - 禁言@QQ 1分钟
- 解除禁言 @QQ - 解除禁言 @QQ
- 我要自闭 1 - 我要自闭 1分钟
- 开启全员禁言 - 开启全员禁言
- 解除全员禁言 - 解除全员禁言
- 升为管理@QQ - 升为管理@QQ
@ -33,12 +33,12 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SetGroupAdmin( ctx.SetGroupAdmin(
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被升为管理的人的qq strToInt(ctx.State["regex_matched"].([]string)[1]), // 被升为管理的人的qq
true, true,
) )
nickname := ctx.GetGroupMemberInfo( // 被升为管理的人的昵称 nickname := ctx.GetGroupMemberInfo( // 被升为管理的人的昵称
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被升为管理的人的qq strToInt(ctx.State["regex_matched"].([]string)[1]), // 被升为管理的人的qq
false, false,
).Get("nickname").Str ).Get("nickname").Str
ctx.Send(nickname + " 升为了管理~") ctx.Send(nickname + " 升为了管理~")
@ -49,12 +49,12 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SetGroupAdmin( ctx.SetGroupAdmin(
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被取消管理的人的qq strToInt(ctx.State["regex_matched"].([]string)[1]), // 被取消管理的人的qq
false, false,
) )
nickname := ctx.GetGroupMemberInfo( // 被取消管理的人的昵称 nickname := ctx.GetGroupMemberInfo( // 被取消管理的人的昵称
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被取消管理的人的qq strToInt(ctx.State["regex_matched"].([]string)[1]), // 被取消管理的人的qq
false, false,
).Get("nickname").Str ).Get("nickname").Str
ctx.Send("残念~ " + nickname + " 暂时失去了管理员的资格") ctx.Send("残念~ " + nickname + " 暂时失去了管理员的资格")
@ -65,12 +65,12 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SetGroupKick( ctx.SetGroupKick(
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被踢出群聊的人的qq strToInt(ctx.State["regex_matched"].([]string)[1]), // 被踢出群聊的人的qq
false, false,
) )
nickname := ctx.GetGroupMemberInfo( // 被踢出群聊的人的昵称 nickname := ctx.GetGroupMemberInfo( // 被踢出群聊的人的昵称
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被踢出群聊的人的qq strToInt(ctx.State["regex_matched"].([]string)[1]), // 被踢出群聊的人的qq
false, false,
).Get("nickname").Str ).Get("nickname").Str
ctx.Send("残念~ " + nickname + " 被放逐") ctx.Send("残念~ " + nickname + " 被放逐")
@ -80,7 +80,7 @@ func init() { // 插件主体
zero.OnRegex(`^退出群聊.*?(\d+)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40). zero.OnRegex(`^退出群聊.*?(\d+)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SetGroupLeave( ctx.SetGroupLeave(
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 要退出的群的群号 strToInt(ctx.State["regex_matched"].([]string)[1]), // 要退出的群的群号
true, true,
) )
return return
@ -108,7 +108,7 @@ func init() { // 插件主体
// 禁言 // 禁言
zero.OnRegex(`^禁言.*?(\d+).*?\s(\d+)(.*)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40). zero.OnRegex(`^禁言.*?(\d+).*?\s(\d+)(.*)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
duration := utils.Str2Int(ctx.State["regex_matched"].([]string)[2]) duration := strToInt(ctx.State["regex_matched"].([]string)[2])
switch ctx.State["regex_matched"].([]string)[3] { switch ctx.State["regex_matched"].([]string)[3] {
case "分钟": case "分钟":
// //
@ -124,7 +124,7 @@ func init() { // 插件主体
} }
ctx.SetGroupBan( ctx.SetGroupBan(
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 要禁言的人的qq strToInt(ctx.State["regex_matched"].([]string)[1]), // 要禁言的人的qq
duration*60, // 要禁言的时间(分钟) duration*60, // 要禁言的时间(分钟)
) )
ctx.Send("小黑屋收留成功~") ctx.Send("小黑屋收留成功~")
@ -135,7 +135,7 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SetGroupBan( ctx.SetGroupBan(
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 要解除禁言的人的qq strToInt(ctx.State["regex_matched"].([]string)[1]), // 要解除禁言的人的qq
0, 0,
) )
ctx.Send("小黑屋释放成功~") ctx.Send("小黑屋释放成功~")
@ -144,7 +144,7 @@ func init() { // 插件主体
// 自闭禁言 // 自闭禁言
zero.OnRegex(`^我要自闭.*?(\d+)(.*)`, zero.OnlyGroup).SetBlock(true).SetPriority(40). zero.OnRegex(`^我要自闭.*?(\d+)(.*)`, zero.OnlyGroup).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
duration := utils.Str2Int(ctx.State["regex_matched"].([]string)[1]) duration := strToInt(ctx.State["regex_matched"].([]string)[1])
switch ctx.State["regex_matched"].([]string)[2] { switch ctx.State["regex_matched"].([]string)[2] {
case "分钟": case "分钟":
// //
@ -171,7 +171,7 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SetGroupCard( ctx.SetGroupCard(
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被修改群名片的人 strToInt(ctx.State["regex_matched"].([]string)[1]), // 被修改群名片的人
ctx.State["regex_matched"].([]string)[2], // 修改成的群名片 ctx.State["regex_matched"].([]string)[2], // 修改成的群名片
) )
ctx.Send("嗯!已经修改了") ctx.Send("嗯!已经修改了")
@ -182,7 +182,7 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SetGroupSpecialTitle( ctx.SetGroupSpecialTitle(
ctx.Event.GroupID, ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被修改群头衔的人 strToInt(ctx.State["regex_matched"].([]string)[1]), // 被修改群头衔的人
ctx.State["regex_matched"].([]string)[2], // 修改成的群头衔 ctx.State["regex_matched"].([]string)[2], // 修改成的群头衔
) )
ctx.Send("嗯!已经修改了") ctx.Send("嗯!已经修改了")
@ -207,7 +207,7 @@ func init() { // 插件主体
content = strings.ReplaceAll(content, "&#91;", "[") content = strings.ReplaceAll(content, "&#91;", "[")
content = strings.ReplaceAll(content, "&#93;", "]") content = strings.ReplaceAll(content, "&#93;", "]")
ctx.SendGroupMessage( ctx.SendGroupMessage(
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 需要发送的群 strToInt(ctx.State["regex_matched"].([]string)[1]), // 需要发送的群
content, // 需要发送的信息 content, // 需要发送的信息
) )
ctx.Send("📧 --> " + ctx.State["regex_matched"].([]string)[1]) ctx.Send("📧 --> " + ctx.State["regex_matched"].([]string)[1])
@ -221,7 +221,7 @@ func init() { // 插件主体
content = strings.ReplaceAll(content, "&#91;", "[") content = strings.ReplaceAll(content, "&#91;", "[")
content = strings.ReplaceAll(content, "&#93;", "]") content = strings.ReplaceAll(content, "&#93;", "]")
ctx.SendPrivateMessage( ctx.SendPrivateMessage(
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 需要发送的人的qq strToInt(ctx.State["regex_matched"].([]string)[1]), // 需要发送的人的qq
content, // 需要发送的信息 content, // 需要发送的信息
) )
ctx.Send("📧 --> " + ctx.State["regex_matched"].([]string)[1]) ctx.Send("📧 --> " + ctx.State["regex_matched"].([]string)[1])
@ -252,3 +252,8 @@ func init() { // 插件主体
ctx.Send(cmd) ctx.Send(cmd)
}) })
} }
func strToInt(str string) int64 {
val, _ := strconv.ParseInt(str, 10, 64)
return val
}

View File

@ -1,15 +0,0 @@
package utils
import (
"strconv"
)
func Int2Str(val int64) string {
str := strconv.FormatInt(val, 10)
return str
}
func Str2Int(str string) int64 {
val, _ := strconv.ParseInt(str, 10, 64)
return val
}

View File

@ -12,31 +12,30 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
) )
var limit = rate.NewManager(time.Minute*3, 5)
func init() { func init() {
zero.OnRegex("^酷我点歌(.+?)$").SetBlock(true).FirstPriority(). zero.OnRegex("^(.{0,2})点歌(.{1,25})$").SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
ctx.SendChain(kuwo(ctx.State["regex_matched"].([]string)[1])) if !limit.Load(ctx.Event.UserID).Acquire() {
ctx.Send("请稍后重试0x0...")
return return
}) }
// switch 平台
zero.OnRegex("^酷狗点歌(.+?)$").SetBlock(true).FirstPriority(). switch ctx.State["regex_matched"].([]string)[1] {
Handle(func(ctx *zero.Ctx) { case "酷我":
ctx.SendChain(kugou(ctx.State["regex_matched"].([]string)[1])) ctx.SendChain(kuwo(ctx.State["regex_matched"].([]string)[2]))
return case "酷狗":
}) ctx.SendChain(kugou(ctx.State["regex_matched"].([]string)[2]))
case "网易":
zero.OnRegex("^网易点歌(.+?)$").SetBlock(true).FirstPriority(). ctx.SendChain(cloud163(ctx.State["regex_matched"].([]string)[2]))
Handle(func(ctx *zero.Ctx) { default: // 默认 QQ音乐
ctx.SendChain(cloud163(ctx.State["regex_matched"].([]string)[1])) ctx.SendChain(qqmusic(ctx.State["regex_matched"].([]string)[2]))
return }
})
zero.OnRegex("^点歌(.+?)$").SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
ctx.SendChain(qqmusic(ctx.State["regex_matched"].([]string)[1]))
return return
}) })
} }

View File

@ -45,7 +45,7 @@ func init() {
func init() { // 插件主体 func init() { // 插件主体
zero.OnRegex(`^来份(.*)$`, FirstValueInList(PoolList)).SetBlock(true).SetPriority(20). zero.OnRegex(`^来份(.*)$`, FirstValueInList(PoolList)).SetBlock(true).SetPriority(20).
Handle(func(ctx *zero.Ctx) { Handle(func(ctx *zero.Ctx) {
if limit.Load(ctx.Event.UserID).Acquire() == false { if !limit.Load(ctx.Event.UserID).Acquire() {
ctx.Send("请稍后重试0x0...") ctx.Send("请稍后重试0x0...")
return return
} }