Files
SupportBot/commands/ticket.js
T

166 lines
8.4 KiB
JavaScript
Raw Normal View History

2019-06-09 23:52:41 +01:00
// SupportBot
2020-01-26 22:35:45 +00:00
// Created by © 2020 Emerald Services
2020-04-16 16:43:16 +01:00
// Command: Ticket
2019-06-09 23:52:41 +01:00
const Discord = require("discord.js");
2020-01-26 22:35:45 +00:00
const bot = new Discord.Client();
2019-06-09 23:52:41 +01:00
2020-01-26 22:35:45 +00:00
const fs = require("fs");
const yaml = require('js-yaml');
const supportbot = yaml.load(fs.readFileSync('./supportbot-config.yml', 'utf8'));
2020-01-29 20:06:28 +00:00
const TicketNumberID = require('../utility/TicketNumber');
2019-06-09 23:52:41 +01:00
2020-01-29 19:59:14 +00:00
exports.run = async (bot, message, args) => {
2020-01-26 22:35:45 +00:00
console.log(`\u001b[33m`, `[${supportbot.Bot_Name}] > `, `\u001b[31;1m`, `${message.author.tag}`, `\u001b[32;1m`, `has executed`, `\u001b[31;1m`, `${supportbot.Prefix}${supportbot.Ticket_Command}`);
2020-01-29 19:08:00 +00:00
// Ticket Number ID Settings
2020-01-29 19:59:14 +00:00
let ticketNumberID = TicketNumberID.pad(message.guild.id);
2020-01-29 19:08:00 +00:00
// Ticket Subject Settings
const subject = args.join(" ") || `${supportbot.Default_Ticket_Reason}`;
// Ticket Limitiations
2020-04-06 18:15:27 -04:00
const alreadyopen = new Discord.MessageEmbed()
2020-01-29 19:59:14 +00:00
.setDescription(`:x: Cannot create a ticket because **${supportbot.Ticket_Channel_Name}-${ticketNumberID}** already exists.`)
2020-01-29 19:08:00 +00:00
.setColor(supportbot.EmbedColour)
2020-01-05 16:33:31 +00:00
2020-01-29 19:08:00 +00:00
// Ticket Creation
2020-04-16 16:43:16 +01:00
if (message.guild.channels.cache.find(TicketChannel => TicketChannel.name === `${supportbot.Ticket_Channel_Name}-` + message.author.username)) return message.channel.send(alreadyopen);
message.guild.channels.create(`${supportbot.Ticket_Channel_Name}-${ticketNumberID}`, {
2020-01-29 19:08:00 +00:00
type: 'text',
}).then(TicketChannel => {
2019-07-24 16:52:30 -06:00
2019-06-09 23:52:41 +01:00
// Roles
2020-04-16 16:43:16 +01:00
let staff = message.guild.roles.cache.find(supportRole => supportRole.name === `${supportbot.StaffRole}`)
2020-04-17 02:48:38 +01:00
let everyone = message.guild.roles.cache.find(everyoneRole => everyoneRole.name === supportbot.everyone)
2020-04-16 16:43:16 +01:00
let department = message.guild.roles.cache.find(DepartmentRole => DepartmentRole.name === `${supportbot.Department_Role_1}`)
let department2 = message.guild.roles.cache.find(DepartmentRole => DepartmentRole.name === `${supportbot.Department_Role_2}`)
let department3 = message.guild.roles.cache.find(DepartmentRole => DepartmentRole.name === `${supportbot.Department_Role_3}`)
2019-06-09 23:52:41 +01:00
// Permissions
2020-04-17 02:48:38 +01:00
TicketChannel.updateOverwrite(everyone, { VIEW_CHANNEL: false });
TicketChannel.updateOverwrite(message.author, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: false, READ_MESSAGES: true });
TicketChannel.updateOverwrite(staff, { VIEW_CHANNEL: true, CREATE_INVITE: false, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
TicketChannel.updateOverwrite(department, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
TicketChannel.updateOverwrite(department2, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
TicketChannel.updateOverwrite(department3, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
2019-06-09 23:52:41 +01:00
// Category
2020-04-16 16:43:16 +01:00
let category = message.guild.channels.cache.find(c => c.name === supportbot.category);
2019-06-09 23:52:41 +01:00
if (category) {
TicketChannel.setParent(category.id);
} else {
2020-04-16 16:43:16 +01:00
if (message.guild.channels.cache.get(supportbot.category)) {
TicketChannel.setParent(message.guild.channels.cache.get(supportbot.category).id);
2019-06-09 23:52:41 +01:00
}
}
2020-04-06 18:15:27 -04:00
const ticketopened = new Discord.MessageEmbed()
2019-11-17 22:24:05 +00:00
.setTitle(":white_check_mark: Support Ticket Created")
.setDescription(`<@${message.author.id}> your support ticket created successfully`)
.addField("Your Ticket:", `<#${TicketChannel.id}>`)
2020-04-16 16:43:16 +01:00
.setColor(supportbot.SuccessColour)
2019-06-09 23:52:41 +01:00
message.channel.send({embed: ticketopened});
// Ticket Message - ( Able to edit this message via the settings.json file )
2020-01-26 22:35:45 +00:00
const ticketMessage = `Hi! <@${message.author.id}>\n${supportbot.Ticket_Message}`;
2019-06-09 23:52:41 +01:00
2020-01-26 22:35:45 +00:00
const Department_1 = supportbot.Ticket_Department_1;
const Department_2 = supportbot.Ticket_Department_2;
const Department_3 = supportbot.Ticket_Department_3;
2019-08-16 18:45:18 +01:00
2020-01-26 22:35:45 +00:00
const Emoji_1 = supportbot.Ticket_Department_Emoji_1;
const Emoji_2 = supportbot.Ticket_Department_Emoji_2;
const Emoji_3 = supportbot.Ticket_Department_Emoji_3;
2019-08-16 18:45:18 +01:00
2020-04-06 18:15:27 -04:00
const TicketMessage = new Discord.MessageEmbed()
2019-08-16 18:45:18 +01:00
.setDescription(ticketMessage, true)
.addField("Available Departments", `${Emoji_1} ${Department_1}\n${Emoji_2} ${Department_2}\n${Emoji_3} ${Department_3}`)
2020-01-26 22:35:45 +00:00
.setColor(supportbot.Ticket_Colour)
.setFooter(supportbot.EmbedFooter)
2019-06-09 23:52:41 +01:00
2019-08-16 18:45:18 +01:00
if (subject != 'No Subject.') {
2019-12-03 23:17:47 +00:00
TicketMessage.addField("Reason", subject);
2019-08-16 18:45:18 +01:00
}
TicketChannel.send({embed: TicketMessage}).then(function(msg) {
msg.react(Emoji_1).then(() => msg.react(Emoji_2)).then(() => msg.react(Emoji_3));
const filter = (reaction, user) => {
return [Emoji_1, Emoji_2, Emoji_3].includes(reaction.emoji.name) && user.id !== msg.author.id;
};
msg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === `${Emoji_1}`) {
2020-04-17 02:48:38 +01:00
TicketChannel.updateOverwrite(message.author, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
TicketChannel.updateOverwrite(staff, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
TicketChannel.updateOverwrite(department, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
2020-04-16 16:43:16 +01:00
const dept1 = new Discord.MessageEmbed()
.setDescription(`You have requested to contact ${department}. Please wait patiently for someone to reach out to you.`)
.setColor(supportbot.SuccessColour)
TicketChannel.send(dept1)
2019-08-16 18:45:18 +01:00
}
if (reaction.emoji.name === `${Emoji_2}`) {
2020-04-17 02:48:38 +01:00
TicketChannel.updateOverwrite(message.author, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
TicketChannel.updateOverwrite(staff, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
TicketChannel.updateOverwrite(department2, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
2019-08-16 18:45:18 +01:00
2020-04-16 16:43:16 +01:00
const dept2 = new Discord.MessageEmbed()
.setDescription(`You have requested to contact ${department2}. Please wait patiently for someone to reach out to you.`)
.setColor(supportbot.SuccessColour)
TicketChannel.send(dept2)
2019-08-16 18:45:18 +01:00
}
if (reaction.emoji.name === `${Emoji_3}`) {
2020-04-17 02:48:38 +01:00
TicketChannel.updateOverwrite(message.author, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
TicketChannel.updateOverwrite(staff, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
TicketChannel.updateOverwrite(department3, { VIEW_CHANNEL: true, CREATE_INVITE: false, SEND_MESSAGES: true, READ_MESSAGES: true });
2020-04-16 16:43:16 +01:00
const dept3 = new Discord.MessageEmbed()
.setDescription(`You have requested to contact ${department3}. Please wait patiently for someone to reach out to you.`)
.setColor(supportbot.SuccessColour)
TicketChannel.send(dept3)
2019-08-16 18:45:18 +01:00
}
});
});
2019-06-09 23:52:41 +01:00
// Ticket Logging
2020-04-06 18:15:27 -04:00
const logEmbed = new Discord.MessageEmbed()
2020-04-16 16:43:16 +01:00
.setTitle(":ticket: Ticket Log")
2020-01-29 19:59:14 +00:00
.addField("Ticket ID", ticketNumberID, true)
2019-06-09 23:52:41 +01:00
.addField("User", `<@${message.author.id}>`, true)
2020-01-29 19:59:14 +00:00
.addField("Channel", `ticket#${ticketNumberID}`, true)
2020-01-26 22:35:45 +00:00
.setColor(supportbot.EmbedColour)
.setFooter(supportbot.EmbedFooter)
2019-06-09 23:52:41 +01:00
if (subject != 'No Subject.') {
logEmbed.addField('Subject', subject, true);
}
2020-04-16 16:43:16 +01:00
let locateChannel = message.guild.channels.cache.find(LocateChannel => LocateChannel.name === `${supportbot.Ticket_Logs}`)
const errornochannel = new Discord.MessageEmbed()
.setTitle("SupportBot Error!")
.setDescription(`:x: **Error!** Channel not Found, This command cannot be executed proberbly as their is no channel within this server.\nThis is configurable via **supportbot-config.yml**\n\nChannel Required: \`${supportbot.Ticket_Logs}\`\n\nError Code: \`SB-03\``)
.setColor(supportbot.ErrorColour);
if(!locateChannel) return message.channel.send(errornochannel);
locateChannel.send(logEmbed)
2019-06-09 23:52:41 +01:00
}).catch(err=>{console.error(err)});
2020-01-26 22:35:45 +00:00
};
2019-06-09 23:52:41 +01:00
exports.help = {
2020-01-26 22:35:45 +00:00
name: supportbot.Ticket_Command,
};