-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
349 lines (279 loc) · 13.4 KB
/
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const Discord = require('discord.js')
const client = new Discord.Client()
const app = require('./app.js')
const { logChannel, mutedRole, adminRole } = require('./config.json')
const reason = null
const setup = require('./setup-command')
const sendLog = require('./send-log')
const embeds = require('./libraries/embeds')
const NodeChache = require('node-cache')
const myCache = new NodeChache( { stdTTL: 0, checkperiod: 0 } )
const mongo = require('./libraries/mongo')
const mongoose = require('mongoose')
const staffRoleSchema = require('./schemas/staffRole-schema')
//command handler
//to add a new command, just add a new else if statement
//example:
//else if (primaryCommand == "newCommand (without the prefix) "){
// logic
//}
module.exports.command = async (recievedMessage, primaryCommand, arguments) => {
//deconstructs relevant properties
const { content, author, guild, channel, member} = recievedMessage
let pCmd = primaryCommand.toLowerCase();
//this checks if there is a Muted role in the guild, if not it will make one
if(!guild.roles.cache.find(r => r.name === "Muted")){
guild.roles.create({
data: {
name: 'Muted',
},
reason: 'muted role for members that are muted, You will have to configure permissions yourself!'
})
}
// checks if it is a update from the setup command for staffrole
if (pCmd == 'cachestaffrole'){
//sets the updated role i cache
roleCache = myCache.set(guild.id, arguments)
console.log(arguments, ' set as srole');
return
}
//gets cached staff role, if its not cached it returns undefined
staffRole = myCache.get( guild.id )
//if the staff role isnt cached it has to fetch from the database
if (!staffRole){
console.log('FETCHING FROM DB (sRole)');
try{
await mongo().then(async mongoose =>{
const result = await staffRoleSchema.findOne({_id:`${guild.id}.srole`})
//if there is no staffrole in the database it will set the value in cache to null
if(!result){
staffRole = myCache.set(guild.id, null)
}else{
staffRole = myCache.set(guild.id, result.staffRole)
}
//sets the result in the cache and gets the new cache
staffRole = myCache.get( guild.id )
})
}finally{
mongoose.connection.close()
}
}
if (pCmd == "setup"){
if (member.permissions.has('ADMINISTRATOR')){
if(!arguments.includes('help')){
setup.setup(arguments, recievedMessage)
}else{
embeds.setupHelp(recievedMessage)
}
}else{
console.log('no admin ;-;');
}return
}
else if (pCmd == "intro"){
if (member.permissions.has('ADMINISTRATOR')){
embeds.staffIntro(recievedMessage)
recievedMessage.delete()
}
}
//admin commands can be above this check
//this checks if there is a role in cache,
//if there isn't it has to be that there is no entry in the database
if(staffRole){
//provides a list of staff commands (!staffhelp)
if (pCmd == 'staffhelp') {
//checks if author is staff
if (recievedMessage.member.roles.cache.find(r => r.id === staffRole)){
recievedMessage.delete()
embeds.staffHelp(recievedMessage)
}
else{
//sends DM if the author isnt a staff member
console.log(`${recievedMessage.author.username} is not a staff member(tried to use staffhelp)`);
recievedMessage.delete()
recievedMessage.author.send(`${recievedMessage.author.toString()} You don't have access to that command!`)
}
}
// unmute command -- its smart to collapse this if possible to save space on the screen
else if (pCmd == 'unmute') {
if (recievedMessage.member.roles.cache.find(r => r.id === staffRole)){
//checks if there is any arguments
if(arguments != ''){
//parameters to check if there is a valid ping
const mention = arguments[0].match(/^<@!?(\d+)>$/)
//checks if there is a valid ping
if (mention){
let role = recievedMessage.guild.roles.cache.find(r => r.name === mutedRole);
let member = recievedMessage.mentions.members.first();
//checks if the pinged member is muted
if (member.roles.cache.some(rolee => rolee.name === mutedRole)){
//removes the muted role of the pinged and deletes the prompted command
member.roles.remove(role);
recievedMessage.delete()
//finds the log channel from the channel name in config
if (recievedMessage.channel.type == 'text') {
var logger = recievedMessage.guild.channels.cache.find(
channel => channel.name === logChannel
);
if (logger) {
//sends mute log embed
sendLog(recievedMessage, 'alog', 'unmute', member)
//sends a dm to the muted member saying they have been muted
embeds.unMmuteDm(recievedMessage)
}
}
}
else{
console.log(`${recievedMessage.author.username} tried to unmute someone that isnt muted`);
recievedMessage.delete()
recievedMessage.author.send(`${recievedMessage.author.toString()} That member isnt muted! Someone might have already unmuted them, or they might have mute evaded!`)
}
} else{
//sends DM if the argument isnt a valid ping
console.log(`${recievedMessage.author.username} didnt provide a valid ping(unmute)`);
recievedMessage.delete()
recievedMessage.author.send(`${recievedMessage.author.toString()} You have to provide a valid ping, just like the one at the start of this sentence`)
}
} else {
//sends DM if there is no arguments
console.log(`${recievedMessage.author.username} didnt supply any arguments(unmute)`);
recievedMessage.delete()
recievedMessage.author.send(`${recievedMessage.author.toString()} You have to have mention someone to unmute them!`)
}
}
else{
//sends DM if the author isnt a staff member
console.log(`${recievedMessage.author.username} is not a staff member(tried to unmute)`);
recievedMessage.delete()
recievedMessage.author.send(`${recievedMessage.author.toString()} You don't have access to that command!`)
}
}
else if (pCmd == 'mute'){
if (recievedMessage.member.roles.cache.find(r => r.id === staffRole)){
//checks if there is any arguments
if(arguments != ''){
//parameters to check if there is a valid ping
const mention = arguments[0].match(/^<@!?(\d+)>$/)
//checks if there is a valid ping
if (mention){
let role = recievedMessage.guild.roles.cache.find(r => r.name === mutedRole);
let member = recievedMessage.mentions.members.first();
//checks if the pinged member is muted
if (!member.roles.cache.some(rolee => rolee.name === mutedRole)){
member.roles.add(role);
//checks if there is a provided reason (WIP)
//if (arguments[1]){
// if (arguments.slice(1)){
// //defines the reason
// let reasonArr = arguments.slice(1)
// let reason = reasonArr.join(" ")
// let reason = reason.match(/.+(-l)$/)
//
//
//
// if (loud[1]){
// console.log(reason);
//
// }
//
// }
//}
//deletes message containing the promted command
recievedMessage.delete()
//finds the log channel based on name provided in config
if (recievedMessage.channel.type == 'text') {
var logger = recievedMessage.guild.channels.cache.find(
channel => channel.name === logChannel
);
if (logger) {
//sends mute log embed
sendLog(recievedMessage, 'alog', 'mute', member)
//sends a dm to the muted member saying they have been muted
embeds.muteDM(recievedMessage)
}
}
}
else{
//Sends dm to the staff member telling them theyre being a dum dum
console.log(`${recievedMessage.author.username} tried to mute someone that is alrady muted`);
recievedMessage.delete()
recievedMessage.author.send(`${recievedMessage.author.toString()} That member is already muted`)
}
} else{
//sends DM if the argument isnt a valid ping
console.log(`${recievedMessage.author.username} didnt provide a valid ping(mute)`);
recievedMessage.delete()
recievedMessage.author.send(`${recievedMessage.author.toString()} You have to provide a valid ping, just like the one at the start of this sentence`)
}
} else {
//sends DM if there is no arguments
console.log(`${recievedMessage.author.username} didnt supply any arguments(mute)`);
recievedMessage.delete()
recievedMessage.author.send(`${recievedMessage.author.toString()} You have to have mention someone to mute them!`)
}
}
else{
//sends DM if the author isnt a staff member
console.log(`${recievedMessage.author.username} is not a staff member(tried to mute)`);
recievedMessage.delete()
recievedMessage.author.send(`${recievedMessage.author.toString()} You don't have access to that command!`)
}
}
//unfinished
else if (pCmd == 'allmuted'){
if (member.permissions.has('ADMINISTRATOR')){
let allMembers = recievedMessage.guild.members
console.log(allMembers.cache.array.id)
}
}
//crashes the bot on purpose incase admins feels its necessary
else if (pCmd == 'alexaoff') {
if (member.permissions.has('ADMINISTRATOR')){
console.log("bye sisters");
client.role.find()
}
}
//suggestion command
else if (pCmd == 'suggest'){
if (recievedMessage.channel.type == 'text') {
//finds channel for suggestions after specified channel name
var logger2 = recievedMessage.guild.channels.cache.find(
channel => channel.name === 'suggestions-for-bot'
);
if (logger2) {
//embed for suggestion
const SuggestionEmbed = new Discord.MessageEmbed()
.setTitle('Suggestion Recieved!')
.addField('Author of Suggestion:', '<@' + recievedMessage.author.id + '>')
.addField('Suggestion:', recievedMessage.content)
.setThumbnail("https://i.imgur.com/IPNxl5W.png")
.setColor('#b8002e');
logger2.send(SuggestionEmbed);
recievedMessage.delete()
}
}
}
//help command
else if (pCmd == 'help'){
recievedMessage.channel.send(helpEmbed)
}
//enter new commands above this line
//if a command is not registered the bot returns this. MUST be the last statement of the function to work
else {
console.log("Sorry, I don't know that one! Try doing `!help` for more commands!");
}
//this is if what the bot does if there is no staffrole in database
}else{
recievedMessage.channel.send('`hey admins you need to provide me a staffrole! do !setup help to see more`')
}
}
//vvv embeds vvv
//Controls what the help command looks like. To add a new field, do .addField.
const helpEmbed = new Discord.MessageEmbed()
.setTitle(`My commands list | My prefix is !`)
.addField("No public commands", "Right now i don't have any commands for non staff.")
.addField("Need a bug fixing? Contact my developers", "https://mcnobby.github.io/Auto-Logger/feedback")
//gif - .setImage("https://media.giphy.com/media/l0Ex3lGUCkofpN5UQ/giphy.gif")
.setImage("https://i.imgur.com/48H0ILI.png") //transparent logo
.setThumbnail("https://i.imgur.com/IPNxl5W.png")
.setColor("#42e3b8")
//mod help embed, to add new field do .addField