diff --git a/Commands/add.js b/Commands/add.js index 1b5546f..3629b83 100644 --- a/Commands/add.js +++ b/Commands/add.js @@ -12,12 +12,6 @@ const cmdconfig = yaml.load(fs.readFileSync("./Configs/commands.yml", "utf8")); const Command = require("../Structures/Command.js"); -async function asyncForEach(array, callback) { - for (let index = 0; index < array.length; index++) { - await callback(array[index], index, array); - } -} - module.exports = new Command({ name: cmdconfig.TicketAdd, description: cmdconfig.TicketAddDesc, @@ -32,16 +26,10 @@ module.exports = new Command({ permission: "SEND_MESSAGES", async run(interaction) { - let SupportStaff = interaction.guild.roles.cache.find( - (SupportTeam) => - SupportTeam.name == supportbot.Staff || - SupportTeam.id == supportbot.Staff - ); - let Admins = interaction.guild.roles.cache.find( - (AdminUser) => - AdminUser.name == supportbot.Admin || AdminUser.id == supportbot.Admin - ); - if (!SupportStaff || !Admins) + const { getRole, getChannel, getCategory } = interaction.client; + let SupportStaff = await getRole(supportbot.Staff, interaction.guild); + let Admin = await getRole(supportbot.Admin, interaction.guild); + if (!SupportStaff || !Admin) return interaction.reply( "Some roles seem to be missing!\nPlease check for errors when starting the bot." ); @@ -55,7 +43,7 @@ module.exports = new Command({ if ( !interaction.member.roles.cache.has(SupportStaff.id) || - !interaction.member.roles.cache.has(Admins.id) + !interaction.member.roles.cache.has(Admin.id) ) return interaction.reply({ embeds: [NoPerms] }); diff --git a/Commands/close.js b/Commands/close.js index 2aed553..0c2b724 100644 --- a/Commands/close.js +++ b/Commands/close.js @@ -25,6 +25,7 @@ module.exports = new Command({ permission: "SEND_MESSAGES", async run(interaction) { + const { getRole, getChannel, getCategory } = interaction.client; await interaction.deferReply(); let TicketData = await JSON.parse( fs.readFileSync("./Data/TicketData.json", "utf8") @@ -37,16 +38,11 @@ module.exports = new Command({ return interaction.followUp({ embeds: [Exists] }); } let tUser = interaction.client.users.cache.get(TicketData.user); - let transcriptChannel = await interaction.guild.channels.cache.find( - (channel) => - channel.name == supportbot.TranscriptLog || - channel.id == supportbot.TranscriptLog - ); - let logChannel = await interaction.guild.channels.cache.find( - (channel) => - channel.name == supportbot.TicketLog || - channel.id == supportbot.TicketLog + let transcriptChannel = await getChannel( + supportbot.TranscriptLog, + interaction.guild ); + let logChannel = await getChannel(supportbot.TicketLog, interaction.guild); let reason = (await interaction.options?.getString("reason")) || "No Reason Provided."; @@ -135,11 +131,13 @@ module.exports = new Command({ .catch(async (err) => { console.error(err); }); - TicketData.subUsers.forEach(async (subUser) => { - interaction.client.users.cache - .get(subUser) - .send({ embeds: [transcriptEmbed], files: [file] }); - }); + if (TicketData.subUsers) { + TicketData.subUsers.forEach(async (subUser) => { + interaction.client.users.cache + .get(subUser) + .send({ embeds: [transcriptEmbed], files: [file] }); + }); + } await interaction.channel.delete().catch(async (error) => { console.error(error); if (error.code !== Discord.Constants.APIErrors.UNKNOWN_CHANNEL) { diff --git a/Commands/embed.js b/Commands/embed.js index 278a777..62aeaf1 100644 --- a/Commands/embed.js +++ b/Commands/embed.js @@ -31,17 +31,11 @@ module.exports = new Command({ permission: "SEND_MESSAGES", async run(interaction) { - let SupportStaff = interaction.guild.roles.cache.find( - (SupportTeam) => - SupportTeam.name === supportbot.Staff || - SupportTeam.id === supportbot.Staff - ); - let Admins = interaction.guild.roles.cache.find( - (AdminUser) => - AdminUser.name === supportbot.Admin || AdminUser.id === supportbot.Admin - ); + const { getRole, getChannel, getCategory } = interaction.client; + let SupportStaff = await getRole(supportbot.Staff, interaction.guild); + let Admin = await getRole(supportbot.Admin, interaction.guild); - if (!SupportStaff || !Admins) + if (!SupportStaff || !Admin) return interaction.reply( "Some roles seem to be missing!\nPlease check for errors when starting the bot." ); @@ -54,7 +48,7 @@ module.exports = new Command({ if ( interaction.member.roles.cache.has(SupportStaff.id) || - interaction.member.roles.cache.has(Admins.id) + interaction.member.roles.cache.has(Admin.id) ) { const EmbedTitle = interaction.options.getString("title"); const EmbedSubject = interaction.options.getString("message"); diff --git a/Commands/help.js b/Commands/help.js index 2ed6624..9de6870 100644 --- a/Commands/help.js +++ b/Commands/help.js @@ -25,16 +25,10 @@ module.exports = new Command({ permission: "SEND_MESSAGES", async run(interaction) { - let SupportStaff = interaction.guild.roles.cache.find( - (SupportTeam) => - SupportTeam.name === supportbot.Staff || - SupportTeam.id === supportbot.Staff - ); - let Admins = interaction.guild.roles.cache.find( - (AdminUser) => - AdminUser.name === supportbot.Admin || AdminUser.id === supportbot.Admin - ); - if (!SupportStaff || !Admins) + const { getRole, getChannel, getCategory } = interaction.client; + let SupportStaff = await getRole(supportbot.Staff, interaction.guild); + let Admin = await getRole(supportbot.Admin, interaction.guild); + if (!SupportStaff || !Admin) return interaction.reply( "Some roles seem to be missing!\nPlease check for errors when starting the bot." ); @@ -74,7 +68,7 @@ module.exports = new Command({ if ( interaction.member.roles.cache.has(SupportStaff.id) || - interaction.member.roles.cache.has(Admins.id) + interaction.member.roles.cache.has(Admin.id) ) { HelpEmbed1.addFields({ name: "🔐 Staff Commands\n", diff --git a/Commands/remove.js b/Commands/remove.js index de144d4..14c17b9 100644 --- a/Commands/remove.js +++ b/Commands/remove.js @@ -32,16 +32,10 @@ module.exports = new Command({ permission: "SEND_MESSAGES", async run(interaction) { - let SupportStaff = interaction.guild.roles.cache.find( - (SupportTeam) => - SupportTeam.name == supportbot.Staff || - SupportTeam.id == supportbot.Staff - ); - let Admins = interaction.guild.roles.cache.find( - (AdminUser) => - AdminUser.name == supportbot.Admin || AdminUser.id == supportbot.Admin - ); - if (!SupportStaff || !Admins) + const { getRole, getChannel, getCategory } = interaction.client; + let Staff = await getRole(supportbot.Staff, interaction.guild); + let Admin = await getRole(supportbot.Admin, interaction.guild); + if (!Staff || !Admin) return interaction.reply( "Some roles seem to be missing!\nPlease check for errors when starting the bot." ); @@ -54,8 +48,8 @@ module.exports = new Command({ .setColor(supportbot.WarningColour); if ( - !interaction.member.roles.cache.has(SupportStaff.id) || - !interaction.member.roles.cache.has(Admins.id) + !interaction.member.roles.cache.has(Staff.id) || + !interaction.member.roles.cache.has(Admin.id) ) return interaction.reply({ embeds: [NoPerms] }); let TicketData = await JSON.parse( diff --git a/Commands/suggest.js b/Commands/suggest.js index f06db63..5182999 100644 --- a/Commands/suggest.js +++ b/Commands/suggest.js @@ -25,10 +25,10 @@ module.exports = new Command({ permission: "SEND_MESSAGES", async run(interaction) { - const suggestChannel = interaction.guild.channels.cache.find( - (channel) => - channel.name === supportbot.SuggestionChannel || - channel.id === supportbot.SuggestionChannel + const { getRole, getChannel, getCategory } = interaction.client; + const suggestChannel = await getChannel( + supportbot.SuggestionChannel, + interaction.guild ); const NoChannel = new Discord.MessageEmbed() diff --git a/Commands/ticket.js b/Commands/ticket.js index 636fd5a..384fa00 100644 --- a/Commands/ticket.js +++ b/Commands/ticket.js @@ -18,12 +18,6 @@ const cmdconfig = yaml.load(fs.readFileSync("./Configs/commands.yml", "utf8")); const Command = require("../Structures/Command.js"); const TicketNumberID = require("../Structures/TicketID.js"); -async function asyncForEach(array, callback) { - for (let index = 0; index < array.length; index++) { - await callback(array[index], index, array); - } -} - module.exports = new Command({ name: cmdconfig.OpenTicket, description: cmdconfig.OpenTicketDesc, @@ -37,19 +31,16 @@ module.exports = new Command({ permission: "SEND_MESSAGES", async run(interaction) { + const { getRole, getChannel, getCategory } = interaction.client; let reactionUser = interaction.guild.members.cache.get(interaction.user.id); if ( - reactionUser.roles.cache.find( - (role) => - role.name == supportbot.TicketBlackListRole || - role.id == supportbot.TicketBlackListRole + reactionUser.roles.cache.has( + getRole(supportbot.TicketBlackListRole, interaction.guild).id ) ) { return interaction - .reply({ - content: `<@${interaction.user.id}> ${supportbot.TicketBlackListMessage}`, - }) + .reply(supportbot.TicketBlackListMessage) .then((msg) => { setTimeout(() => msg.delete(), 3500); }); @@ -78,41 +69,30 @@ module.exports = new Command({ ) { return await interaction.reply({ embeds: [TicketExists] }); } - const Staff = interaction.guild.roles.cache.find( - (SupportTeam) => - SupportTeam.name === supportbot.Staff || - SupportTeam.id === supportbot.Staff + const Staff = await getRole(supportbot.Staff, interaction.guild); + const Admin = await getRole(supportbot.Admin, interaction.guild); + const DeptRole1 = await getRole( + supportbot.DepartmentRole_1, + interaction.guild ); - const Admins = interaction.guild.roles.cache.find( - (AdminUser) => - AdminUser.name === supportbot.Admin || AdminUser.id === supportbot.Admin + const DeptRole2 = await getRole( + supportbot.DepartmentRole_2, + interaction.guild ); - const DeptRole1 = interaction.guild.roles.cache.find( - (DepartmentRole) => - DepartmentRole.name === supportbot.DepartmentRole_1 || - DepartmentRole.id === supportbot.DepartmentRole_1 + const DeptRole3 = await getRole( + supportbot.DepartmentRole_3, + interaction.guild ); - const DeptRole2 = interaction.guild.roles.cache.find( - (DepartmentRole) => - DepartmentRole.name === supportbot.DepartmentRole_2 || - DepartmentRole.id === supportbot.DepartmentRole_2 - ); - const DeptRole3 = interaction.guild.roles.cache.find( - (DepartmentRole) => - DepartmentRole.name === supportbot.DepartmentRole_3 || - DepartmentRole.id === supportbot.DepartmentRole_3 - ); - if (!Staff || !Admins || !DeptRole1 || !DeptRole2 || !DeptRole3) + if (!Staff || !Admin || !DeptRole1 || !DeptRole2 || !DeptRole3) return interaction.reply({ content: "Some roles seem to be missing!\nPlease check for errors when starting the bot.", ephemeral: true, }); const Author = interaction.user; - let TicketCategory = interaction.guild.channels.cache.find( - (category) => - category.name === supportbot.TicketCategory || - category.id === supportbot.TicketCategory + let TicketCategory = await getCategory( + supportbot.TicketCategory, + interaction.guild ); const ticketChannel = await interaction.guild.channels.create( `${supportbot.TicketPrefix}${ticketNumberID}`, @@ -121,7 +101,7 @@ module.exports = new Command({ parent: TicketCategory.id, permissionOverwrites: [ { - id: Admins.id, + id: Admin.id, allow: [Permissions.FLAGS.VIEW_CHANNEL], }, { @@ -170,7 +150,7 @@ module.exports = new Command({ await interaction.reply({ embeds: [CreatedTicket], ephemeral: true }); if (supportbot.AllowTicketMentions) { - await ticketChannel.send({ content: `${interaction.user}` }); + await ticketChannel.send(`${interaction.user}`); } const TicketMessage = new MessageEmbed() @@ -290,7 +270,7 @@ module.exports = new Command({ }); if (supportbot.AllowTicketMentions) { - await ticketChannel.send({ content: "@here" }); + await ticketChannel.send("@here"); } await collector.message.edit({ embeds: [TicketMessage], diff --git a/Events/ready.js b/Events/ready.js index bbf436d..9c304bc 100644 --- a/Events/ready.js +++ b/Events/ready.js @@ -15,6 +15,7 @@ const panelconfig = yaml.load( const Event = require("../Structures/Event.js"); module.exports = new Event("ready", async (client) => { + const { getRole, getChannel, getCategory } = client; client.user.setActivity(supportbot.BotActivity, { type: supportbot.ActivityType, url: supportbot.StreamingURL, @@ -92,21 +93,23 @@ module.exports = new Event("ready", async (client) => { supportbot.TranscriptLog, panelconfig.Channel, ]; - const missingR = []; - const missingC = []; + const categories = [supportbot.TicketCategory]; + const missingC = []; + const missingR = []; + const missingCat = []; for (let c of channels) { - const find = await client.channels.cache.find( - (channel) => channel.name === c || channel.id === c - ); + const find = await getChannel(c, client.guilds.cache.first()); if (!find) missingC.push(c); } for (let r of roles) { - const find = await client.guilds.cache - .first() - .roles.cache.find((role) => role.name === r || role.id === r); + const find = await getRole(r, client.guilds.cache.first()); if (!find) missingR.push(r); } + for (let cat of categories) { + const find = await getCategory(cat, client.guilds.cache.first()); + if (!find) missingCat.push(cat); + } if (missingR.length > 0) { console.log( @@ -125,6 +128,14 @@ module.exports = new Event("ready", async (client) => { )}` ); } + if (missingCat.length > 0) { + console.log( + "\u001b[33m", + `[WARN] The following Categories could not be found! Please check your configs!\n${missingCat.join( + ", " + )}` + ); + } if (panelconfig.TicketPanel) { let chan1 = client.channels.cache.find( (channel) => channel.name === panelconfig.Channel diff --git a/Structures/Client.js b/Structures/Client.js index fbb847e..f12cb87 100644 --- a/Structures/Client.js +++ b/Structures/Client.js @@ -19,6 +19,25 @@ class Client extends Discord.Client { this.commands = new Discord.Collection(); } + async getChannel(channel, guild) { + return guild.channels.cache.find( + (c) => + (c.type === "GUILD_TEXT" || c.type === "GUILD_NEWS") && + (c.id === channel || c.name.toLowerCase() === channel.toLowerCase()) + ); + } + async getRole(role, guild) { + return guild.roles.cache.find( + (r) => r.id === role || r.name.toLowerCase() === role.toLowerCase() + ); + } + async getCategory(category, guild) { + return guild.channels.cache.find( + (c) => + c.type === "GUILD_CATEGORY" && + (c.id === category || c.name.toLowerCase() === category.toLowerCase()) + ); + } start(token) { const commandFiles = fs @@ -73,7 +92,6 @@ class Client extends Discord.Client { })); this.removeAllListeners(); - this.on("ready", async () => { const allCommands = slashCommands.concat(slashCommands1); await this.application?.commands.set(allCommands, supportbot.Guild);