🎨 改进代码结构

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
import (
"math/rand"
"time"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
)
var poke = rate.NewManager(time.Minute*5, 8) // 戳一戳
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().
Handle(func(ctx *zero.Ctx) {
@ -17,11 +33,11 @@ func init() { // 插件主体
case poke.Load(ctx.Event.UserID).AcquireN(3):
// 5分钟共8块命令牌 一次消耗3块命令牌
time.Sleep(time.Second * 1)
ctx.Send("请不要戳我 >_<")
ctx.SendChain(message.Text("请不要戳", NICKNAME, " >_<"))
case poke.Load(ctx.Event.UserID).Acquire():
// 5分钟共8块命令牌 一次消耗1块命令牌
time.Sleep(time.Second * 1)
ctx.Send("喂(#`O) 戳我干嘛!")
ctx.SendChain(message.Text("喂(#`O) 戳", NICKNAME, "干嘛!"))
default:
// 频繁触发,不回复
}

View File

@ -1,6 +1,7 @@
package github
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
@ -9,71 +10,81 @@ import (
"github.com/tidwall/gjson"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
func init() { // 插件主体
zero.OnRegex(`>G\s(.*)`).SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
api, _ := url.Parse("https://api.github.com/search/repositories")
params := url.Values{
"q": []string{ctx.State["regex_matched"].([]string)[1]},
// 发送请求
header := http.Header{
"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()
link := api.String()
api, _ := url.Parse("https://api.github.com/search/repositories")
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{}
req, err := http.NewRequest("GET", link, nil)
req, err := http.NewRequest("GET", dest, nil)
if err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
return nil, err
}
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)
if err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
return nil, err
}
if code := resp.StatusCode; code != 200 {
// 如果返回不是200则立刻抛出错误
ctx.Send(fmt.Sprintf("ERROR: code %d", code))
return
return nil, errors.New(fmt.Sprintf("code %d", code))
}
count := gjson.ParseBytes(body).Get("total_count").Int()
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: 可能被风控,发送失败")
}
})
return body, nil
}

View File

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

View File

