UNPKG

easy-tickets

Version:

easy package for discord.js at any version

207 lines 8.99 kB
const { User } = require('discord.js'); const { MessageEmbed, MessageButton, MessageActionRow, Client } = require('discord.js'); const db = require('quick.db') class manager { static async setTicketChannel(channel, options = { embed: { title: String, thumbnail: String, description: String, color: 'BLACK', footer: String, timestamp: Boolean }, button: { label: String, emoji: '🔓', id: String, style: String } } ) { if (!channel || !channel.isText()) { throw new Error(`INVAILD_CHANNEL => channel must be \`GUILD_TEXT\` channel!`) } let style = options.button && options.button.style ? options.button.style : "SECONDARY"; let arr = ["SECONDARY", "PRIMARY", "DANGER", "SUCCESS"] if (style !== 'SECONDARY' && style !== 'PRIMARY' && style !== 'DANGER' && style !== 'SUUCESS') { throw new Error(`INVAILD_STYLE => must provide a vaild style !`) } var optionsD = { embed: { title: "Ticket System", thumbnail: null, description: "Click on the button bellow to create a ticket!", color: 'BLACK', footer: "Developer: KANOX#1323", timestamp: true }, button: { label: 'click here', emoji: '🔓', id: "OpenTicket1", style: "SECONDARY" } } if(!options.embed){ options.embed = optionsD.embed } if(!options.button){ options.button = optionsD.button } let embed = new MessageEmbed() .setTitle(options.embed.title ? options.embed.title : optionsD.embed.title) .setDescription(options.embed.description ? options.embed.description : optionsD.embed.description) .setThumbnail(options.embed.thumbnail ? options.embed.thumbnail : optionsD.embed.thumbnail) .setColor(options.embed.color ? options.embed.color : optionsD.embed.color) .setFooter(options.embed.footer ? options.embed.footer : optionsD.embed.footer) if (options.embed && options.embed.timestamp == true) embed.setTimestamp(); let button = new MessageActionRow() .addComponents( new MessageButton() .setStyle(options.button.style ? options.button.style : optionsD.button.style) .setLabel(options.button.label ? options.button.label : optionsD.button.label) .setEmoji(options.button.emoji ? options.button.emoji : optionsD.button.emoji) .setCustomId(options.button.id ? options.button.id : optionsD.button.id) ) channel.send({ embeds: [embed], components: [button] }) } static async createTicket(guild, category, options = { config: { ownerID: User, allowRoles: Array, }, embed: { title: String, thumbnail: Boolean, description: String, color: 'BLACK', footer: String, timestamp: Boolean }, message: { content: String }, button: { label: String, emoji: '🔒', id: "OpenTicket", style: "DANGER" } } ) { if (!category || category.type != 'GUILD_CATEGORY') { throw new Error(`INVAILD_CATEGORY => channel must be \`GUILD_CATEGORY\` category!`) } if(!options.config){ throw new Error(`INVAILD_CATEGORY => must provide the config options !`) } if (!options.config.ownerID) { throw new Error(`INVAILD_MEMBER => must provide a vaild memberID !`) } if (!options.config.allowRoles || (options.config.allowRoles || []).length === 0) { throw new Error(`INVAILD_ARRAY => must provide an array of roles that allowed to view the ticket channel !`) } if (options.message && !options.message.content) { throw new Error(`INVAILD_CONTENT => you must type content !`) } var optionsD = { config: { ownerID: User, allowRoles: Array, }, embed: { title: "Ticket Created", thumbnail: null, description: "Click on the button bellow to delete the ticket!", color: 'BLACK', footer: "Developer: KANOX#1323", timestamp: true }, message: { content: null }, button: { label: "Delete", emoji: '🔒', id: "CloseTicket", style: "DANGER" } } if(!options.embed && !options.message){ options.embed = optionsD.embed options.message.content = null; } if(!options.button){ options.button = optionsD.button } let style = options.button && options.button.style ? options.button.style : "DANGER"; if (style !== 'SECONDARY' && style !== 'PRIMARY' && style !== 'DANGER' && style !== 'SUUCESS') { throw new Error(`INVAILD_STYLE => must provide a vaild style !`) } var msg = ""; let embed = new MessageEmbed() if (options.embed && !options.message) { embed .setTitle(options.embed.title ? options.embed.title : optionsD.embed.title) .setDescription(options.embed.description ? options.embed.description : optionsD.embed.description) .setThumbnail(options.embed.thumbnail ? options.embed.thumbnail : optionsD.embed.thumbnail) .setColor(options.embed.color ? options.embed.color : optionsD.embed.color) .setFooter(options.embed.footer ? options.embed.footer : optionsD.embed.footer) if (options.embed && options.embed.timestamp == true) embed.setTimestamp(); } else if (!options.embed && options.message) { msg = options.message.content ? options.message.content : "Click on the button bellow to close the ticket"; } let button = new MessageActionRow() .addComponents( new MessageButton() .setStyle(options.button.style ? options.button.style : optionsD.button.style) .setLabel(options.button.label ? options.button.label : optionsD.button.label) .setEmoji(options.button.emoji ? options.button.emoji : optionsD.button.emoji) .setCustomId(options.button.id ? options.button.id : optionsD.button.id) ) let ticketCount = db.get(`ticketCount_${guild.id}`) || 0; if (ticketCount === 9999) db.delete(`ticketCount_${guild.id}`) let newTicketCount = db.get(`ticketCount_${guild.id}`) || 0; db.add(`ticketCount_${guild.id}`, 1) guild.channels.create(`ticket-${newTicketCount + 1}`, { type: 'GUILD_TEXT', parent: category.id, permissionOverwrites: [ { id: guild.roles.everyone.id, deny: ["VIEW_CHANNEL"], }, { id: options.config.ownerID, allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY'] } ] }).then(async (channel) => { options.config.allowRoles.map(role => { channel.permissionOverwrites.edit(role, { VIEW_CHANNEL: true, SEND_MESSAGES: true, READ_MESSAGE_HISTORY: true }) }) if (options.embed && !options.message) { channel.send({ embeds: [embed], components: [button] }) } else if (!options.embed && options.message) { channel.send({ content: `${msg}`, components: [button] }) }else if(options.embed && options.message){ channel.send({ content: `${msg}`, embeds: [embed], components: [button] }) } }) } static async deleteTicket(channel){ if(!channel){ throw new Error(`INVAILD_CHANNEL => must provide a vaild channel !`) } channel.delete() } } module.exports = manager;