-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
428 lines (409 loc) · 14.2 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
/*
For further help please check the repo at https://github.com/peterhanania/modmail-bot/
*/
const {
token,
prefix,
mongooseString,
guild,
mainCategory,
logs,
modMailRole,
mainServer
} = require("./config/settings.json");
const mongoose = require("mongoose");
const fs = require("fs");
const {
Client,
Message,
MessageEmbed,
MessageAttachment,
} = require("discord.js");
const client = new Client();
//customizable snippets "./custom/snippets.json"
const snippets = require("./custom/snippets.json");
//connecting to mongoDB
mongoose.connect(mongooseString, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
//creating a Schema
const db = new mongoose.model(
"modmail-transcript",
new mongoose.Schema({
AuthorID: String,
Content: Array,
})
);
//ready event
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`);
client.user.setActivity("DM me for help", {
type: "LISTENING",
});
});
/**
* @param {Message} message
* @param {Client} client
*/
//message event
client.on("message", async (message) => {
if (message.author.bot) return; // return if message is sent by a bot
const args = message.content.slice(prefix.length).trim().split(/ +/g); //splitting args
const mainGuild = client.guilds.cache.get(guild); //server where the threads are created
const modMailLogs = mainGuild.channels.cache.get(logs); //modmail logs are sent here
//only create threads if it is a DM message
if (message.channel.type === "dm") {
checkAndSave(message);
//checking if channel for author already exists in the modmail category
const checkChannel = !!mainGuild.channels.cache.find(
(ch) => ch.topic === message.author.id
);
//if exists, send in existing
if (checkChannel === true) {
const mailChannel = await mainGuild.channels.cache.find(
(ch) => ch.topic === message.author.id
);
//if message is an attachment
if (message.attachments && message.content === "") {
message.react('✅')
mailChannel.send(
new MessageEmbed()
.setAuthor(
message.author.tag,
message.author.displayAvatarURL({ dynamic: true })
)
.setColor("BLUE")
.setImage(message.attachments.first().proxyURL)
.setTimestamp()
);
}
//if message is a text message
else {
message.react('✅')
mailChannel.send(
new MessageEmbed()
.setAuthor(
message.author.tag,
message.author.displayAvatarURL({ dynamic: true })
)
.setColor("BLUE")
.setDescription(message.content)
.setTimestamp()
);
}
}
//modmail thread channel does not exist
else if (checkChannel === false) {
//create a thread under the mainCategory (can be modified in "./config/settings.json")
const mailChannel = await mainGuild.channels.create(`thread-${message.author.tag}-${message.author.id}`, {
type: "text",
topic: message.author.id,
parent: mainCategory,
permissionOverwrites: [
{
id: mainGuild.id,
allow: ["VIEW_CHANNEL"], //permission overwrites. You can change visiblity by chaging "allow" to "deny"
},
],
});
//pinging the modmail role on the creation of a new thread. Role ID can be changed in "./config/settings.json"
mailChannel.send(
`<@&${modMailRole}> **${message.author.tag}** has created a new thread, use ${prefix}reply To reply!`
);
//logging the creation of the thread. LogChannel can be changed in "./config/settings.json"
modMailLogs.send(
new MessageEmbed()
.setDescription(
`\`${message.author.tag}\` has created a new thread [${mailChannel}] \`${mailChannel.name}\``
)
.setColor(`GREEN`)
.setTimestamp()
);
//DMing the author on successfully creating the thread
message.author.send(
new MessageEmbed()
.setAuthor(client.user.tag, client.user.displayAvatarURL())
.setTitle("Thread Created!")
.setDescription(
`Thank you for reaching out to our staff team, ${message.author.username}! They will be contacting you as soon as they can! Meanwhile please specify your questions/issues.`
)
.setColor(`RANDOM`)
.setTimestamp()
.setFooter(`This is an automated message`)
).catch((e)=>{
return console.log(e.message);
})
//if message content is an attachment
if (message.attachments && message.content === "") {
message.react('✅')
mailChannel.send(
new MessageEmbed()
.setAuthor(
message.author.tag,
message.author.displayAvatarURL({ dynamic: true })
)
.setColor("BLUE")
.setImage(message.attachments.first().proxyURL)
.setTimestamp()
);
}
//if message content is plain text
else {
message.react('✅')
mailChannel.send(
new MessageEmbed()
.setAuthor(
message.author.tag,
message.author.displayAvatarURL({ dynamic: true })
)
.setColor("BLUE")
.setDescription(message.content)
.setTimestamp()
);
}
}
}
//switching to staff replies
if (!message.guild) return; //returning if message is in DMs
//info command- it would be appreciated if this was not changed :)
if (message.content === `${prefix}info` || message.content === `${prefix}information`) {
message.channel.send(
new MessageEmbed()
.setTitle("Modmail Bot")
.setDescription(
"Hi there! Modmail bot is an open sourced discord.js project to aid Discord servers with managing ModMails\n👉 Open sourced project developed by `zhue#5683` and edited by `Peter_#4444`\n📌 Code [here](https://github.com/peterhanania/modmail-bot)"
)
.setColor("BLUE")
.setTimestamp()
);
}
//checking if the thread handling commands are in the staff server only and are under the modmail category (wont work elsewhere)
if (
message.guild.id === mainGuild.id &&
message.channel.parentID === mainCategory &&
!message.author.bot
) {
//ID of author who created the thread
let firstAuthorID = message.channel.topic;
//the main server (not the one in which threads are created)
let zCafe = client.guilds.cache.get(mainServer);
//getting the author who created the thread by ID
let firstAuthorMember = zCafe.members.cache.get(firstAuthorID);
//close command- closes the current thread with a timeout of 10s
if (message.content === `${prefix}close`) {
await message.channel
.send(
new MessageEmbed()
.addField(
`Thread closed`,
`This channel will be deleted in 10 seconds`
)
.setColor("RED")
.setFooter(`Closed by ${message.author.tag}`)
)
.catch(console.error);
//deleting after 10 seconds
setTimeout(async () => {
//if member left the main guild
if (!firstAuthorMember)
return message.channel.send(
`Couldn't send close confirmation to member. They might've left the server...`
);
await firstAuthorMember
.send(
new MessageEmbed()
.setTitle("Thread Closed")
.setDescription(
"This thread has been closed by our staff team. Replying to this message will create a new thread. Feel free to contact us if you need any help again <3"
)
.setColor("RED")
.setAuthor(client.user.tag, client.user.displayAvatarURL())
.setTimestamp()
.setFooter("This is an automated message")
)
.then(async () => {
await modMailLogs
.send(
new MessageEmbed()
.addField(
`Thread closed`,
`Thread with ID \`#${firstAuthorID}\` closed by \`${message.author.tag}\``
)
.setColor("RED")
.setTimestamp()
)
.catch(console.error);
})
.catch(console.error);
//logging thread close
await modMailLogs
.send(
new MessageEmbed()
.addField(
`Thread closed`,
`Thread with ID \`#${firstAuthorID}\` closed by \`${message.author.tag}\``
)
.setColor("RED")
.setTimestamp()
)
.catch(console.error);
await message.channel.delete().catch(console.error);
sendTranscriptAndDelete(message, modMailLogs);
}, 10000);
// ^^^^^ timeout can be changed by changing that (TIME IN MILLISECONDS)
return;
} //end of close command
//pre-defined snippet commands
//snippets can be fully customized from "./custom/snippets.json"
if (message.content.startsWith(prefix)) {
var reply;
let snippetEmbed = new MessageEmbed()
.setTimestamp()
.setFooter("Staff Team")
.setColor("GREEN");
if (args[0].toLowerCase() === "r" || args[0].toLowerCase() === "reply") {
//no reply stated
if (!args[1]) return;
//staff reply
reply = args.slice(1).join(" ");
//member not found
if (!firstAuthorMember)
return message.channel.send(
`Couldn't reply as member might have left the main server`
);
const replyEmbed = new MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL())
.setDescription(reply)
.setTimestamp()
.setColor("GREEN")
.setFooter("Staff Team");
try {
await message.delete();
await message.channel.send(replyEmbed);
await firstAuthorMember.send(replyEmbed);
} catch (e) {
console.log(`${e.message}`);
}
} else if (args[0].toLowerCase() === "hi") {
snippetEmbed.setAuthor(
message.author.tag,
message.author
.displayAvatarURL({ dynamic: true })
)
.setDescription(snippets.hi)
try {
await message.delete();
await message.channel.send(snippetEmbed);
await firstAuthorMember.send(snippetEmbed);
} catch (e) {
console.log(`${e.message}`);
}
} else if (args[0].toLowerCase() === "transferred") {
snippetEmbed.setAuthor(
message.author.tag,
message.author
.displayAvatarURL({ dynamic: true })
).setDescription(snippets.transferred)
try {
await message.delete();
await message.channel.send(snippetEmbed);
await firstAuthorMember.send(snippetEmbed);
} catch (e) {
console.log(`${e.message}`);
}
} else if (args[0].toLowerCase() === "reported") {
snippetEmbed.setAuthor(
message.author.tag,
message.author
.displayAvatarURL({ dynamic: true })
) .setDescription(snippets.reported)
try {
await message.delete();
await message.channel.send(snippetEmbed);
await firstAuthorMember.send(snippetEmbed);
} catch (e) {
console.log(`${e.message}`);
}
} else if (args[0].toLowerCase() === "morehelp") {
snippetEmbed.setAuthor(
message.author.tag,
message.author
.displayAvatarURL({ dynamic: true })
).setDescription(snippets.morehelp)
try {
await message.delete();
await message.channel.send(snippetEmbed);
await firstAuthorMember.send(snippetEmbed);
} catch (e) {
console.log(`${e.message}`);
}
} else if (args[0].toLowerCase() === "noreply") {
snippetEmbed.setAuthor(
message.author.tag,
message.author
.displayAvatarURL({ dynamic: true })
).setDescription(snippets.noreply)
try {
await message.delete();
await message.channel.send(snippetEmbed);
await firstAuthorMember.send(snippetEmbed);
} catch (e) {
console.log(`${e.message}`);
}
} else {
return;
}
}
}
});
//functions
async function checkAndSave(message) {
db.findOne({ AuthorID: message.author.id }, async (err, data) => {
if (err) throw err;
if (data) {
if (message.attachments && message.content === "") {
data.Content.push(
`${message.author.tag} : ${message.attachments.first().proxyURL}`
);
} else {
data.Content.push(`${message.author.tag} : ${message.content}`);
}
} else {
if (message.attachments && message.content === "") {
data = new db({
AuthorID: message.author.id,
Content: `${message.author.tag} : ${
message.attachments.first().proxyURL
}`,
});
} else {
data = new db({
AuthorID: message.author.id,
Content: `${message.author.tag} : ${message.content}`,
});
}
}
data.save();
});
}
async function sendTranscriptAndDelete(message, channel) {
db.findOne({ AuthorID: message.channel.name }, async (err, data) => {
if (err) throw err;
if (data) {
fs.writeFileSync(
`../${message.channel.name}.txt`,
data.Content.join("\n\n")
);
await channel.send(
new MessageAttachment(
fs.createReadStream(`../${message.channel.name}.txt`)
)
);
fs.unlinkSync(`../${message.channel.name}.txt`);
await db.findOneAndDelete({ AuthorID: message.channel.name });
}
});
}
//login
client.login(token);