@ -1,20 +1,20 @@
package manager
import (
"strconv"
"strings"
"github.com/Yiwen-Chan/ZeroBot-Plugin/manager/utils"
zero "github.com/wdvxdr1123/ZeroBot"
)
func init() { // 插件主体
// 菜单
zero.OnFullMatch("群管系统").SetBlock(true).SetPriority(40).
zero.OnFullMatch("群管系统", zero.AdminPermission).SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
ctx.Send(`====群管====
- 禁言@QQ 1
- 禁言@QQ 1分钟
- 解除禁言 @QQ
- 我要自闭 1
- 我要自闭 1分钟
- 开启全员禁言
- 解除全员禁言
- 升为管理@QQ
@ -33,12 +33,12 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupAdmin(
ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被升为管理的人的qq
strToInt(ctx.State["regex_matched"].([]string)[1]), // 被升为管理的人的qq
true,
)
nickname := ctx.GetGroupMemberInfo( // 被升为管理的人的昵称
ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被升为管理的人的qq
strToInt(ctx.State["regex_matched"].([]string)[1]), // 被升为管理的人的qq
false,
).Get("nickname").Str
ctx.Send(nickname + " 升为了管理~")
@ -49,12 +49,12 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupAdmin(
ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被取消管理的人的qq
strToInt(ctx.State["regex_matched"].([]string)[1]), // 被取消管理的人的qq
false,
)
nickname := ctx.GetGroupMemberInfo( // 被取消管理的人的昵称
ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被取消管理的人的qq
strToInt(ctx.State["regex_matched"].([]string)[1]), // 被取消管理的人的qq
false,
).Get("nickname").Str
ctx.Send("残念~ " + nickname + " 暂时失去了管理员的资格")
@ -65,12 +65,12 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupKick(
ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被踢出群聊的人的qq
strToInt(ctx.State["regex_matched"].([]string)[1]), // 被踢出群聊的人的qq
false,
)
nickname := ctx.GetGroupMemberInfo( // 被踢出群聊的人的昵称
ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 被踢出群聊的人的qq
strToInt(ctx.State["regex_matched"].([]string)[1]), // 被踢出群聊的人的qq
false,
).Get("nickname").Str
ctx.Send("残念~ " + nickname + " 被放逐")
@ -80,7 +80,7 @@ func init() { // 插件主体
zero.OnRegex(`^退出群聊.*?(\d+)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupLeave(
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 要退出的群的群号
strToInt(ctx.State["regex_matched"].([]string)[1]), // 要退出的群的群号
true,
)
return
@ -108,7 +108,7 @@ func init() { // 插件主体
// 禁言
zero.OnRegex(`^禁言.*?(\d+).*?\s(\d+)(.*)`, zero.OnlyGroup, zero.AdminPermission).SetBlock(true).SetPriority(40).
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] {
case "分钟":
//
@ -124,7 +124,7 @@ func init() { // 插件主体
}
ctx.SetGroupBan(
ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 要禁言的人的qq
strToInt(ctx.State["regex_matched"].([]string)[1]), // 要禁言的人的qq
duration*60, // 要禁言的时间(分钟)
)
ctx.Send("小黑屋收留成功~")
@ -135,7 +135,7 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupBan(
ctx.Event.GroupID,
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 要解除禁言的人的qq
strToInt(ctx.State["regex_matched"].([]string)[1]), // 要解除禁言的人的qq
0,
)
ctx.Send("小黑屋释放成功~")
@ -144,7 +144,7 @@ func init() { // 插件主体
// 自闭禁言
zero.OnRegex(`^我要自闭.*?(\d+)(.*)`, zero.OnlyGroup).SetBlock(true).SetPriority(40).
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] {
case "分钟":
//
@ -171,7 +171,7 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupCard(
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.Send("嗯!已经修改了")
@ -182,7 +182,7 @@ func init() { // 插件主体
Handle(func(ctx *zero.Ctx) {
ctx.SetGroupSpecialTitle(
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.Send("嗯!已经修改了")
@ -207,7 +207,7 @@ func init() { // 插件主体
content = strings.ReplaceAll(content, "&#91;", "[")
content = strings.ReplaceAll(content, "&#93;", "]")
ctx.SendGroupMessage(
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 需要发送的群
strToInt(ctx.State["regex_matched"].([]string)[1]), // 需要发送的群
content, // 需要发送的信息
)
ctx.Send("📧 --> " + ctx.State["regex_matched"].([]string)[1])
@ -221,7 +221,7 @@ func init() { // 插件主体
content = strings.ReplaceAll(content, "&#91;", "[")
content = strings.ReplaceAll(content, "&#93;", "]")
ctx.SendPrivateMessage(
utils.Str2Int(ctx.State["regex_matched"].([]string)[1]), // 需要发送的人的qq
strToInt(ctx.State["regex_matched"].([]string)[1]), // 需要发送的人的qq
content, // 需要发送的信息
)
ctx.Send("📧 --> " + ctx.State["regex_matched"].([]string)[1])
@ -252,3 +252,8 @@ func init() { // 插件主体
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"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
)
var limit = rate.NewManager(time.Minute*3, 5)
func init() {
zero.OnRegex("^酷我点歌(.+?)$").SetBlock(true).FirstPriority().
zero.OnRegex("^(.{0,2})点歌(.{1,25})$").SetBlock(true).FirstPriority().
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
})
zero.OnRegex("^酷狗点歌(.+?)$").SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
ctx.SendChain(kugou(ctx.State["regex_matched"].([]string)[1]))
return
})
zero.OnRegex("^网易点歌(.+?)$").SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
ctx.SendChain(cloud163(ctx.State["regex_matched"].([]string)[1]))
return
})
zero.OnRegex("^点歌(.+?)$").SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
ctx.SendChain(qqmusic(ctx.State["regex_matched"].([]string)[1]))
}
// switch 平台
switch ctx.State["regex_matched"].([]string)[1] {
case "酷我":
ctx.SendChain(kuwo(ctx.State["regex_matched"].([]string)[2]))
case "酷狗":
ctx.SendChain(kugou(ctx.State["regex_matched"].([]string)[2]))
case "网易":
ctx.SendChain(cloud163(ctx.State["regex_matched"].([]string)[2]))
default: // 默认 QQ音乐
ctx.SendChain(qqmusic(ctx.State["regex_matched"].([]string)[2]))
}
return
})
}

View File

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