-
Notifications
You must be signed in to change notification settings - Fork 1
/
roletriggers.go
186 lines (166 loc) · 4.59 KB
/
roletriggers.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2020-2022, The OneBot Contributors. All rights reserved.
package main
import (
"strings"
"github.com/TheDiscordian/onebot/libs/discord"
"github.com/TheDiscordian/onebot/onelib"
)
const (
// NAME is same as filename, minus extension
NAME = "roletriggers"
// LONGNAME is what's presented to the user
LONGNAME = "Role Triggers Plugin"
// VERSION of the plugin
VERSION = "v0.0.0"
DB_TABLE = "roletriggers"
)
// Load returns the Plugin object.
func Load() onelib.Plugin {
return &RoleTriggersPlugin{
monitor: &onelib.Monitor{
OnMessageUpdate: handlereact,
},
}
}
func handlereact(sender onelib.Sender, msg onelib.Message) {
if sender.Protocol() != "discord" {
return
}
reaction := msg.Reaction()
if reaction == nil {
return
}
roleId, err := onelib.Db.GetString(DB_TABLE, string(msg.UUID())+"_"+reaction.Name)
if err != nil {
return // not found, just return
}
disLoc := sender.Location().(*discord.DiscordLocation)
if reaction.Added {
// add role
err = disLoc.Client.GuildMemberRoleAdd(string(disLoc.GuildID), string(sender.UUID()), roleId)
} else {
// remove role
err = disLoc.Client.GuildMemberRoleRemove(string(disLoc.GuildID), string(sender.UUID()), roleId)
}
if err != nil {
onelib.Error.Println("["+NAME+"] Error adding/removing role:", err)
}
}
func roleid(msg onelib.Message, sender onelib.Sender) {
if sender.Protocol() != "discord" {
return
}
txt := msg.Text()
if txt == "" || len(txt) < 5 || txt[:2] != "<@" {
return
}
// disLoc := sender.Location().(*discord.DiscordLocation)
roleId := txt[3 : len(txt)-1]
sender.Location().SendText("RoleID: " + roleId)
}
// !addtrigger msgID emojiName @role
func addtrigger(msg onelib.Message, sender onelib.Sender) {
if sender.Protocol() != "discord" {
return
}
if sender.UUID() != discord.DiscordAdminId {
return
}
txt := msg.Text()
if txt == "" || len(txt) < 9 {
return
}
splitTxt := strings.Split(txt, " ")
if len(splitTxt) != 3 {
return
}
// Get msg id
msgId := splitTxt[0]
// Get emoji name
if len(splitTxt[1]) < 1 {
return
}
var emojiName string
if splitTxt[1][0] == ':' {
emojiName = splitTxt[1][1 : len(splitTxt[1])-1]
} else if len(splitTxt[1]) > 5 && splitTxt[1][0] == '<' {
emojiName = strings.Split(splitTxt[1], ":")[1]
} else {
emojiName = splitTxt[1]
}
// Get role id
if len(splitTxt[2]) < 5 || splitTxt[2][:2] != "<@" {
return
}
roleId := splitTxt[2][3 : len(splitTxt[2])-1]
err := onelib.Db.PutString(DB_TABLE, msgId+"_"+emojiName, roleId)
if err != nil {
sender.Location().SendText("Failed to add trigger: " + err.Error())
return
}
sender.Location().SendText("Trigger added successfully!")
}
func removetrigger(msg onelib.Message, sender onelib.Sender) {
if sender.Protocol() != "discord" {
return
}
if sender.UUID() != discord.DiscordAdminId {
return
}
txt := msg.Text()
if txt == "" || len(txt) < 9 {
return
}
splitTxt := strings.Split(txt, " ")
if len(splitTxt) != 2 {
return
}
// Get msg id
msgId := splitTxt[0]
// Get emoji name
if len(splitTxt[1]) < 1 {
return
}
var emojiName string
if splitTxt[1][0] == ':' {
emojiName = splitTxt[1][1 : len(splitTxt[1])-1]
} else if len(splitTxt[1]) > 5 && splitTxt[1][0] == '<' {
emojiName = strings.Split(splitTxt[1], ":")[1]
} else {
emojiName = splitTxt[1]
}
_, err := onelib.Db.GetString(DB_TABLE, msgId+"_"+emojiName)
if err != nil {
sender.Location().SendText("Failed to find trigger: " + err.Error())
return
}
err = onelib.Db.Remove(DB_TABLE, msgId+"_"+emojiName)
if err != nil {
sender.Location().SendText("Failed to remove trigger: " + err.Error())
return
}
sender.Location().SendText("Trigger removed successfully!")
}
// RoleTriggersPlugin is an object for satisfying the Plugin interface.
type RoleTriggersPlugin struct {
monitor *onelib.Monitor
}
// Name returns the name of the plugin, usually the filename.
func (rt *RoleTriggersPlugin) Name() string {
return NAME
}
// LongName returns the display name of the plugin.
func (rt *RoleTriggersPlugin) LongName() string {
return LONGNAME
}
// Version returns the version of the plugin, usually in the format of "v0.0.0".
func (rt *RoleTriggersPlugin) Version() string {
return VERSION
}
// Implements returns a map of commands and monitor the plugin implements.
func (rt *RoleTriggersPlugin) Implements() (map[string]onelib.Command, *onelib.Monitor) {
return map[string]onelib.Command{"roleid": roleid, "addtrigger": addtrigger, "at": addtrigger, "removetrigger": removetrigger, "rt": removetrigger}, rt.monitor
}
// Remove is necessary to satisfy the Plugin interface, it does nothing.
func (rt *RoleTriggersPlugin) Remove() {
}