-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.go
130 lines (108 loc) · 2.64 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package tfcc
import (
"fmt"
. "github.com/CuteReimu/onebot"
"math/rand/v2"
"slices"
"strconv"
"strings"
)
func init() {
addCmdListener(&showTips{})
addCmdListener(&ping{})
addCmdListener(&roll{})
addCmdListener(&randDraw{})
}
type showTips struct{}
func (t *showTips) Name() string {
return "查看帮助"
}
func (t *showTips) ShowTips(int64, int64) string {
return ""
}
func (t *showTips) CheckAuth(int64, int64) bool {
return true
}
func (t *showTips) Execute(msg *GroupMessage, _ string) MessageChain {
var ret []string
for _, h := range cmdMap {
if h.CheckAuth(msg.GroupId, msg.Sender.UserId) {
if tip := h.ShowTips(msg.GroupId, msg.Sender.UserId); len(tip) > 0 {
ret = append(ret, tip)
}
}
}
slices.Sort(ret)
return MessageChain{&Text{Text: "你可以使用以下功能:\n" + strings.Join(ret, "\n")}}
}
type ping struct{}
func (p *ping) Name() string {
return "ping"
}
func (p *ping) ShowTips(int64, int64) string {
return ""
}
func (p *ping) CheckAuth(int64, int64) bool {
return true
}
func (p *ping) Execute(_ *GroupMessage, content string) MessageChain {
if len(content) == 0 {
return MessageChain{&Text{Text: "pong"}}
}
return nil
}
type roll struct{}
func (r *roll) Name() string {
return "roll"
}
func (r *roll) ShowTips(int64, int64) string {
return ""
}
func (r *roll) CheckAuth(int64, int64) bool {
return true
}
func (r *roll) Execute(message *GroupMessage, content string) MessageChain {
if len(content) == 0 {
replyGroupMessage(true, message, &Text{Text: "roll: " + strconv.Itoa(rand.IntN(100))})
}
return nil
}
type randDraw struct{}
func (r *randDraw) Name() string {
return "抽签"
}
func (r *randDraw) ShowTips(int64, int64) string {
return "抽签 选手数量"
}
func (r *randDraw) CheckAuth(_ int64, senderId int64) bool {
return IsAdmin(senderId)
}
func (r *randDraw) Execute(_ *GroupMessage, content string) MessageChain {
count, err := strconv.Atoi(content)
if err != nil {
return MessageChain{&Text{Text: "指令格式如下:\n抽签 选手数量"}}
}
if count%2 != 0 {
return MessageChain{&Text{Text: "选手数量必须为偶数"}}
}
if count > 32 {
return MessageChain{&Text{Text: "选手数量太多"}}
}
if count <= 0 {
return MessageChain{&Text{Text: "选手数量太少"}}
}
a := make([]int, 0, count)
for i := count; i > 0; i-- {
a = append(a, i)
}
ret := make([]string, 0, count/2)
for i := 0; i < count/2; i++ {
a1 := a[len(a)-1]
a = a[:len(a)-1]
index := rand.IntN(len(a))
a2 := a[index]
a = append(a[:index], a[index+1:]...)
ret = append(ret, fmt.Sprintf("%d号 对阵 %d号", a1, a2))
}
return MessageChain{&Text{Text: strings.Join(ret, "\n")}}
}