This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
222 lines (193 loc) · 8.62 KB
/
bot.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
require('dotenv').config();
const Ticket = require('./models/Ticket');
const FUNC = require("./resources/functions.js");
const CONFIG = require('./resources/config.json');
const Discord = require('discord.js');
global.client = new Discord.Client();
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Custom Commands
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Initial Startup
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
client.once('ready', async () => {
const db = require ('./database.js');
const TicketConfig = require('./models/TicketConfig');
console.log(`[Discord Bot Initialised - Logged in as ${client.user.tag}].`)
client.user.setPresence({ activity: { name: 'for commands', type: 'WATCHING' }, status: 'idle' })
db.authenticate()
.then(() => {
console.log('[Database Connection Successful.]');
Ticket.init(db);
TicketConfig.init(db);
Ticket.sync();
TicketConfig.sync();
}).catch((err) => {
return console.log(`DATABASE ERROR: ${err}`);
});
});
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Commands
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
client.on('message', async (message) => {
if (message.author.bot) return;
if (message.content[0] != CONFIG.PREFERENCES.PREFIX) return;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Command Content Vars
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
let args = message.content.substring(CONFIG.PREFERENCES.PREFIX.length).split(" ")
let argsstr = message.content.substring(CONFIG.PREFERENCES.PREFIX.length);
argsstr = argsstr.substr(argsstr.indexOf(" ") + 1)
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Commands
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
switch(args[0]) {
case "add":
if (await FUNC.IsUser("staff", message)) {
client.commands.get('add').execute(message, args)
await message.delete();
}
break;
case "remove":
if (await FUNC.IsUser("staff", message)) {
client.commands.get('remove').execute(message, args)
await message.delete();
}
break;
case "close":
if (await FUNC.IsUser("staff", message)) {
client.commands.get('close').execute(message, argsstr)
await message.delete();
}
break;
case "new":
if (await FUNC.IsUser("staff", message)) {
client.commands.get('new').execute(message, args)
await message.delete();
}
break;
case "setup":
if (message.author.id === message.guild.ownerID) {
client.commands.get('setup').execute(message, args)
await message.delete();
}
break;
case "rules":
if (message.author.id === message.guild.ownerID) {
client.commands.get('rules').execute(message, args)
await message.delete();
}
break;
case "release":
if (message.author.id === message.guild.ownerID) {
client.commands.get('release').execute(message, argsstr)
await message.delete();
}
break;
}
console.log(`[${message.guild.name}] ${args[0]} command ran by ${message.author.username}.`)
});
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ticket Reactions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
client.on('messageReactionAdd', async (reaction, user) => {
let message = reaction.message, emoji = reaction.emoji, channel = reaction.message.channel
if (user.bot) return;
if (emoji.name === '🙋♂️' || emoji.name === '❓' || emoji.name === '⚙️' || emoji.name === '🔒' || emoji.name === '⛔' || emoji.name === '📝' || emoji.name === '🔓') {
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Remove the Emoji
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
const ticket = await Ticket.findOne({ where: { channelId: message.channel.id }})
const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
try {
for (const reaction of userReactions.values()) {
await reaction.users.remove(user.id);
}
} catch (err) {
console.error(err);
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Emoji Switch
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
switch(emoji.name) {
// Open Ticket Emojis
case "🙋♂️":
FUNC.CreateTicket(user, message, "type_client").catch(err => console.log(err));
break;
case "❓":
FUNC.CreateTicket(user, message, "type_general").catch(err => console.log(err));
break;
case "⚙️":
FUNC.CreateTicket(user, message, "type_service").catch(err => console.log(err));
break;
// Close a Ticket
case "🔒":
const closedMessageId = ticket.getDataValue('closedMessageId');
if (user.id !== ticket.authorId && message.id === closedMessageId) return;
FUNC.CloseTicket(ticket, message, user, "Closed by Ticket Author").catch(err => console.log(err));
break;
// Delete the Ticket
case "⛔":
try {
if (ticket && ticket.resolved && ticket.modToolMessageId === message.id) {
let filter = m => m.author.id === user.id;
let check = true
message.channel.send(FUNC.Notify("Channel will be deleted in 5 seconds. Type `cancel` to stop the deletion.", "red")).then(msg => {
message.channel.awaitMessages(filter, {
max: 1,
time: 5000,
errors: ['time']
}).then(message => {
message = message.first()
if (message.content.toLowerCase() === "cancel") {
message.delete();
check = false
msg.edit(FUNC.Notify("Ticket Deletion Cancelled.", "green")).then(msg => {msg.delete({ timeout: 5000 })})
}
})
setTimeout(() => {
if (!check) return;
FUNC.UpdateTicket("Ticket Deleted", ticket, null, message, user)
message.channel.delete()
}, 5000)
})
}
} catch(err) {
console.error
}
break;
// Save Transcript
case "📝":
if (ticket && ticket.resolved && ticket.modToolMessageId === message.id) {
var date = message.channel.createdAt.toString().split(" ")
var time = date[4].toString().replace(/:/g, "").slice(0, -2)
const path = `./ticket-logs/${message.channel.name.toUpperCase()}-${date[2]}-${date[1].toUpperCase()}-${date[3]}-${time}.html`
FUNC.TranscribeChannel(message, path, ticket)
}
break;
// Reopen Ticket
case "🔓":
if (ticket && ticket.resolved && ticket.modToolMessageId === message.id) {
channel.send(FUNC.Notify("Ticket Reopened.", "green"))
channel.updateOverwrite(ticket.getDataValue('authorId'), {
VIEW_CHANNEL: true
}).catch((err) => console.log(`[${message.guild.name}] TICKET CLOSE ERROR: ${err}`));
ticket.resolved = false;
ticket.save();
message.channel.messages.fetch(ticket.modToolMessageId)
.then(message.delete())
.catch(console.error);
FUNC.UpdateTicket("Ticket Reopened", ticket, null, message, user)
}
break;
}
}
});
client.login(process.env.BOT_TOKEN);