-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom-commands.js
303 lines (246 loc) · 10.1 KB
/
custom-commands.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const Discord = require('discord.js')
const mongo = require('./mongo')
const customCommandSchema = require('./schema/custom-command-schema.js')
const loadCommands = require('./commands/load-commands')
const commandBase = require('./commands/command-base')
const reply = require('./message-reply')
const commandsCache = {} //"name": obj
let ignoredUsers = [] //Just an array of IDs
module.exports.getIgnoredUsers = () => {
return ignoredUsers
}
module.exports.getCustomCommand = async (name) => {
console.log('RUNNING...')
name = name.toLowerCase()
const cachedValue = commandsCache[name]
if (cachedValue) { return cachedValue }
return await mongo().then(async (mongoose) => {
try {
console.log('Running findOne()')
const result = await customCommandSchema.findOne({
commandName: name
})
console.log('RESULT: ', result)
if (result) {
commandsCache[name] = result
console.log('RSULT FROM GET:', result)
return result
} else {
return false
}
} finally {
mongoose.connection.close()
}
})
}
module.exports.addCustomCommand = async (name, defaultResponse, aor, responses, customCommandType, channelIds) => { //Add message with embed object
name = name.toLowerCase()
const embed = defaultResponse.embed
return await mongo().then(async (mongoose) => {
try {
const addedDoc = await new customCommandSchema({
commandName: name,
defaultResponse: {
message: defaultResponse.message,
embed: embed ? {
title: embed.title,
description: embed.description,
hexColor: embed.color,
thumbnailURL: embed.thumbnail.url,
fields: embed.fields,
footer: {
text: embed.footer ? embed.footer.text : '',
iconURL: embed.footer ? embed.footer.iconURL : ''
}
} : undefined
},
amountOfResponses: aor,
responses,
customCommandType,
invalidChannelIds: channelIds[0],
validChannelIds: channelIds[1]
}).save()
commandsCache[name] = addedDoc
} finally {
mongoose.connection.close()
}
})
}
module.exports.editCustomCommand = async (originalName, name, defaultResponse, amountOfResponses, responses, channelIds) => {
originalName = originalName.toLowerCase()
name = name.toLowerCase()
const embed = defaultResponse ? defaultResponse.embed : null
return await mongo().then(async (mongoose) => {
try {
const newDoc = await customCommandSchema.findOneAndUpdate({
commandName: originalName
},
{
commandName: name,
defaultResponse: {
message: defaultResponse ? defaultResponse.message : '',
embed: embed ? {
title: embed.title,
description: embed.description,
hexColor: embed.color,
thumbnailURL: embed.thumbnail.url,
fields: embed.fields,
footer: {
text: embed.footer ? embed.footer.text : '',
iconURL: embed.footer ? embed.footer.iconURL : ''
}
} : undefined
},
amountOfResponses,
responses,
invalidChannelIds: channelIds ? channelIds[0] : [],
validChannelIds: channelIds ? channelIds[1] : []
},
{
upsert: true,
useFindAndModify: false,
new: true
}
)
delete commandsCache[originalName]
commandsCache[name] = newDoc
} finally {
mongoose.connection.close()
}
})
}
module.exports.deleteCustomCommand = async (name) => {
name = name.toLowerCase()
return await mongo().then(async (mongoose) => {
try {
console.log('Running deleteOne()')
await customCommandSchema.deleteOne({
commandName: name
}).then(() => {
console.log('Object deleted.')
if (commandsCache[name]) {
delete commandsCache[name]
}
}).catch(error => {
console.log(error)
})
} finally {
mongoose.connection.close()
}
})
}
module.exports.startup = async (bot) => {
return await mongo().then(async (mongoose) => {
try {
const allCommands = await customCommandSchema.find()
for (const command of allCommands) {
commandsCache[command.commandName] = command
console.log(`Custom command loaded &${command.commandName}`) //Use default prefix since this shows up in the global config
}
this.startListener(bot)
} finally {
mongoose.connection.close()
}
})
}
module.exports.startListener = async (bot) => {
const defaultCommands = loadCommands()
let commandNamesArray = []
for (const commandOption of defaultCommands) {
if (typeof (commandOption.commands) === 'string') {
commandNamesArray.push(commandOption.commands)
}
else {
for (const alias of commandOption.commands) {
commandNamesArray.push(alias)
}
}
}
console.log('ALL COMMANDS', commandNamesArray)
bot.on('message', async message => {
if (!message.member) { return } //NO DMs!
if (ignoredUsers.includes(message.member.user.id)) { return } //Very simple
if (message.member.user.bot) { return }
const prefix = getPrefix(message.guild.id)
const newContent = message.content
const arguments = newContent.split(/[ ]+/)
if (arguments.length > 1) { return }
if (newContent.toLowerCase().startsWith(`${prefix}`)) {
let cmdString = newContent.substring(1)
if (commandNamesArray.includes(cmdString)) { return }
let commandObject = await this.getCustomCommand(cmdString)
if (!commandObject) { //Command was not found
message.channel.send(new Discord.MessageEmbed({
title: 'That Command Does Not Exist',
description: "Need help? Type in " + `**${prefix}help**\n` + `Looking for a custom command? Use **${prefix}help commands** or **${prefix}commands**`,
color: 'RED'
}))
return
}
if (commandObject.invalidChannelIds && commandObject.invalidChannelIds.length !== 0) { //We have channels that are invalid and the mode is set to invalid channels
if (commandObject.invalidChannelIds.includes(message.channel.id)) { //Sent in an invalid channel
reply.replyExclaim(message, 'That command is disabled in this channel.')
return
}
}
else if (commandObject.validChannelIds && commandObject.validChannelIds.length !== 0) { //We only have a limited amount of valid channels and the mode is valid channels
if (!(commandObject.validChannelIds.includes(message.channel.id))) { //The channel we are in isn't a valid one
reply.replyExclaim(message, 'That command is disabled in this channel.')
return
}
}
if (commandObject.customCommandType === 1) { //Only one response in the command
message.channel.send(`${commandObject.defaultResponse.message}`)
return
}
else if (commandObject.customCommandType === 2) { //Random custom command
let randomIdex = getRandomIntBetween(0, commandObject.amountOfResponses - 1)
message.channel.send(`${commandObject.responses[randomIdex].message}`)
return
}
else if (commandObject.customCommandType === 3) { //Single embed command
const commandText = commandObject.defaultResponse.message
const d = commandObject.defaultResponse.embed
const color = d.hexColor ? '#' + ((Number)(d.hexColor)).toString(16) : undefined
let embed = new Discord.MessageEmbed({
title: d.title,
description: d.description ? d.description : undefined,
color: color ? color : undefined,
fields: d.fields ? d.fields : undefined
})
if (d.thumbnailURL) {
embed.setThumbnail(d.thumbnailURL)
}
if (d.footer.text) {
embed.setFooter(d.footer.text, d.footer.iconURL)
}
message.channel.send(commandText, embed)
return
}
}
})
}
module.exports.pushNewUserToIgnore = (id, reason) => {
console.log(`USER WITH ID ${id} IGNORED:`)
ignoredUsers.push(id)
}
module.exports.removeUserToIgnore = (id, reason) => {
const index = ignoredUsers.indexOf(id)
if (index > -1) { ignoredUsers.splice(index, 1) }
console.log(`USER WITH ID ${id} REMOVED FROM IGNORED:`, reason)
}
module.exports.getAllInCache = () => {
let commandArr = []
for (const command in commandsCache) {
commandArr.push(command)
}
return commandArr
}
getPrefix = (guildId) => {
return commandBase.getGuildPrefix(guildId)
}
getRandomIntBetween = (min, max) => {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1)) + min
}