Added getRole, getChannel, getCategory functions

This commit is contained in:
Noah committed 2021-12-15 00:19:40 -06:00
1 parent 6107d268f7
commit 2c7be5882c
9 files changed
+97 -120

No files matched your search

+5 -17
View File
@@ -12,12 +12,6 @@ const cmdconfig = yaml.load(fs.readFileSync("./Configs/commands.yml", "utf8"));
const Command = require("../Structures/Command.js"); 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({ module.exports = new Command({
name: cmdconfig.TicketAdd, name: cmdconfig.TicketAdd,
description: cmdconfig.TicketAddDesc, description: cmdconfig.TicketAddDesc,
@@ -32,16 +26,10 @@ module.exports = new Command({
permission: "SEND_MESSAGES", permission: "SEND_MESSAGES",
async run(interaction) { async run(interaction) {
let SupportStaff = interaction.guild.roles.cache.find( const { getRole, getChannel, getCategory } = interaction.client;
(SupportTeam) => let SupportStaff = await getRole(supportbot.Staff, interaction.guild);
SupportTeam.name == supportbot.Staff || let Admin = await getRole(supportbot.Admin, interaction.guild);
SupportTeam.id == supportbot.Staff if (!SupportStaff || !Admin)
);
let Admins = interaction.guild.roles.cache.find(
(AdminUser) =>
AdminUser.name == supportbot.Admin || AdminUser.id == supportbot.Admin
);
if (!SupportStaff || !Admins)
return interaction.reply( return interaction.reply(
"Some roles seem to be missing!\nPlease check for errors when starting the bot." "Some roles seem to be missing!\nPlease check for errors when starting the bot."
); );
@@ -55,7 +43,7 @@ module.exports = new Command({
if ( if (
!interaction.member.roles.cache.has(SupportStaff.id) || !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] }); return interaction.reply({ embeds: [NoPerms] });
+12 -14
View File
@@ -25,6 +25,7 @@ module.exports = new Command({
permission: "SEND_MESSAGES", permission: "SEND_MESSAGES",
async run(interaction) { async run(interaction) {
const { getRole, getChannel, getCategory } = interaction.client;
await interaction.deferReply(); await interaction.deferReply();
let TicketData = await JSON.parse( let TicketData = await JSON.parse(
fs.readFileSync("./Data/TicketData.json", "utf8") fs.readFileSync("./Data/TicketData.json", "utf8")
@@ -37,16 +38,11 @@ module.exports = new Command({
return interaction.followUp({ embeds: [Exists] }); return interaction.followUp({ embeds: [Exists] });
} }
let tUser = interaction.client.users.cache.get(TicketData.user); let tUser = interaction.client.users.cache.get(TicketData.user);
let transcriptChannel = await interaction.guild.channels.cache.find( let transcriptChannel = await getChannel(
(channel) => supportbot.TranscriptLog,
channel.name == supportbot.TranscriptLog || interaction.guild
channel.id == supportbot.TranscriptLog
);
let logChannel = await interaction.guild.channels.cache.find(
(channel) =>
channel.name == supportbot.TicketLog ||
channel.id == supportbot.TicketLog
); );
let logChannel = await getChannel(supportbot.TicketLog, interaction.guild);
let reason = let reason =
(await interaction.options?.getString("reason")) || "No Reason Provided."; (await interaction.options?.getString("reason")) || "No Reason Provided.";
@@ -135,11 +131,13 @@ module.exports = new Command({
.catch(async (err) => { .catch(async (err) => {
console.error(err); console.error(err);
}); });
TicketData.subUsers.forEach(async (subUser) => { if (TicketData.subUsers) {
interaction.client.users.cache TicketData.subUsers.forEach(async (subUser) => {
.get(subUser) interaction.client.users.cache
.send({ embeds: [transcriptEmbed], files: [file] }); .get(subUser)
}); .send({ embeds: [transcriptEmbed], files: [file] });
});
}
await interaction.channel.delete().catch(async (error) => { await interaction.channel.delete().catch(async (error) => {
console.error(error); console.error(error);
if (error.code !== Discord.Constants.APIErrors.UNKNOWN_CHANNEL) { if (error.code !== Discord.Constants.APIErrors.UNKNOWN_CHANNEL) {
+5 -11
View File
@@ -31,17 +31,11 @@ module.exports = new Command({
permission: "SEND_MESSAGES", permission: "SEND_MESSAGES",
async run(interaction) { async run(interaction) {
let SupportStaff = interaction.guild.roles.cache.find( const { getRole, getChannel, getCategory } = interaction.client;
(SupportTeam) => let SupportStaff = await getRole(supportbot.Staff, interaction.guild);
SupportTeam.name === supportbot.Staff || let Admin = await getRole(supportbot.Admin, interaction.guild);
SupportTeam.id === supportbot.Staff
);
let Admins = interaction.guild.roles.cache.find(
(AdminUser) =>
AdminUser.name === supportbot.Admin || AdminUser.id === supportbot.Admin
);
if (!SupportStaff || !Admins) if (!SupportStaff || !Admin)
return interaction.reply( return interaction.reply(
"Some roles seem to be missing!\nPlease check for errors when starting the bot." "Some roles seem to be missing!\nPlease check for errors when starting the bot."
); );
@@ -54,7 +48,7 @@ module.exports = new Command({
if ( if (
interaction.member.roles.cache.has(SupportStaff.id) || 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 EmbedTitle = interaction.options.getString("title");
const EmbedSubject = interaction.options.getString("message"); const EmbedSubject = interaction.options.getString("message");
+5 -11
View File
@@ -25,16 +25,10 @@ module.exports = new Command({
permission: "SEND_MESSAGES", permission: "SEND_MESSAGES",
async run(interaction) { async run(interaction) {
let SupportStaff = interaction.guild.roles.cache.find( const { getRole, getChannel, getCategory } = interaction.client;
(SupportTeam) => let SupportStaff = await getRole(supportbot.Staff, interaction.guild);
SupportTeam.name === supportbot.Staff || let Admin = await getRole(supportbot.Admin, interaction.guild);
SupportTeam.id === supportbot.Staff if (!SupportStaff || !Admin)
);
let Admins = interaction.guild.roles.cache.find(
(AdminUser) =>
AdminUser.name === supportbot.Admin || AdminUser.id === supportbot.Admin
);
if (!SupportStaff || !Admins)
return interaction.reply( return interaction.reply(
"Some roles seem to be missing!\nPlease check for errors when starting the bot." "Some roles seem to be missing!\nPlease check for errors when starting the bot."
); );
@@ -74,7 +68,7 @@ module.exports = new Command({
if ( if (
interaction.member.roles.cache.has(SupportStaff.id) || interaction.member.roles.cache.has(SupportStaff.id) ||
interaction.member.roles.cache.has(Admins.id) interaction.member.roles.cache.has(Admin.id)
) { ) {
HelpEmbed1.addFields({ HelpEmbed1.addFields({
name: "🔐 Staff Commands\n", name: "🔐 Staff Commands\n",
+6 -12
View File
@@ -32,16 +32,10 @@ module.exports = new Command({
permission: "SEND_MESSAGES", permission: "SEND_MESSAGES",
async run(interaction) { async run(interaction) {
let SupportStaff = interaction.guild.roles.cache.find( const { getRole, getChannel, getCategory } = interaction.client;
(SupportTeam) => let Staff = await getRole(supportbot.Staff, interaction.guild);
SupportTeam.name == supportbot.Staff || let Admin = await getRole(supportbot.Admin, interaction.guild);
SupportTeam.id == supportbot.Staff if (!Staff || !Admin)
);
let Admins = interaction.guild.roles.cache.find(
(AdminUser) =>
AdminUser.name == supportbot.Admin || AdminUser.id == supportbot.Admin
);
if (!SupportStaff || !Admins)
return interaction.reply( return interaction.reply(
"Some roles seem to be missing!\nPlease check for errors when starting the bot." "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); .setColor(supportbot.WarningColour);
if ( if (
!interaction.member.roles.cache.has(SupportStaff.id) || !interaction.member.roles.cache.has(Staff.id) ||
!interaction.member.roles.cache.has(Admins.id) !interaction.member.roles.cache.has(Admin.id)
) )
return interaction.reply({ embeds: [NoPerms] }); return interaction.reply({ embeds: [NoPerms] });
let TicketData = await JSON.parse( let TicketData = await JSON.parse(
+4 -4
View File
@@ -25,10 +25,10 @@ module.exports = new Command({
permission: "SEND_MESSAGES", permission: "SEND_MESSAGES",
async run(interaction) { async run(interaction) {
const suggestChannel = interaction.guild.channels.cache.find( const { getRole, getChannel, getCategory } = interaction.client;
(channel) => const suggestChannel = await getChannel(
channel.name === supportbot.SuggestionChannel || supportbot.SuggestionChannel,
channel.id === supportbot.SuggestionChannel interaction.guild
); );
const NoChannel = new Discord.MessageEmbed() const NoChannel = new Discord.MessageEmbed()
+22 -42
View File
@@ -18,12 +18,6 @@ const cmdconfig = yaml.load(fs.readFileSync("./Configs/commands.yml", "utf8"));
const Command = require("../Structures/Command.js"); const Command = require("../Structures/Command.js");
const TicketNumberID = require("../Structures/TicketID.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({ module.exports = new Command({
name: cmdconfig.OpenTicket, name: cmdconfig.OpenTicket,
description: cmdconfig.OpenTicketDesc, description: cmdconfig.OpenTicketDesc,
@@ -37,19 +31,16 @@ module.exports = new Command({
permission: "SEND_MESSAGES", permission: "SEND_MESSAGES",
async run(interaction) { async run(interaction) {
const { getRole, getChannel, getCategory } = interaction.client;
let reactionUser = interaction.guild.members.cache.get(interaction.user.id); let reactionUser = interaction.guild.members.cache.get(interaction.user.id);
if ( if (
reactionUser.roles.cache.find( reactionUser.roles.cache.has(
(role) => getRole(supportbot.TicketBlackListRole, interaction.guild).id
role.name == supportbot.TicketBlackListRole ||
role.id == supportbot.TicketBlackListRole
) )
) { ) {
return interaction return interaction
.reply({ .reply(supportbot.TicketBlackListMessage)
content: `<@${interaction.user.id}> ${supportbot.TicketBlackListMessage}`,
})
.then((msg) => { .then((msg) => {
setTimeout(() => msg.delete(), 3500); setTimeout(() => msg.delete(), 3500);
}); });
@@ -78,41 +69,30 @@ module.exports = new Command({
) { ) {
return await interaction.reply({ embeds: [TicketExists] }); return await interaction.reply({ embeds: [TicketExists] });
} }
const Staff = interaction.guild.roles.cache.find( const Staff = await getRole(supportbot.Staff, interaction.guild);
(SupportTeam) => const Admin = await getRole(supportbot.Admin, interaction.guild);
SupportTeam.name === supportbot.Staff || const DeptRole1 = await getRole(
SupportTeam.id === supportbot.Staff supportbot.DepartmentRole_1,
interaction.guild
); );
const Admins = interaction.guild.roles.cache.find( const DeptRole2 = await getRole(
(AdminUser) => supportbot.DepartmentRole_2,
AdminUser.name === supportbot.Admin || AdminUser.id === supportbot.Admin interaction.guild
); );
const DeptRole1 = interaction.guild.roles.cache.find( const DeptRole3 = await getRole(
(DepartmentRole) => supportbot.DepartmentRole_3,
DepartmentRole.name === supportbot.DepartmentRole_1 || interaction.guild
DepartmentRole.id === supportbot.DepartmentRole_1
); );
const DeptRole2 = interaction.guild.roles.cache.find( if (!Staff || !Admin || !DeptRole1 || !DeptRole2 || !DeptRole3)
(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)
return interaction.reply({ return interaction.reply({
content: content:
"Some roles seem to be missing!\nPlease check for errors when starting the bot.", "Some roles seem to be missing!\nPlease check for errors when starting the bot.",
ephemeral: true, ephemeral: true,
}); });
const Author = interaction.user; const Author = interaction.user;
let TicketCategory = interaction.guild.channels.cache.find( let TicketCategory = await getCategory(
(category) => supportbot.TicketCategory,
category.name === supportbot.TicketCategory || interaction.guild
category.id === supportbot.TicketCategory
); );
const ticketChannel = await interaction.guild.channels.create( const ticketChannel = await interaction.guild.channels.create(
`${supportbot.TicketPrefix}${ticketNumberID}`, `${supportbot.TicketPrefix}${ticketNumberID}`,
@@ -121,7 +101,7 @@ module.exports = new Command({
parent: TicketCategory.id, parent: TicketCategory.id,
permissionOverwrites: [ permissionOverwrites: [
{ {
id: Admins.id, id: Admin.id,
allow: [Permissions.FLAGS.VIEW_CHANNEL], allow: [Permissions.FLAGS.VIEW_CHANNEL],
}, },
{ {
@@ -170,7 +150,7 @@ module.exports = new Command({
await interaction.reply({ embeds: [CreatedTicket], ephemeral: true }); await interaction.reply({ embeds: [CreatedTicket], ephemeral: true });
if (supportbot.AllowTicketMentions) { if (supportbot.AllowTicketMentions) {
await ticketChannel.send({ content: `${interaction.user}` }); await ticketChannel.send(`${interaction.user}`);
} }
const TicketMessage = new MessageEmbed() const TicketMessage = new MessageEmbed()
@@ -290,7 +270,7 @@ module.exports = new Command({
}); });
if (supportbot.AllowTicketMentions) { if (supportbot.AllowTicketMentions) {
await ticketChannel.send({ content: "@here" }); await ticketChannel.send("@here");
} }
await collector.message.edit({ await collector.message.edit({
embeds: [TicketMessage], embeds: [TicketMessage],
+19 -8
View File
@@ -15,6 +15,7 @@ const panelconfig = yaml.load(
const Event = require("../Structures/Event.js"); const Event = require("../Structures/Event.js");
module.exports = new Event("ready", async (client) => { module.exports = new Event("ready", async (client) => {
const { getRole, getChannel, getCategory } = client;
client.user.setActivity(supportbot.BotActivity, { client.user.setActivity(supportbot.BotActivity, {
type: supportbot.ActivityType, type: supportbot.ActivityType,
url: supportbot.StreamingURL, url: supportbot.StreamingURL,
@@ -92,21 +93,23 @@ module.exports = new Event("ready", async (client) => {
supportbot.TranscriptLog, supportbot.TranscriptLog,
panelconfig.Channel, panelconfig.Channel,
]; ];
const missingR = []; const categories = [supportbot.TicketCategory];
const missingC = [];
const missingC = [];
const missingR = [];
const missingCat = [];
for (let c of channels) { for (let c of channels) {
const find = await client.channels.cache.find( const find = await getChannel(c, client.guilds.cache.first());
(channel) => channel.name === c || channel.id === c
);
if (!find) missingC.push(c); if (!find) missingC.push(c);
} }
for (let r of roles) { for (let r of roles) {
const find = await client.guilds.cache const find = await getRole(r, client.guilds.cache.first());
.first()
.roles.cache.find((role) => role.name === r || role.id === r);
if (!find) missingR.push(r); 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) { if (missingR.length > 0) {
console.log( 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) { if (panelconfig.TicketPanel) {
let chan1 = client.channels.cache.find( let chan1 = client.channels.cache.find(
(channel) => channel.name === panelconfig.Channel (channel) => channel.name === panelconfig.Channel
+19 -1
View File
@@ -19,6 +19,25 @@ class Client extends Discord.Client {
this.commands = new Discord.Collection(); 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) { start(token) {
const commandFiles = fs const commandFiles = fs
@@ -73,7 +92,6 @@ class Client extends Discord.Client {
})); }));
this.removeAllListeners(); this.removeAllListeners();
this.on("ready", async () => { this.on("ready", async () => {
const allCommands = slashCommands.concat(slashCommands1); const allCommands = slashCommands.concat(slashCommands1);
await this.application?.commands.set(allCommands, supportbot.Guild); await this.application?.commands.set(allCommands, supportbot.Guild);