forked from amanv8060/DSCKIIT-BOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticketClose.js
152 lines (143 loc) · 5.03 KB
/
ticketClose.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
/*
Copyright 2021 Aman Verma. All rights reserved.
Use of this source code is governed by a MIT license that can be
found in the LICENSE file.
*/
const data = require("../../../data/data.json");
async function generateContent(messages) {
const generatedMessages = (
await Promise.all(
messages.map(async (message) => {
let content = `**${message.author.username}:** ${message.content}`;
if (message.reactions.cache.size) {
const reactions = (
await Promise.all(
Array.from(message.reactions.cache.entries()).map(
async ([emoji, { users }]) => {
const reaction = await users.fetch();
return ` * ${emoji} ${Array.from(
reaction.values()
)
.map(({ username }) => `@${username}`)
.join(", ")}`;
}
)
)
).join("\n");
content += `\n${reactions}`;
}
if (message.attachments.size) {
const attachments = message.attachments.map(
(xyz) => xyz.url
);
content += `\nAttachments ${attachments}`;
}
return content;
})
)
).join("\n\n");
return `Transcript\n\n${generatedMessages}\n`;
}
function getTranscriptMessages(messages) {
return messages.sort((a, b) => a.createdTimestamp - b.createdTimestamp);
}
async function fetchMessages(channel) {
let messages = [];
// Discord's API limits fetching messages to 50 at a time. Continue requesting batches
// until we have no messages
// eslint-disable-next-line no-constant-condition
while (true) {
const temp = await channel.messages.fetch(
messages.length ? { before: messages[0].id } : undefined
);
const batch = [...temp.values()];
if (!batch.length) {
break;
}
messages = [...getTranscriptMessages(batch), ...messages];
}
return messages.map((message) => {
message.content = message.content.replace(
/<@!?(\d+)>/g,
(match, p1) => `@${channel.client.users.cache.get(p1).username}`
);
return message;
});
}
module.exports = {
name: "ticketclose",
description: "Close the ticket!",
run: async (client, message) => {
if (!message.channel.name.includes("ticket-"))
return message.channel.send("You cannot use that here!");
let channel = message.channel;
let user;
await message.guild.members
.fetch(message.channel.name.substring(7))
.then((member) => {
user = member;
})
.catch((error) => {
console.log(error);
});
const messages = await fetchMessages(channel);
let content = await generateContent(messages);
if (!user) {
message.channel.send(
"Error finding the user or user has left the server"
);
} else {
await user
.send(
`Transcript for your ticket in ${message.guild.name} Server`
)
.catch((err) => {
console.log(err);
});
await user
.send({
files: [
{
name: `${message.channel.name}.txt`,
attachment: Buffer.from(content)
}
]
})
.catch((err) => {
console.log(err);
});
}
message.channel.send(
`I have dmed you transcript if your dms are opened. Deleting channel in 30 seconds`
);
message.channel.send(
`Just in case Your dms are closed here is transcript`
);
message.channel.send({
files: [
{
name: `${message.channel.name}.txt`,
attachment: Buffer.from(content)
}
]
});
const logchannel = message.guild.channels.cache.find(
(channel) => channel.id === data["logChannelId"]
);
if (!logchannel) message.channel.send("Log channel not found");
else
logchannel.send({
content: `${user.displayName}`,
files: [
{
name: `${message.channel.name}.txt`,
attachment: Buffer.from(content)
}
]
});
setTimeout(function () {
message.channel.delete();
}, 30000);
},
aliases: []
};