forked from TejasLamba2006/Discord-Button-Tickets-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
458 lines (451 loc) · 23 KB
/
index.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
const Discord = require("discord.js");
const client = new Discord.Client({
disableMentions: 'everyone'
})
require("dotenv").config()
require('discord-reply');
const { Database } = require("quickmongo");
const db = new Database(process.env.Mongo)
const randomstring = require("randomstring");
const disbut = require('discord-buttons');
require('discord-buttons')(client);
const { MessageMenu, MessageMenuOption } = require('discord-buttons');
const config = require(`./config.json`)
const prefix = config.prefix;
async function channelLog(embed) {
if (!config.log_channel_id) return;
let ch = await client.channels.cache.get(config.log_channel_id) || message.guild.channels.cache.find(channel => channel.name.match("log"));
if (!ch) return console.log(`Pls fill config.json`)
ch.send(embed)
}
client.on('ready', async () => {
await console.clear()
channelLog(`> The **Bot** is connecting to discord API`)
console.log(`Made by Tejas Lamba$1924`)
console.log(`Credits | Visa2Code | https://discord.gg/xtessK2DPA`)
console.log(`Join above or you gay`)
client.user.setActivity(config.status.name, { type: config.status.type.toUpperCase(), url: "https://twitch.tv/SmallCadaver" })
});
client.on("message", async(message) =>{
if (message.author.bot || !message.guild) return;
let args = message.content.toLowerCase().split(" ");
let command = args.shift()
if (command == prefix + `help`) {
let embed = new Discord.MessageEmbed()
.setTitle(`Bot commands list`)
.setDescription(`> \`${prefix}send\` - Send a message to open tickets
> \`${prefix}add\` - Adds a member to a specific ticket
> \`${prefix}remove\` - Removes a member to a specific ticket.
> \`${prefix}delete\` - Delete a specific ticket
> \`${prefix}close\` - Close a specific ticket
> \`${prefix}open\` - Open a specific ticket
> \`${prefix}rename\` - Rename a specific ticket
> \`${prefix}setchannels\` - set channels relating to ticket log and category
> \`${prefix}setstaff\` - set staff roles`)
.setTimestamp()
.setColor(0x5865F2)
.setFooter(`All rights belong to https://discord.gg/xtessK2DPA`)
message.lineReply({ embed: embed })
}
if (command == prefix + `add`) {
if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.lineReply(`:x: This command requires \`MANAGE_MESSAGES\` permission.`);
let args = message.content.split(' ').slice(1).join(' ');
let channel = message.mentions.channels.first() || message.channel;
const sfats = await db.get(`Staff_${message.guild.id}`)
if (!sfats) return message.lineReply({ embed: { description: `this server needs to set up their staff roles first! \`{prefix}setstaff\``, color: 0x5865F2 } })
if (await db.get(`ticket_${channel.id}_${message.guild.id}`)) {
let member = message.mentions.members.first() || message.guild.members.cache.get(args || message.guild.members.cache.find(x => x.user.username === args || x.user.username === args));
if (!member) return message.lineReply(`Mention a member of its ID`);
try {
channel.updateOverwrite(member.user, {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
ATTACH_FILES: true,
READ_MESSAGE_HISTORY: true,
}).then(() => {
message.lineReply({ embed: { description: `${member} has been successfully added to ${channel}`, color: 0x5865F2 } });
let log_embed = new Discord.MessageEmbed()
.setTitle(`A person has been added to a ticket`)
.addField(`Ticket`, `<#${channel.id}>`)
.addField(`Added Person`, member.user)
.addField(`Action by`, `<@!${message.author.id}>`)
.setTimestamp()
.setColor(`GREEN`)
.setFooter(message.guild.name, message.guild.iconURL())
channelLog(log_embed)
});
}
catch (e) {
return message.channel.send(`An error occurred, please try again!`);
}
}
}
if (command == prefix + `remove`) {
if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.lineReply(`:x: This command requires \`MANAGE_MESSAGES\` permission.`);
let args = message.content.split(' ').slice(1).join(' ');
let channel = message.mentions.channels.first() || message.channel;
const sfats = await db.get(`Staff_${message.guild.id}`)
if (!sfats) return message.lineReply({ embed: { description: `this server needs to set up their staff roles first! \`{prefix}setstaff\``, color: 0x5865F2 } })
if (await db.get(`ticket_${channel.id}_${message.guild.id}`)) {
let member = message.mentions.members.first() || message.guild.members.cache.get(args || message.guild.members.cache.find(x => x.user.username === args || x.user.username === args));
if (!member) return message.lineReply(`Mention a member of its ID`);
try {
channel.updateOverwrite(member.user, {
VIEW_CHANNEL: false,
}).then(() => {
let log_embed = new Discord.MessageEmbed()
.setTitle(`People removed to ticket`)
.addField(`Ticket`, `<#${channel.id}>`)
.addField(`person added`, member.user)
.addField(`Action by`, `<@!${message.author.id}>`)
.setTimestamp()
.setColor(`RED`)
.setFooter(message.guild.name, message.guild.iconURL())
channelLog(log_embed)
message.lineReply({ embed: { description: `Successfully delete ${member} from ${channel}`, color: 0x5865F2 } });
});
}
catch (e) {
return message.channel.send(`An error occurred, please try again!`);
}
}
}
if (command == prefix + 'delete') {
if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.lineReply(`:x: This command requires \`MANAGE_MESSAGES\` permission.`);
let channel = message.mentions.channels.first() || message.channel;
const sfats = await db.get(`Staff_${message.guild.id}`)
if (!sfats) return message.lineReply({ embed: { description: `this server needs to set up their staff roles first! \`{prefix}setstaff\``, color: 0x5865F2 } })
if (await db.get(`ticket_${channel.id}_${message.guild.id}`)) {
message.lineReply({ embed: { description: `Your order is executed after 5 seconds, and it will be closed`, color: 0x5865F2 } })
setTimeout(async () => {
let log_embed = new Discord.MessageEmbed()
.setTitle(`Ticket Deleted`)
.addField(`Ticket number`, `${await db.get(`ticket_${channel.id}_${message.guild.id}`).count}`)
.addField(`Ticket by`,`<@!${await db.get(`ticket_${channel.id}_${message.guild.id}`).ticket_by}>`)
.addField(`Action by`, `<@!${message.author.id}>`)
.setTimestamp()
.setColor(`RED`)
.setFooter(message.guild.name, message.guild.iconURL())
channelLog(log_embed)
channel.delete()
}, 5000)
}
}
if (command == prefix + 'close') {
if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.lineReply(`:x: This command requires \`MANAGE_MESSAGES\` permission.`);
let channel = message.mentions.channels.first() || message.channel;
const sfats = await db.get(`Staff_${message.guild.id}`)
if (!sfats) return message.lineReply({ embed: { description: `this server needs to set up their staff roles first! \`{prefix}setstaff\``, color: 0x5865F2 } })
if (await db.get(`ticket_${channel.id}_${message.guild.id}`)) {
let msg = await message.lineReply({ embed: { description: `Your order is executed after 5 seconds, and it will be closed`, color: 0x5865F2 } })
setTimeout(async () => {
try {
msg.delete()
channel.send({ embed: { description: `Ticket has been closed by <@!${message.author.id}>`, color: `YELLOW` } })
let type = 'member'
await Promise.all(channel.permissionOverwrites.filter(o => o.type === type).map(o => o.delete()));
channel.setName(`closed-${(await db.get(`ticket_${channel.id}_${message.guild.id}`))}`)
let log_embed = new Discord.MessageEmbed()
.setTitle(`Ticket closed`)
.addField(`Ticket`, `<#${channel.id}>`)
.addField(`Action by`, `<@!${message.author.id}>`)
.setTimestamp()
.setColor(`YELLOW`)
.setFooter(message.guild.name, message.guild.iconURL())
channelLog(log_embed)
} catch (e) {
return message.channel.send(`An error occurred, please try again!`);
}
}, 1000)
}
}
if (command == prefix + 'open') {
if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.lineReply(`:x: This command requires \`MANAGE_MESSAGES\` permission.`);
let channel = message.mentions.channels.first() || message.channel;
const sfats = await db.get(`Staff_${message.guild.id}`)
if (!sfats) return message.lineReply({ embed: { description: `this server needs to set up their staff roles first! \`{prefix}setstaff\``, color: 0x5865F2 } })
if (await db.get(`ticket_${channel.id}_${message.guild.id}`)) {
let msg = await message.lineReply({ embed: { description: `Your order is executed after 5 seconds`, color: 0x5865F2 } })
setTimeout(async () => {
try {
msg.delete()
channel.send({ embed: { description: `Ticket opened by <@!${message.author.id}>`, color: `GREEN` } })
let meember = client.users.cache.get(await db.get(`ticket_${channel.id}_${message.guild.id}`).ticket_by);
channel.updateOverwrite(meember, {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
ATTACH_FILES: true,
READ_MESSAGE_HISTORY: true,
})
channel.updateOverwrite((await db.get(`Staff_${message.guild.id}.Admin`)), {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
ATTACH_FILES: true,
READ_MESSAGE_HISTORY: true,
})
channel.updateOverwrite((await db.get(`Staff_${message.guild.id}.Moder`)), {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
ATTACH_FILES: true,
READ_MESSAGE_HISTORY: true,
})
channel.setName(`ticket-${await db.get(`ticket_${channel.id}_${message.guild.id}`).count}`)
let log_embed = new Discord.MessageEmbed()
.setTitle(`Ticket has reopened`)
.addField(`Ticket`, `<#${channel.id}>`)
.addField(`Action by`, `<@!${message.author.id}>`)
.setTimestamp()
.setColor(`GREEN`)
.setFooter(message.guild.name, message.guild.iconURL())
channelLog(log_embed)
} catch (e) {
return message.channel.send(`An error occurred, please try again!`);
}
}, 1000)
}
}
if (command == prefix + 'rename' || command == prefix + 'setname') {
if (!message.member.hasPermission('MANAGE_MESSAGES')) return message.lineReply(`:x: This command requires \`MANAGE_MESSAGES\` permission.`);
let channel = message.mentions.channels.first() || message.channel;
const sfats = await db.get(`Staff_${message.guild.id}`)
if (!sfats) return message.lineReply({ embed: { description: `this server needs to set up their staff roles first! \`{prefix}setstaff\``, color: 0x5865F2 } })
if (await db.get(`ticket_${channel.id}_${message.guild.id}`)) {
let args = message.content.split(' ').slice(1).join(' ');
if (!args) return message.lineReply({ embed: { description: `Please select the name you want for the ticket`, color: 0x5865F2 } })
channel.setName(args)
message.delete()
let log_embed = new Discord.MessageEmbed()
.setTitle(`Ticket name change`)
.addField(`New name`, args)
.addField(`Ticket`, `<#${channel.id}>`)
.addField(`by`, `<@!${message.author.id}>`)
.setTimestamp()
.setColor(0x5865F2)
.setFooter(message.guild.name, message.guild.iconURL())
channelLog(log_embed)
}
}
if (command == prefix + 'setstaff'){
console.log(args)
if (!message.member.hasPermission('ADMINISTRATOR')) return message.lineReply(`:x: This command requires \`ADMINISTRATOR\` permission.`);
if (args.length != 2) return message.lineReply({ embed: { description: `Please provide an Admin role id, *then* a Mod role id with this command! `, color: 0x5865F2 } })
if (message.mentions.roles.length < 2 && !Number(args[0]) && !Number(args[1])) return message.lineReply({ embed: { description: `Please mention an Admin role (or iD) first, *then* a Mod role (or iD) with this command! `, color: 0x5865F2 } })
const Admin = message.guild.roles.cache.get(args[0]);
const Moder = message.guild.roles.cache.get(args[1]);
await db.set(`Staff_${message.guild.id}.Admin`, Admin.id)
await db.set(`Staff_${message.guild.id}.Moder`, Moder.id)
message.react("✅")
}
if (command == prefix + 'setchannels'){
if (!message.member.hasPermission('ADMINISTRATOR')) return message.lineReply(`:x: This command requires \`ADMINISTRATOR\` permission.`);
if (args.length != 2) return message.lineReply({ embed: { description: `Please mention a channelid, *then* a categoryid with this command! `, color: 0x5865F2 } })
if (message.mentions.roles.length < 2 && !Number(args[0]) && !Number(args[1])) return message.lineReply({ embed: { description: `Please mention an Log Channel (or iD), *then* a Category (or iD) with this command! `, color: 0x5865F2 } })
const txt = message.guild.channels.cache.get(args[0]);
const cat = message.guild.channels.cache.get(args[1]);
if (txt.type !== "text") return message.channel.send("The first input should be a text channel");
if (cat.type !== "category") return message.channel.send("The second input should be a text category");
await db.set(`Channels_${message.guild.id}.Log`, txt.id)
await db.set(`Channels_${message.guild.id}.Cat`, cat.id)
message.react("✅")
}
if (command == prefix + 'send' || command == prefix + 'ticket') {
if (!message.member.hasPermission('ADMINISTRATOR')) return message.lineReply(`:x: This command requires \`ADMINISTRATOR\` permission.`);
const sfats = await db.get(`Staff_${message.guild.id}`)
const sfas = await db.get(`Channels_${message.guild.id}`)
if (!sfats || sfats === null) return message.lineReply({ embed: { description: `This server needs to set up their staff roles first! \`${prefix}setstaff\``, color: 0x5865F2 } })
if (!sfas || sfas === null) return message.lineReply({ embed: { description: `This server needs to set up their ticket channels first! \`${prefix}setchannels\``, color: 0x5865F2 } })
let idd = randomstring.generate({ length: 20 })
let args = message.content.split(' ').slice(1).join(' ');
if (!args) args = `Tickets`
let button1 = new MessageMenuOption()
.setLabel('Special Support')
.setEmoji('🔴')
.setValue("men")
.setDescription('Use this to contact Admins+ only!')
let button3 = new MessageMenuOption()
.setLabel('General Support')
.setEmoji('🟠')
.setValue("hlp")
.setDescription('Use this to contact Helpers and higher ranks!')
let select = new MessageMenu()
.setID(idd)
.setPlaceholder('Create A ticket!')
.setMaxValues(1)
.setMinValues(1)
.addOptions(button1, button3)
let embed = new Discord.MessageEmbed()
.setTitle(args)
.setDescription("To create a ticket, select one of the options below from the menu.")
.setThumbnail(message.guild.iconURL())
.setTimestamp()
.setColor(0x5865F2)
.setFooter(message.guild.name, message.guild.iconURL())
let msg = await message.channel.send({ embed: embed, component: select }).then(async msg => {
msg.pin()
let log_embed = new Discord.MessageEmbed()
.setTitle(`A message has been sent to open new tickets`)
.addField(`Channel`, `<#${message.channel.id}>`)
.addField(`by`, `<@!` + message.author.id + `>`)
.setTimestamp()
.setColor(0x5865F2)
.setFooter(message.guild.name, message.guild.iconURL())
channelLog(log_embed)
await db.set(`tickets_${idd}_${message.guild.id}`, {
reason: args,
msgID: msg.id,
id: idd,
options: [button1, button3],
guildName: message.guild.name,
guildAvatar: message.guild.iconURL(),
channelID: message.channel.id
})
})
}
})
client.on('clickMenu', async (button) => {
console.log(button.values)
if (await db.get(`tickets_${button.id}_${button.message.guild.id}`)) {
await button.reply.send(`Your ticket is being processed. Please wait `, true)
await db.math(`counts_${button.message.id}_${button.message.guild.id}`, `+`, 1)
let count = await db.get(`counts_${button.message.id}_${button.message.guild.id}`)
let channel;
await button.clicker.fetch();
if (button.values[0] === "men") { // Admins +
button.guild.channels.create(`ticket-${count}`, {
permissionOverwrites: [
{
id: button.guild.roles.everyone,
deny: ['VIEW_CHANNEL'],
},
{
id: (await db.get(`Staff_${button.message.guild.id}.Admin`)),
allow: ['VIEW_CHANNEL', `READ_MESSAGE_HISTORY`, `ATTACH_FILES`, `SEND_MESSAGES`,`MANAGE_MESSAGES`],
},
{
id: button.clicker.user.id,
allow: ['VIEW_CHANNEL', `READ_MESSAGE_HISTORY`, `ATTACH_FILES`, `SEND_MESSAGES`],
},
], parent: (await db.get(`Channels_${button.message.guild.id}.Cat`)), position: 1, topic: `A Ticket : <@!${button.clicker.user.id}>`, reason: "All rights reserved to Visa2Code"
}).then(async channel => {
channel = channel
await db.set(`ticket_${channel.id}_${button.message.guild.id}`, { count: count, ticket_by: button.clicker.user.id })
await button.reply.edit(`
**Your ticket has been successfully opened** <#${channel.id}>`, true)
let log_embed = new Discord.MessageEmbed()
.setTitle(`New ticket opened`)
.addField(`Ticket`, `<#${channel.id}>`)
.addField(`Ticket by`, `<@!${button.clicker.user.id}>`)
.addField(`Ticket number`, count)
.setTimestamp()
.setColor(`GREEN`)
channelLog(log_embed)
const embedticket = new Discord.MessageEmbed()
.setTimestamp()
.setTitle("Specialised Support")
.setFooter(`Ticket opened at`)
.setColor(0x5865F2)
.setDescription(`Support will be with you soon.\n
To close this ticket, interact with 🔒`)
let idd = randomstring.generate({ length: 25 })
let bu1tton = new disbut.MessageButton()
.setStyle(`gray`)
.setEmoji(`🔒`)
.setLabel(`Close`)
.setID(idd)
channel.send(`Welcome <@!${button.clicker.user.id}>`, { embed: embedticket, component: bu1tton }).then(msg => {
msg.pin()
})
})
}
if (button.values[0] === "hlp"){ // help +
button.guild.channels.create(`ticket-${count}`, {
permissionOverwrites: [
{
id: button.guild.roles.everyone,
deny: ['VIEW_CHANNEL'],
},
{
id: (await db.get(`Staff_${button.message.guild.id}.Admin`)),
allow: ['VIEW_CHANNEL', `READ_MESSAGE_HISTORY`, `ATTACH_FILES`, `SEND_MESSAGES`,`MANAGE_MESSAGES`],
},
{
id: (await db.get(`Staff_${button.message.guild.id}.Moder`)),
allow: ['VIEW_CHANNEL', `READ_MESSAGE_HISTORY`, `ATTACH_FILES`, `SEND_MESSAGES`,`MANAGE_MESSAGES`],
},
{
id: button.clicker.user.id,
allow: ['VIEW_CHANNEL', `READ_MESSAGE_HISTORY`, `ATTACH_FILES`, `SEND_MESSAGES`],
},
], parent: (await db.get(`Channels_${button.message.guild.id}.Cat`)), position: 1, topic: `A Ticket : <@!${button.clicker.user.id}>`, reason: "All rights reserved to Visa2Code"
}).then(async channel => {
channel = channel
await db.set(`ticket_${channel.id}_${button.message.guild.id}`, { count: count, ticket_by: button.clicker.user.id })
await button.reply.edit(`
**Your ticket has been successfully opened** <#${channel.id}>`, true)
let log_embed = new Discord.MessageEmbed()
.setTitle(`New ticket opened`)
.addField(`Ticket`, `<#${channel.id}>`)
.addField(`Ticket by`, `<@!${button.clicker.user.id}>`)
.addField(`Ticket number`, count)
.setTimestamp()
.setColor(`GREEN`)
channelLog(log_embed)
const embedticket = new Discord.MessageEmbed()
.setTimestamp()
.setTitle("General Support")
.setFooter(`Ticket opened at`)
.setColor(0x5865F2)
.setDescription(`Support will be with you soon.\n
To close this ticket, interact with 🔒`)
let idd = randomstring.generate({ length: 25 })
await db.set(`close_${button.clicker.user.id}`, idd)
let bu1tton = new disbut.MessageButton()
.setStyle(`gray`)
.setEmoji(`🔒`)
.setLabel(`Close`)
.setID(idd)
channel.send(`Welcome <@!${button.clicker.user.id}>`, { embed: embedticket, component: bu1tton }).then(msg => {
msg.pin()
})
})
}
}
});
client.on('clickButton', async (button1) => {
await button1.clicker.fetch()
let idd = randomstring.generate({ length: 25 })
await db.set(`close_${button1.clicker.user.id}_sure`, idd)
if (button1.id == (await db.get(`close_${button1.clicker.user.id}`))) {
let bu0tton = new disbut.MessageButton()
.setStyle(`red`)
.setLabel(`close`)
.setID(idd)
await button1.reply.send(`Are you sure you want to close this ticket?`, { component: bu0tton, ephemeral: true });
}
})
client.on('clickButton', async (button) => {
await button.clicker.fetch()
if (button.id == (await db.get(`close_${button.clicker.user.id}_sure`))) {
await button.reply.send(`Your order is executed after 5 seconds, and it will be closed`, true)
let ch = button.channel
if (!ch) return;
setTimeout(async () => {
try {
await ch.send({ embed: { description: `The ticket has already been closed <@!${button.clicker.user.id}>`, color: `YELLOW` } });
let type = 'member'
await Promise.all(ch.permissionOverwrites.filter(o => o.type === type).map(o => o.delete()));
ch.setName(`closed-ticket`)
let log_embed = new Discord.MessageEmbed()
.setTitle(`Ticket closed`)
.addField(`Ticket`, `<#${ch.id}>`)
.addField(`Action by`, `<@!${button.clicker.user.id}>`)
.setTimestamp()
.setColor(`YELLOW`)
channelLog(log_embed)
} catch (e) {
return button.channel.send(`An error occurred, please try again!`);
}
}, 4000)
}
})
client.login(process.env.TOKEN);