This repository has been archived by the owner on Dec 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
248 lines (212 loc) · 7.14 KB
/
commands.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package commands
import (
"context"
"fmt"
"github.com/PaulekOfficial/RocketDiscord/cache"
"github.com/andersfylling/disgord"
"github.com/sirupsen/logrus"
"os"
"strings"
)
var (
availableCommands = make(map[string]Command)
)
var Log = &logrus.Logger{
Out: os.Stderr,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}
type Command struct {
Name string
HelpMessage string
GuildOwnerOnly bool
RequirePermissions bool
PermissionsLevel disgord.PermissionBit
RequireArguments bool
MinimumArgumentsSize int
MaximumArgumentsSize int
NSFW bool
Exec func(disgord.Session, *disgord.MessageCreate, *disgord.Guild, []string) error
}
type GuildCaller struct {
session disgord.Session
guildID disgord.Snowflake
}
func (caller *GuildCaller) Guild(id disgord.Snowflake) disgord.GuildQueryBuilder {
return caller.session.Guild(id)
}
func NewCommand(name string, requirePermissions bool, nsfw bool, f func(disgord.Session, *disgord.MessageCreate, *disgord.Guild, []string) error) Command {
if f == nil {
panic("function is nil")
}
return Command{
Name: name,
RequirePermissions: requirePermissions,
NSFW: nsfw,
Exec: f,
}
}
func (command *Command) SetPermissions(requirePermissions bool, guildOwnerOnly bool, permissionsLevel disgord.PermissionBit) {
command.GuildOwnerOnly = guildOwnerOnly
command.RequirePermissions = requirePermissions
command.PermissionsLevel = permissionsLevel
}
//If MinimumArgumentsSize is set to -1 that means no limit
//If MinimumArgumentsSize is set to -1 that means no limit
func (command *Command) SetArgumentsRequirements(requireArguments bool, minimumArgumentsSize int, maximumArgumentsSize int) {
command.RequireArguments = requireArguments
command.MinimumArgumentsSize = minimumArgumentsSize
command.MaximumArgumentsSize = maximumArgumentsSize
}
func (command *Command) SetHelpMessage(message string) {
command.HelpMessage = message
}
func (command *Command) Register() Command {
availableCommands[command.Name] = *command
Log.WithFields(logrus.Fields{
"command-name": command.Name,
}).Info("Registered new command")
return *command
}
func ParseMessage(session disgord.Session, event *disgord.MessageCreate, guild *disgord.Guild, message string) {
guildSettings := cache.CreateOrGetGuildSettings(guild)
commandPrefix := guildSettings.CommandPrefix
//Check if message contains command prefix
if !strings.HasPrefix(message, commandPrefix) {
return
}
//Parse to arguments
args := strings.Fields(message)
//Return if no args provided
if len(args) <= 0 {
return
}
//Parse command name
commandName := strings.Replace(args[0], commandPrefix, "", 1)
//Now we check if everything is clear to lift off
command, ok := availableCommands[commandName]
if !ok {
return
}
//Load channel to memory
channel := session.Channel(event.Message.ChannelID)
//Check guild member permissions
channelID := event.Message.ChannelID
memberID := event.Message.Member.UserID
permissionLevel, err := channelPermissionLevel(session, channelID, memberID, guild.ID)
if err != nil {
Log.WithFields(logrus.Fields{
"member-memberId": memberID,
"member-name": event.Message.Member.Nick,
"guild-id": event.Message.GuildID.String(),
"guild-name": guild.Name,
"content": event.Message.Content,
"command-name": commandName,
}).WithError(err).Error("Could not get user permissions level")
return
}
if (command.GuildOwnerOnly && memberID.String() != guild.OwnerID.String()) || (command.RequirePermissions && permissionLevel&command.PermissionsLevel <= 0) {
_, err := session.SendMsg(channelID, fmt.Sprintf(":rotating_light: Brak wystarczających uprawnień do użycia tego polecenia! Wymagany poziom dostępu %d.", command.PermissionsLevel))
if err != nil {
Log.WithFields(logrus.Fields{
"member-memberId": memberID,
"member-name": event.Message.Member.Nick,
"guild-id": event.Message.GuildID.String(),
"guild-name": guild.Name,
"content": event.Message.Content,
"command-name": commandName,
}).WithError(err).Error("Could not send no permissions warning")
return
}
return
}
//Arguments check
length := len(args[1:])
if (length > command.MaximumArgumentsSize && command.MaximumArgumentsSize != -1) || (length < command.MinimumArgumentsSize && command.MinimumArgumentsSize != -1) {
_, err := session.SendMsg(channelID, fmt.Sprintf(":boom: Niepoprawne użycie polecenia %s.", commandName))
if err != nil {
Log.WithFields(logrus.Fields{
"member-memberId": memberID,
"member-name": event.Message.Member.Nick,
"guild-id": event.Message.GuildID.String(),
"guild-name": guild.Name,
"content": event.Message.Content,
"command-name": commandName,
}).WithError(err).Error("Could send help message usage help")
return
}
_, err = session.SendMsg(channelID, fmt.Sprintf(":star: Wyświetlam pomoc dla polecenia %s: %s", commandName, command.HelpMessage))
if err != nil {
Log.WithFields(logrus.Fields{
"member-memberId": memberID,
"member-name": event.Message.Member.Nick,
"guild-id": event.Message.GuildID.String(),
"guild-name": guild.Name,
"content": event.Message.Content,
"command-name": commandName,
}).WithError(err).Error("Could send help message usage")
return
}
return
}
//Check if command is executed on nsfw channel
pureChannel, err := channel.Get()
if err != nil {
return
}
if !pureChannel.NSFW && command.NSFW {
_, err := session.SendMsg(channelID, ":no_entry: Polecienie __**MOŻE**__ być tylko wykonane na kanale __**NSFW**__!")
if err != nil {
Log.WithFields(logrus.Fields{
"member-memberId": memberID,
"member-name": event.Message.Member.Nick,
"guild-id": event.Message.GuildID.String(),
"guild-name": guild.Name,
"content": event.Message.Content,
"command-name": commandName,
}).WithError(err).Error("Could not check nsfw channel status")
return
}
return
}
//Logs
Log.WithFields(logrus.Fields{
"member-memberId": memberID,
"member-name": event.Message.Member.Nick,
"guild-id": event.Message.GuildID.String(),
"guild-name": guild.Name,
"content": event.Message.Content,
"command-name": commandName,
}).Debug("Executing command")
//Execute
err = command.Exec(session, event, guild, args[1:])
if err != nil {
Log.WithFields(logrus.Fields{
"member-memberId": memberID,
"member-name": event.Message.Member.Nick,
"guild-id": event.Message.GuildID.String(),
"guild-name": guild.Name,
"content": event.Message.Content,
"command-name": commandName,
}).WithError(err).Error("Command general error")
return
}
}
func channelPermissionLevel(session disgord.Session, channelID disgord.Snowflake, memberID disgord.Snowflake, guildID disgord.Snowflake) (permissionLevel disgord.PermissionBit, err error) {
//Get pure channel object
channel, err := session.Channel(channelID).Get()
if err != nil {
return
}
guild := session.Guild(guildID)
member, err := guild.Member(memberID).Get()
if err != nil {
return
}
permissionLevel, err = channel.GetPermissions(context.TODO(), &GuildCaller{
session: session,
guildID: guildID,
}, member)
return
}