forked from Cyanic76/discord-modmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
265 lines (251 loc) · 12.2 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
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
const Discord = require("discord.js");
const db = require("quick.db");
const config = require("./config.json");
const table = new db.table("Tickets");
// declare the client
const client = new Discord.Client();
// do something when the bot is logged in
client.on("ready", () => {
console.log(`Successfully logged in as ${client.user.tag}.`)
console.log(`Guild ID: ${config.guild}\nLogs channel ID: ${config.log}\nPrefix: ${config.prefix}`)
})
client.on("message", async message => {
if(message.channel.type === "dm"){
const dbTable = new db.table("Tickets");
if(message.author.bot) return;
if(message.content.includes("@everyone") || message.content.includes("@here")) return message.author.send("I'm sorry, but you can not use everyone/here mentions in a modmail thread.")
let active = await dbTable.get(`support_${message.author.id}`);
let guild = client.guilds.cache.get(config.guild);
let channel, found = true;
let user = await dbTable.get(`isBlocked${message.author.id}`);
if(user === true || user === "true") return message.react("❌");
if(active === null){
active = {};
let everyone = guild.roles.cache.get(guild.roles.everyone.id);
let bot = guild.roles.cache.get(config.roles.bot);
await dbTable.add("ticket", 1)
let actualticket = await dbTable.get("ticket");
channel = await guild.channels.create(`${message.author.username}-${message.author.discriminator}`, { type: 'text', reason: `New modmail thread: #${actualticket}.` });
channel.setParent(config.ticketCategory);
channel.setTopic(`#${actualticket} | Use ${config.prefix}complete to close this ticket | ${message.author.username}'s ticket`)
config.roles.mod.forEach(mod => {
let modrole = guild.roles.cache.get(mod);
if(!modrole){
console.warn("I could not fetch this role. Does it exist? Is this the right role ID?")
} else {
channel.createOverwrite(modrole, {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
READ_MESSAGE_HISTORY: true
});
}
})
channel.createOverwrite(everyone, {
VIEW_CHANNEL: false
});
channel.createOverwrite(bot, {
VIEW_CHANNEL: true,
SEND_MESSAGES: true,
READ_MESSAGE_HISTORY: true,
MANAGE_MESSAGES: true
})
let author = message.author;
const newTicket = new Discord.MessageEmbed()
.setColor("GREEN")
.setAuthor(author.tag, author.avatarURL({dynamic: true}))
.setTitle(`Ticket #${actualticket}`)
.addField("Channel", `<#${channel.id}>`, true)
let supportServer = client.guilds.cache.get(config.guild);
if(config.logs){
try {
supportServer.channels.cache.get(config.log).send({embed: newTicket})
} catch(e) {
if(e) supportServer.channels.cache.get(config.log).send(`Ticket #${actualticket} was created by ${author.tag}.`)
}
}
const newChannel = new Discord.MessageEmbed()
.setColor("BLUE").setAuthor(author.tag, author.avatarURL())
.setDescription(`Ticket #${actualticket} created.\nUser: ${author}\nID: ${author.id}`)
.setTimestamp()
try {
supportServer.channels.cache.get(channel.id).send({embed:newChannel});
} catch(e) {
supportServer.channels.cache.get(channel.id).send(`This ticket was created by ${author.tag}.`)
}
message.author.send(`Thanks for contacting the support team! We'll get back to you quickly.\nYour ticket ID is #${actualticket}.`)
active.channelID = channel.id;
active.targetID = author.id;
}
channel = client.channels.cache.get(active.channelID);
var msg = message.content;
var isPaused = await dbTable.get(`suspended${message.author.id}`);
var isBlocked = await dbTable.get(`isBlocked${message.author.id}`);
if(isPaused === true){
return message.channel.send("Sorry, but your ticket is currently paused. I'll message you back when the support team unpause it.")
}
if(isBlocked === true) return; // the user is blocked, so we're just gonna move on.
if(message.attachments.size > 0){
let attachment = new Discord.MessageAttachment(message.attachments.first().url)
try {
client.channels.cache.get(active.channelID).send(`${message.author.username} > ${text}`, {files: [message.attachments.first().url]})
} catch(e) {
if(e) client.guilds.cache.get(config.guild).channels.cache.get(active.channelID).send(`${message.author.username} > ${text}`, {files: [message.attachments.first().url]})
}
} else {
try {
client.channels.cache.get(active.channelID).send(`${message.author.username} > ${text}`);
} catch(e) {
if(e) client.guilds.cache.get(config.guild).channels.cache.get(active.channelID).send(`${message.author.username} > ${text}`)
}
}
await dbTable.set(`support_${message.author.id}`, active);
await dbTable.set(`supportChannel_${active.channelID}`, message.author.id);
return;
}
if(message.author.bot) return;
var support = await dbTable.get(`supportChannel_${message.channel.id}`);
let supportServer = client.guilds.cache.get(config.guild);
if(support){
var support = await table.get(`support_${support}`);
let supportUser = client.users.cache.get(support.targetID);
if(!supportUser) return message.channel.delete();
// reply (with user and role)
if(message.content.startsWith(`${config.prefix}reply`)){
var isPause = await table.get(`suspended${support.targetID}`);
let isBlock = await table.get(`isBlocked${support.targetID}`);
if(isPause === true) return message.channel.send("This ticket already paused. Unpause it to continue.")
if(isBlock === true) return message.channel.send("The user is blocked. Unblock them to continue or close the ticket.")
var args = message.content.split(" ").slice(1)
let msg = args.join(" ");
message.react("✅");
if(message.attachments.size > 0){
let attachment = new Discord.MessageAttachment(message.attachments.first().url)
return supportUser.send(`${message.author.username} > ${msg}`, {files: [message.attachments.first().url]})
} else {
return supportUser.send(`${message.author.username} > ${msg}`);
}
};
// anonymous reply
if(message.content.startsWith(`${config.prefix}areply`)){
var isPause = await table.get(`suspended${support.targetID}`);
let isBlock = await table.get(`isBlocked${support.targetID}`);
if(isPause === true) return message.channel.send("This ticket already paused. Unpause it to continue.")
if(isBlock === true) return message.channel.send("The user is blocked. Unblock them to continue or close the ticket.")
var args = message.content.split(" ").slice(1)
let msg = args.join(" ");
message.react("✅");
return supportUser.send(`Support Team > ${msg}`);
};
// print user ID
if(message.content === `${config.prefix}id`){
return message.channel.send(`User's ID is **${support.targetID}**.`);
};
// suspend a thread
if(message.content === `${config.prefix}pause`){
var isPause = await table.get(`suspended${support.targetID}`);
if(isPause === true || isPause === "true") return message.channel.send("This ticket already paused. Unpause it to continue.")
await table.set(`suspended${support.targetID}`, true);
var suspend = new Discord.MessageEmbed()
.setDescription(`⏸️ This thread has been **locked** and **suspended**. Do \`${config.prefix}continue\` to cancel.`)
.setTimestamp()
.setColor("YELLOW")
message.channel.send({embed: suspend});
return client.users.cache.get(support.targetID).send("Your ticket has been paused. We'll send you a message when we're ready to continue.")
};
// continue a thread
if(message.content === `${config.prefix}continue`){
var isPause = await table.get(`suspended${support.targetID}`);
if(isPause === null || isPause === false) return message.channel.send("This ticket was not paused.");
await table.delete(`suspended${support.targetID}`);
var c = new Discord.MessageEmbed()
.setDescription("▶️ This thread has been **unlocked**.")
.setColor("BLUE").setTimestamp()
message.channel.send({embed: c});
return client.users.cache.get(support.targetID).send("Hi! Your ticket isn't paused anymore. We're ready to continue!");
}
// block a user
if(message.content.startsWith(`${config.prefix}block`)){
var args = message.content.split(" ").slice(1)
let reason = args.join(" ");
if(!reason) reason = `Unspecified.`
let user = client.users.fetch(`${support.targetID}`); // djs want a string here
const blocked = new Discord.MessageEmbed()
.setColor("RED").setAuthor(user.tag)
.setTitle("User blocked")
.addField("Channel", `<#${message.channel.id}>`, true)
.addField("Reason", reason, true)
if(config.logs){
client.channels.cache.get(config.log).send({embed: blocked})
}
let isBlock = await table.get(`isBlocked${support.targetID}`);
if(isBlock === true) return message.channel.send("The user is already blocked.")
await table.set(`isBlocked${support.targetID}`, true);
var c = new Discord.MessageEmbed()
.setDescription("⏸️ The user has been blocked from the modmail. You may now close the ticket or unblock them to continue.")
.setColor("RED").setTimestamp()
message.channel.send({embed: c});
return;
}
// complete
if(message.content.toLowerCase() === `${config.prefix}complete`){
var embed = new Discord.MessageEmbed()
.setDescription(`This ticket will be deleted in **10** seconds...\n:lock: This thread has been locked and closed.`)
.setColor("RED").setTimestamp()
message.channel.send({embed: embed})
var timeout = 10000
setTimeout(() => {end(support.targetID);}, timeout)
}
async function end(userID){
let actualticket = await table.get("ticket");
message.channel.delete()
let u = await client.users.fetch(userID);
let end_log = new Discord.MessageEmbed()
.setColor("RED").setAuthor(u.tag, u.avatarURL())
.setDescription(`Ticket #${actualticket} closed.\nUser: ${u.username}\nID: ${userID}`)
.setTimestamp()
await table.delete(`support_${userID}`);
supportServer.channels.cache.get(config.log).send({embed:end_log});
return client.users.cache.get(support.targetID).send(`Thanks for getting in touch with us. If you wish to open a new ticket, feel free to message me.\nYour ticket #${actualticket} has been closed.`)
}
};
})
client.on("message", async message => {
if(message.content.startsWith(`${config.prefix}unblock`)){
if(message.guild.member(message.author).roles.cache.has(config.roles.mod)){
var args = message.content.split(" ").slice(1);
client.users.fetch(`${args[0]}`).then(async user => {
let data = await table.get(`isBlocked${args[0]}`);
if(data === true){
await table.delete(`isBlocked${args[0]}`);
return message.channel.send(`Successfully unblocked ${user.username} (${user.id}) from the modmail service.`);
} else {
return message.channel.send(`${user.username} (${user.id}) is not blocked from the modmail at the moment.`)
}
}).catch(err => {
if(err) return message.channel.send("Unknown user.");
})
} else {
return message.channel.send("You can not use that.");
}
}
})
client.on("guildMemberRemove", async member => {
if(config.deleteTicketOnLeave === true){
let active = await table.get(`support_${message.author.id}`);
if(active === null) return;
client.channels.cache.get(active.channelID).delete();
await table.delete(`support_${message.author.id}`);
let end_log = new Discord.MessageEmbed()
.setColor("RED").setAuthor(member.tag, member.avatarURL())
.setDescription(`Ticket #${actualticket} closed because the user has left the server.\nUser: ${member.username}\nID: ${member.id}`)
.setTimestamp()
supportServer.channels.cache.get(config.log).send({embed:end_log});
return;
} else return;
})
/*
just in case:
the token should not be here.
the token should be in the 1st line of the process.env file instead.
*/
client.login(process.env.TOKEN); // Log the bot in