From c93c16c1abc4b11628af0fc6b54a31d6291bbc06 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Wed, 7 Sep 2022 15:23:06 -0500 Subject: [PATCH 01/32] WIP --- commands/owner/csCreateChannels.ts | 61 ++++++++++++++++++++++++++++++ utils/channelUtils.ts | 33 ++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 commands/owner/csCreateChannels.ts create mode 100644 utils/channelUtils.ts diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts new file mode 100644 index 0000000..01745e2 --- /dev/null +++ b/commands/owner/csCreateChannels.ts @@ -0,0 +1,61 @@ +import { MessageEmbed } from "discord.js"; + +import chalk from "chalk"; +import { ICommand } from "wokcommands"; +import { classModel } from "../../models/classModel"; +import { checkForChannel, createTextChannel } from "../../utils/channelUtils"; + +export default { + name: "csCreateChannels", + category: "owner", + description: "Update CS class channels", + slash: true, + testOnly: false, + guildOnly: true, + requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"], + ownerOnly: true, + + callback: async ({ client, interaction: msgInt }) => { + if (msgInt.guild === null) { + console.log(chalk.red("No guild")); + return; + } + + const classes = await classModel.find({}).sort({ CODE: 1 }); + + for (let index = 0; index < classes.length; index++) { + const channel = await checkForChannel(msgInt.guild, classes[index].CODE); + + if (channel === undefined) { + const new_channel = await createTextChannel( + msgInt.guild, + classes[index].CODE, + classes[index].INFO + ); + } else { + } + } + + const infoEmbed = new MessageEmbed() + .setTitle("Created Classes") + .setColor("#0099ff") + .setDescription(`Populated ${msgInt.guild.name} with COMPSCI classes.`) + .setFooter({ + text: `Delivered in: ${client.ws.ping}ms | CSSC-Bot | ${process.env.VERSION}`, + iconURL: "https://playantares.com/resources/CSSC-bot/icon.jpg", + }); + + msgInt.reply({ embeds: [infoEmbed] }); + + // Log the command usage + console.log( + chalk.blue( + `${chalk.green(`[COMMAND]`)} ${chalk.yellow( + msgInt.user.tag + )} used the ${chalk.green(`/csclasspoll`)} command in ${chalk.yellow( + msgInt.guild?.name + )}` + ) + ); + }, +} as ICommand; diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts new file mode 100644 index 0000000..46f9d9c --- /dev/null +++ b/utils/channelUtils.ts @@ -0,0 +1,33 @@ +import { Guild } from "discord.js"; + +export async function checkForChannel(guild: Guild, channel_name: string) { + return guild.channels.cache.find((channel) => channel.name === channel_name); +} + +export async function createTextChannel( + guild: Guild, + channel_name: string, + channel_topic?: string, + channel_category?: string +) { + let category = undefined; + + if (channel_category) { + category = guild.channels.cache.find( + (channel) => + channel.name == channel_name && channel.type === "GUILD_CATEGORY" + ); + + if (category === undefined) { + category = guild.channels.create(channel_category, { + type: "GUILD_CATEGORY", + }); + } + } + + return guild.channels.create(channel_name, { + type: "GUILD_TEXT", + topic: channel_topic ? channel_topic : "", + parent: channel_category, + }); +} From 8fc0485dbb7da40a345f4f6790f064ae77216e95 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Wed, 7 Sep 2022 23:03:50 -0500 Subject: [PATCH 02/32] WIP --- commands/owner/csCreateChannels.ts | 52 +++++++++++++++++++++--------- index.ts | 2 +- utils/channelUtils.ts | 44 +++++++++++++++++++++++-- 3 files changed, 79 insertions(+), 19 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 01745e2..8b280f8 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -3,7 +3,11 @@ import { MessageEmbed } from "discord.js"; import chalk from "chalk"; import { ICommand } from "wokcommands"; import { classModel } from "../../models/classModel"; -import { checkForChannel, createTextChannel } from "../../utils/channelUtils"; +import { + checkForChannel, + createTextChannel, + moveChannel, +} from "../../utils/channelUtils"; export default { name: "csCreateChannels", @@ -21,40 +25,58 @@ export default { return; } + const color = "#0099ff"; + const thumbnail = + "https://playantares.com/resources/CSSC-bot/cssc-server-icon.png"; + const title = "Create Classes"; + const description = `Populated ${msgInt.guild.name} with COMPSCI classes.`; + const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; + const footerIcon = "https://playantares.com/resources/CSSC-bot/icon.jpg"; + + // Embed construction + const embed = new MessageEmbed() + .setColor(color) + .setTitle(title) + .setThumbnail(thumbnail) + .setDescription(description) + .setFooter({ text: footer, iconURL: footerIcon }); + const classes = await classModel.find({}).sort({ CODE: 1 }); for (let index = 0; index < classes.length; index++) { const channel = await checkForChannel(msgInt.guild, classes[index].CODE); - if (channel === undefined) { + if (channel === undefined || channel.type !== "GUILD_TEXT") { const new_channel = await createTextChannel( msgInt.guild, classes[index].CODE, classes[index].INFO ); + + console.log( + chalk.yellow( + `Created channel: ${new_channel.name} in guild ${msgInt.guild.name}` + ) + ); + + moveChannel(msgInt.guild, new_channel, "COMP SCI CLASSES"); + //TODO: Write channel id to db } else { + moveChannel(msgInt.guild, channel, "COMP SCI CLASSES"); + //Confirm channel ID is in db } } - const infoEmbed = new MessageEmbed() - .setTitle("Created Classes") - .setColor("#0099ff") - .setDescription(`Populated ${msgInt.guild.name} with COMPSCI classes.`) - .setFooter({ - text: `Delivered in: ${client.ws.ping}ms | CSSC-Bot | ${process.env.VERSION}`, - iconURL: "https://playantares.com/resources/CSSC-bot/icon.jpg", - }); - - msgInt.reply({ embeds: [infoEmbed] }); + await msgInt.reply({ embeds: [embed], ephemeral: true }); // Log the command usage console.log( chalk.blue( `${chalk.green(`[COMMAND]`)} ${chalk.yellow( msgInt.user.tag - )} used the ${chalk.green(`/csclasspoll`)} command in ${chalk.yellow( - msgInt.guild?.name - )}` + )} used the ${chalk.green( + `/csCreateChannels` + )} command in ${chalk.yellow(msgInt.guild?.name)}` ) ); }, diff --git a/index.ts b/index.ts index ee5ec5e..5467eea 100644 --- a/index.ts +++ b/index.ts @@ -6,7 +6,7 @@ import chalk from "chalk"; import dotenv from "dotenv"; // import custom modules -import { checkForRoles, checkIfCollectionsExist } from "./rolesOps"; +import { checkForRoles, checkIfCollectionsExist } from "./utils/roleUtils"; import { classModel } from "./models/classModel"; import { staffModel } from "./models/staffModel"; import { yearModel } from "./models/yearModel"; diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 46f9d9c..3d638a2 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -1,7 +1,11 @@ -import { Guild } from "discord.js"; +import { Guild, GuildChannel } from "discord.js"; export async function checkForChannel(guild: Guild, channel_name: string) { - return guild.channels.cache.find((channel) => channel.name === channel_name); + // console.log(guild.channels.cache); + return guild.channels.cache.find((channel) => { + console.log(channel.name); + return channel.name == channel_name; + }); } export async function createTextChannel( @@ -19,7 +23,7 @@ export async function createTextChannel( ); if (category === undefined) { - category = guild.channels.create(channel_category, { + category = await guild.channels.create(channel_category, { type: "GUILD_CATEGORY", }); } @@ -31,3 +35,37 @@ export async function createTextChannel( parent: channel_category, }); } + +export async function moveChannel( + guild: Guild, + channel: GuildChannel, + category_name: string +) { + if ( + channel.parent === null || + channel.parent === undefined || + channel.parent?.name != category_name + ) { + // Move the class to parent "COMP SCI" + console.log( + `Parent Name: ${channel.parent?.name}.....Category name: ${category_name}` + ); + console.log("moving channel"); + let category = await findCategory(guild, category_name); + channel.setParent(category); + } +} + +async function findCategory(guild: Guild, category_name: string) { + let category = guild.channels.cache.find( + (category) => category.name == category_name + ); + + if (category === undefined || category.type !== "GUILD_CATEGORY") { + category = await guild.channels.create(category_name, { + type: "GUILD_CATEGORY", + }); + } + + return category; +} From 952015aa4a81cc8091f358ddadac44bac5478210 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Wed, 7 Sep 2022 23:36:57 -0500 Subject: [PATCH 03/32] WIP --- commands/owner/csCreateChannels.ts | 30 +++++++++++++++++++++++------- utils/channelUtils.ts | 26 ++++++-------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 8b280f8..23fbe0e 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -6,8 +6,10 @@ import { classModel } from "../../models/classModel"; import { checkForChannel, createTextChannel, + findCategory, moveChannel, } from "../../utils/channelUtils"; +import { sleep } from "../../utils/util"; export default { name: "csCreateChannels", @@ -29,7 +31,7 @@ export default { const thumbnail = "https://playantares.com/resources/CSSC-bot/cssc-server-icon.png"; const title = "Create Classes"; - const description = `Populated ${msgInt.guild.name} with COMPSCI classes.`; + const description = `Finished populating ${msgInt.guild.name} with COMPSCI classes.`; const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; const footerIcon = "https://playantares.com/resources/CSSC-bot/icon.jpg"; @@ -41,25 +43,36 @@ export default { .setDescription(description) .setFooter({ text: footer, iconURL: footerIcon }); + const finish_description = `Started populating ${msgInt.guild.name} with COMPSCI classes.`; + + const finish_embed = new MessageEmbed() + .setColor(color) + .setTitle(title) + .setThumbnail(thumbnail) + .setDescription(description) + .setFooter({ text: footer, iconURL: footerIcon }); + + await msgInt.reply({ embeds: [finish_embed], ephemeral: true }); + const classes = await classModel.find({}).sort({ CODE: 1 }); + const cs_category_name = "COMP SCI CLASSES"; for (let index = 0; index < classes.length; index++) { const channel = await checkForChannel(msgInt.guild, classes[index].CODE); + console.log(channel); if (channel === undefined || channel.type !== "GUILD_TEXT") { const new_channel = await createTextChannel( msgInt.guild, classes[index].CODE, - classes[index].INFO + classes[index].INFO, + cs_category_name ); console.log( - chalk.yellow( - `Created channel: ${new_channel.name} in guild ${msgInt.guild.name}` - ) + chalk.yellow(`Created channel: ${new_channel.name} index: ${index}`) ); - moveChannel(msgInt.guild, new_channel, "COMP SCI CLASSES"); //TODO: Write channel id to db } else { moveChannel(msgInt.guild, channel, "COMP SCI CLASSES"); @@ -67,7 +80,10 @@ export default { } } - await msgInt.reply({ embeds: [embed], ephemeral: true }); + console.log("reply"); + + // Can't reply for some reason. Timeout? + // await msgInt.reply({ embeds: [embed], ephemeral: true }); // Log the command usage console.log( diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 3d638a2..98da248 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -1,9 +1,8 @@ import { Guild, GuildChannel } from "discord.js"; export async function checkForChannel(guild: Guild, channel_name: string) { - // console.log(guild.channels.cache); + //FIXME: returns undefined return guild.channels.cache.find((channel) => { - console.log(channel.name); return channel.name == channel_name; }); } @@ -11,28 +10,15 @@ export async function checkForChannel(guild: Guild, channel_name: string) { export async function createTextChannel( guild: Guild, channel_name: string, - channel_topic?: string, - channel_category?: string + channel_topic: string, + channel_category_name: string ) { - let category = undefined; - - if (channel_category) { - category = guild.channels.cache.find( - (channel) => - channel.name == channel_name && channel.type === "GUILD_CATEGORY" - ); - - if (category === undefined) { - category = await guild.channels.create(channel_category, { - type: "GUILD_CATEGORY", - }); - } - } + let category = await findCategory(guild, channel_category_name); return guild.channels.create(channel_name, { type: "GUILD_TEXT", topic: channel_topic ? channel_topic : "", - parent: channel_category, + parent: category, }); } @@ -56,7 +42,7 @@ export async function moveChannel( } } -async function findCategory(guild: Guild, category_name: string) { +export async function findCategory(guild: Guild, category_name: string) { let category = guild.channels.cache.find( (category) => category.name == category_name ); From 1ddc5991d1131de3560e3baf85521077ecc4bd99 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 12:52:03 -0500 Subject: [PATCH 04/32] Create channels rework --- commands/owner/csCreateChannels.ts | 100 +++++++++++++++++------------ utils/channelUtils.ts | 31 ++++++--- 2 files changed, 79 insertions(+), 52 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 23fbe0e..329d358 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -1,16 +1,35 @@ -import { MessageEmbed } from "discord.js"; +import { Client, MessageEmbed } from "discord.js"; import chalk from "chalk"; import { ICommand } from "wokcommands"; import { classModel } from "../../models/classModel"; import { checkForChannel, + cleanChannelString, createTextChannel, - findCategory, moveChannel, } from "../../utils/channelUtils"; -import { sleep } from "../../utils/util"; +function create_default_embed( + client: Client, + title: string, + description: string +) { + const color = "#0099ff"; + const thumbnail = + "https://playantares.com/resources/CSSC-bot/cssc-server-icon.png"; + const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; + const footerIcon = "https://playantares.com/resources/CSSC-bot/icon.jpg"; + + // Embed construction + const embed = new MessageEmbed() + .setColor(color) + .setTitle(title) + .setThumbnail(thumbnail) + .setDescription(description) + .setFooter({ text: footer, iconURL: footerIcon }); + return embed; +} export default { name: "csCreateChannels", category: "owner", @@ -27,63 +46,60 @@ export default { return; } - const color = "#0099ff"; - const thumbnail = - "https://playantares.com/resources/CSSC-bot/cssc-server-icon.png"; - const title = "Create Classes"; - const description = `Finished populating ${msgInt.guild.name} with COMPSCI classes.`; - const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; - const footerIcon = "https://playantares.com/resources/CSSC-bot/icon.jpg"; - - // Embed construction - const embed = new MessageEmbed() - .setColor(color) - .setTitle(title) - .setThumbnail(thumbnail) - .setDescription(description) - .setFooter({ text: footer, iconURL: footerIcon }); - - const finish_description = `Started populating ${msgInt.guild.name} with COMPSCI classes.`; - - const finish_embed = new MessageEmbed() - .setColor(color) - .setTitle(title) - .setThumbnail(thumbnail) - .setDescription(description) - .setFooter({ text: footer, iconURL: footerIcon }); + await msgInt.deferReply({ ephemeral: true }); - await msgInt.reply({ embeds: [finish_embed], ephemeral: true }); + const courses = await classModel.find({}).sort({ CODE: 1 }); - const classes = await classModel.find({}).sort({ CODE: 1 }); + //create a copy of the courses with cleaned names + const cleaned_courses = Array.from(courses).map((course) => { + course.CODE = cleanChannelString(course.CODE); + return course; + }); const cs_category_name = "COMP SCI CLASSES"; - for (let index = 0; index < classes.length; index++) { - const channel = await checkForChannel(msgInt.guild, classes[index].CODE); - console.log(channel); + let new_channel_count = 0; + let move_channel_count = 0; + for (let index = 0; index < cleaned_courses.length; index++) { + const channel = await checkForChannel( + msgInt.guild, + cleaned_courses[index].CODE + ); if (channel === undefined || channel.type !== "GUILD_TEXT") { + console.log( + chalk.yellow(`Created channel: ${cleaned_courses[index].CODE}`) + ); const new_channel = await createTextChannel( msgInt.guild, - classes[index].CODE, - classes[index].INFO, + cleaned_courses[index].CODE, + cleaned_courses[index].INFO, cs_category_name ); - console.log( - chalk.yellow(`Created channel: ${new_channel.name} index: ${index}`) - ); + ++new_channel_count; + console.log(chalk.yellow(`Created channel: ${new_channel.name}`)); //TODO: Write channel id to db } else { - moveChannel(msgInt.guild, channel, "COMP SCI CLASSES"); - //Confirm channel ID is in db + move_channel_count += await moveChannel( + msgInt.guild, + channel, + "COMP SCI CLASSES" + ); + //TODO: Confirm channel ID is in db } } - console.log("reply"); + const title = "Create Classes"; + let description = `Created ${new_channel_count} new channels\nMoved ${move_channel_count} channels`; + if (move_channel_count > 0) { + description += ` to ${cs_category_name}`; + } + + const embed = create_default_embed(client, title, description); + await msgInt.editReply({ embeds: [embed] }); - // Can't reply for some reason. Timeout? - // await msgInt.reply({ embeds: [embed], ephemeral: true }); + console.log(chalk.yellow(description)); // Log the command usage console.log( diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 98da248..8acb4f5 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -1,7 +1,7 @@ import { Guild, GuildChannel } from "discord.js"; +import chalk from "chalk"; export async function checkForChannel(guild: Guild, channel_name: string) { - //FIXME: returns undefined return guild.channels.cache.find((channel) => { return channel.name == channel_name; }); @@ -26,26 +26,29 @@ export async function moveChannel( guild: Guild, channel: GuildChannel, category_name: string -) { +): Promise { if ( channel.parent === null || channel.parent === undefined || channel.parent?.name != category_name ) { - // Move the class to parent "COMP SCI" - console.log( - `Parent Name: ${channel.parent?.name}.....Category name: ${category_name}` - ); - console.log("moving channel"); let category = await findCategory(guild, category_name); channel.setParent(category); + + console.log( + chalk.yellow( + `Moved channel${channel.name} from: ${channel.parent?.name} to: ${category_name}` + ) + ); + return 1; } + return 0; } export async function findCategory(guild: Guild, category_name: string) { - let category = guild.channels.cache.find( - (category) => category.name == category_name - ); + let category = guild.channels.cache.find((category) => { + return category.name == category_name; + }); if (category === undefined || category.type !== "GUILD_CATEGORY") { category = await guild.channels.create(category_name, { @@ -55,3 +58,11 @@ export async function findCategory(guild: Guild, category_name: string) { return category; } + +export function cleanChannelString(s: string): string { + return s + .toLowerCase() + .replace(/[`~!@#$%^&*)|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "") + .replace("(", "-") + .replace("compsci ", "cs"); +} From b102371dd5005725ad14fccbc1af34b6f89f5a5e Mon Sep 17 00:00:00 2001 From: schiltz3 Date: Thu, 8 Sep 2022 17:59:33 +0000 Subject: [PATCH 05/32] Apply auto formatting changes --- commands/owner/csCreateChannels.ts | 230 ++++++++++++++--------------- utils/channelUtils.ts | 136 ++++++++--------- 2 files changed, 183 insertions(+), 183 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 329d358..a8d020c 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -1,115 +1,115 @@ -import { Client, MessageEmbed } from "discord.js"; - -import chalk from "chalk"; -import { ICommand } from "wokcommands"; -import { classModel } from "../../models/classModel"; -import { - checkForChannel, - cleanChannelString, - createTextChannel, - moveChannel, -} from "../../utils/channelUtils"; - -function create_default_embed( - client: Client, - title: string, - description: string -) { - const color = "#0099ff"; - const thumbnail = - "https://playantares.com/resources/CSSC-bot/cssc-server-icon.png"; - const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; - const footerIcon = "https://playantares.com/resources/CSSC-bot/icon.jpg"; - - // Embed construction - const embed = new MessageEmbed() - .setColor(color) - .setTitle(title) - .setThumbnail(thumbnail) - .setDescription(description) - .setFooter({ text: footer, iconURL: footerIcon }); - return embed; -} -export default { - name: "csCreateChannels", - category: "owner", - description: "Update CS class channels", - slash: true, - testOnly: false, - guildOnly: true, - requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"], - ownerOnly: true, - - callback: async ({ client, interaction: msgInt }) => { - if (msgInt.guild === null) { - console.log(chalk.red("No guild")); - return; - } - - await msgInt.deferReply({ ephemeral: true }); - - const courses = await classModel.find({}).sort({ CODE: 1 }); - - //create a copy of the courses with cleaned names - const cleaned_courses = Array.from(courses).map((course) => { - course.CODE = cleanChannelString(course.CODE); - return course; - }); - const cs_category_name = "COMP SCI CLASSES"; - - let new_channel_count = 0; - let move_channel_count = 0; - for (let index = 0; index < cleaned_courses.length; index++) { - const channel = await checkForChannel( - msgInt.guild, - cleaned_courses[index].CODE - ); - - if (channel === undefined || channel.type !== "GUILD_TEXT") { - console.log( - chalk.yellow(`Created channel: ${cleaned_courses[index].CODE}`) - ); - const new_channel = await createTextChannel( - msgInt.guild, - cleaned_courses[index].CODE, - cleaned_courses[index].INFO, - cs_category_name - ); - - ++new_channel_count; - console.log(chalk.yellow(`Created channel: ${new_channel.name}`)); - - //TODO: Write channel id to db - } else { - move_channel_count += await moveChannel( - msgInt.guild, - channel, - "COMP SCI CLASSES" - ); - //TODO: Confirm channel ID is in db - } - } - - const title = "Create Classes"; - let description = `Created ${new_channel_count} new channels\nMoved ${move_channel_count} channels`; - if (move_channel_count > 0) { - description += ` to ${cs_category_name}`; - } - - const embed = create_default_embed(client, title, description); - await msgInt.editReply({ embeds: [embed] }); - - console.log(chalk.yellow(description)); - - // Log the command usage - console.log( - chalk.blue( - `${chalk.green(`[COMMAND]`)} ${chalk.yellow( - msgInt.user.tag - )} used the ${chalk.green( - `/csCreateChannels` - )} command in ${chalk.yellow(msgInt.guild?.name)}` - ) - ); - }, -} as ICommand; +import { Client, MessageEmbed } from "discord.js"; + +import chalk from "chalk"; +import { ICommand } from "wokcommands"; +import { classModel } from "../../models/classModel"; +import { + checkForChannel, + cleanChannelString, + createTextChannel, + moveChannel, +} from "../../utils/channelUtils"; + +function create_default_embed( + client: Client, + title: string, + description: string +) { + const color = "#0099ff"; + const thumbnail = + "https://playantares.com/resources/CSSC-bot/cssc-server-icon.png"; + const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; + const footerIcon = "https://playantares.com/resources/CSSC-bot/icon.jpg"; + + // Embed construction + const embed = new MessageEmbed() + .setColor(color) + .setTitle(title) + .setThumbnail(thumbnail) + .setDescription(description) + .setFooter({ text: footer, iconURL: footerIcon }); + return embed; +} +export default { + name: "csCreateChannels", + category: "owner", + description: "Update CS class channels", + slash: true, + testOnly: false, + guildOnly: true, + requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"], + ownerOnly: true, + + callback: async ({ client, interaction: msgInt }) => { + if (msgInt.guild === null) { + console.log(chalk.red("No guild")); + return; + } + + await msgInt.deferReply({ ephemeral: true }); + + const courses = await classModel.find({}).sort({ CODE: 1 }); + + //create a copy of the courses with cleaned names + const cleaned_courses = Array.from(courses).map((course) => { + course.CODE = cleanChannelString(course.CODE); + return course; + }); + const cs_category_name = "COMP SCI CLASSES"; + + let new_channel_count = 0; + let move_channel_count = 0; + for (let index = 0; index < cleaned_courses.length; index++) { + const channel = await checkForChannel( + msgInt.guild, + cleaned_courses[index].CODE + ); + + if (channel === undefined || channel.type !== "GUILD_TEXT") { + console.log( + chalk.yellow(`Created channel: ${cleaned_courses[index].CODE}`) + ); + const new_channel = await createTextChannel( + msgInt.guild, + cleaned_courses[index].CODE, + cleaned_courses[index].INFO, + cs_category_name + ); + + ++new_channel_count; + console.log(chalk.yellow(`Created channel: ${new_channel.name}`)); + + //TODO: Write channel id to db + } else { + move_channel_count += await moveChannel( + msgInt.guild, + channel, + "COMP SCI CLASSES" + ); + //TODO: Confirm channel ID is in db + } + } + + const title = "Create Classes"; + let description = `Created ${new_channel_count} new channels\nMoved ${move_channel_count} channels`; + if (move_channel_count > 0) { + description += ` to ${cs_category_name}`; + } + + const embed = create_default_embed(client, title, description); + await msgInt.editReply({ embeds: [embed] }); + + console.log(chalk.yellow(description)); + + // Log the command usage + console.log( + chalk.blue( + `${chalk.green(`[COMMAND]`)} ${chalk.yellow( + msgInt.user.tag + )} used the ${chalk.green( + `/csCreateChannels` + )} command in ${chalk.yellow(msgInt.guild?.name)}` + ) + ); + }, +} as ICommand; diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 8acb4f5..252c1fb 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -1,68 +1,68 @@ -import { Guild, GuildChannel } from "discord.js"; -import chalk from "chalk"; - -export async function checkForChannel(guild: Guild, channel_name: string) { - return guild.channels.cache.find((channel) => { - return channel.name == channel_name; - }); -} - -export async function createTextChannel( - guild: Guild, - channel_name: string, - channel_topic: string, - channel_category_name: string -) { - let category = await findCategory(guild, channel_category_name); - - return guild.channels.create(channel_name, { - type: "GUILD_TEXT", - topic: channel_topic ? channel_topic : "", - parent: category, - }); -} - -export async function moveChannel( - guild: Guild, - channel: GuildChannel, - category_name: string -): Promise { - if ( - channel.parent === null || - channel.parent === undefined || - channel.parent?.name != category_name - ) { - let category = await findCategory(guild, category_name); - channel.setParent(category); - - console.log( - chalk.yellow( - `Moved channel${channel.name} from: ${channel.parent?.name} to: ${category_name}` - ) - ); - return 1; - } - return 0; -} - -export async function findCategory(guild: Guild, category_name: string) { - let category = guild.channels.cache.find((category) => { - return category.name == category_name; - }); - - if (category === undefined || category.type !== "GUILD_CATEGORY") { - category = await guild.channels.create(category_name, { - type: "GUILD_CATEGORY", - }); - } - - return category; -} - -export function cleanChannelString(s: string): string { - return s - .toLowerCase() - .replace(/[`~!@#$%^&*)|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "") - .replace("(", "-") - .replace("compsci ", "cs"); -} +import { Guild, GuildChannel } from "discord.js"; +import chalk from "chalk"; + +export async function checkForChannel(guild: Guild, channel_name: string) { + return guild.channels.cache.find((channel) => { + return channel.name == channel_name; + }); +} + +export async function createTextChannel( + guild: Guild, + channel_name: string, + channel_topic: string, + channel_category_name: string +) { + let category = await findCategory(guild, channel_category_name); + + return guild.channels.create(channel_name, { + type: "GUILD_TEXT", + topic: channel_topic ? channel_topic : "", + parent: category, + }); +} + +export async function moveChannel( + guild: Guild, + channel: GuildChannel, + category_name: string +): Promise { + if ( + channel.parent === null || + channel.parent === undefined || + channel.parent?.name != category_name + ) { + let category = await findCategory(guild, category_name); + channel.setParent(category); + + console.log( + chalk.yellow( + `Moved channel${channel.name} from: ${channel.parent?.name} to: ${category_name}` + ) + ); + return 1; + } + return 0; +} + +export async function findCategory(guild: Guild, category_name: string) { + let category = guild.channels.cache.find((category) => { + return category.name == category_name; + }); + + if (category === undefined || category.type !== "GUILD_CATEGORY") { + category = await guild.channels.create(category_name, { + type: "GUILD_CATEGORY", + }); + } + + return category; +} + +export function cleanChannelString(s: string): string { + return s + .toLowerCase() + .replace(/[`~!@#$%^&*)|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "") + .replace("(", "-") + .replace("compsci ", "cs"); +} From ab67099f9cfc50f0e9325ab9f640b6e88318f5f4 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 17:40:06 -0500 Subject: [PATCH 06/32] Archive old classes --- commands/owner/csCreateChannels.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index a8d020c..1590a55 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -55,17 +55,39 @@ export default { course.CODE = cleanChannelString(course.CODE); return course; }); - const cs_category_name = "COMP SCI CLASSES"; + const cs_category_name = "COMP SCI CLASSES"; + const cs_past_category_name = "PAST CLASSES"; let new_channel_count = 0; let move_channel_count = 0; + + //Move classes no longer in the db to + const cs_category = await checkForChannel(msgInt.guild, cs_category_name); + if (cs_category != undefined && cs_category.type == "GUILD_CATEGORY") { + const children = Array.from(cs_category.children.values()); + for (let index = 0; index < children.length; index++) { + const match = cleaned_courses.find((course) => { + return course.CODE == children[index].name; + }); + if (match == undefined) { + await moveChannel( + msgInt.guild, + children[index], + cs_past_category_name + ); + } + } + } + for (let index = 0; index < cleaned_courses.length; index++) { + // Iterate through courses in db const channel = await checkForChannel( msgInt.guild, cleaned_courses[index].CODE ); if (channel === undefined || channel.type !== "GUILD_TEXT") { + // Create new channels console.log( chalk.yellow(`Created channel: ${cleaned_courses[index].CODE}`) ); @@ -81,6 +103,7 @@ export default { //TODO: Write channel id to db } else { + // Move old channels move_channel_count += await moveChannel( msgInt.guild, channel, From f9b8c62e79d8a31eac56e023cb24004661aae044 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 18:56:23 -0500 Subject: [PATCH 07/32] Deepsource fixes --- commands/owner/csCreateChannels.ts | 4 ++-- utils/channelUtils.ts | 33 +++++++++++++++--------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 1590a55..f597222 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -62,7 +62,7 @@ export default { let move_channel_count = 0; //Move classes no longer in the db to - const cs_category = await checkForChannel(msgInt.guild, cs_category_name); + const cs_category = checkForChannel(msgInt.guild, cs_category_name); if (cs_category != undefined && cs_category.type == "GUILD_CATEGORY") { const children = Array.from(cs_category.children.values()); for (let index = 0; index < children.length; index++) { @@ -81,7 +81,7 @@ export default { for (let index = 0; index < cleaned_courses.length; index++) { // Iterate through courses in db - const channel = await checkForChannel( + const channel = checkForChannel( msgInt.guild, cleaned_courses[index].CODE ); diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 252c1fb..2441ab9 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -1,19 +1,32 @@ import { Guild, GuildChannel } from "discord.js"; import chalk from "chalk"; -export async function checkForChannel(guild: Guild, channel_name: string) { +export function checkForChannel(guild: Guild, channel_name: string) { return guild.channels.cache.find((channel) => { return channel.name == channel_name; }); } +export async function findCategory(guild: Guild, category_name: string) { + let category = guild.channels.cache.find((category) => { + return category.name == category_name; + }); + + if (category === undefined || category.type !== "GUILD_CATEGORY") { + category = await guild.channels.create(category_name, { + type: "GUILD_CATEGORY", + }); + } + + return category; +} export async function createTextChannel( guild: Guild, channel_name: string, channel_topic: string, channel_category_name: string ) { - let category = await findCategory(guild, channel_category_name); + const category = await findCategory(guild, channel_category_name); return guild.channels.create(channel_name, { type: "GUILD_TEXT", @@ -32,7 +45,7 @@ export async function moveChannel( channel.parent === undefined || channel.parent?.name != category_name ) { - let category = await findCategory(guild, category_name); + const category = await findCategory(guild, category_name); channel.setParent(category); console.log( @@ -45,20 +58,6 @@ export async function moveChannel( return 0; } -export async function findCategory(guild: Guild, category_name: string) { - let category = guild.channels.cache.find((category) => { - return category.name == category_name; - }); - - if (category === undefined || category.type !== "GUILD_CATEGORY") { - category = await guild.channels.create(category_name, { - type: "GUILD_CATEGORY", - }); - } - - return category; -} - export function cleanChannelString(s: string): string { return s .toLowerCase() From 8e4669cf4960861d2675b800eac77178b12cc6c0 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 19:30:39 -0500 Subject: [PATCH 08/32] Add ability to pass in category or category name --- utils/channelUtils.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 2441ab9..13f6bb8 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -1,4 +1,4 @@ -import { Guild, GuildChannel } from "discord.js"; +import { CategoryChannel, Guild, GuildChannel } from "discord.js"; import chalk from "chalk"; export function checkForChannel(guild: Guild, channel_name: string) { @@ -24,14 +24,24 @@ export async function createTextChannel( guild: Guild, channel_name: string, channel_topic: string, - channel_category_name: string + channel_category?: CategoryChannel, + channel_category_name?: string ) { - const category = await findCategory(guild, channel_category_name); - + if ( + channel_category_name == undefined && + channel_category_name == undefined + ) { + throw Error( + "Must specify either channel_category or channel_category_name" + ); + } return guild.channels.create(channel_name, { type: "GUILD_TEXT", topic: channel_topic ? channel_topic : "", - parent: category, + parent: + channel_category != undefined + ? channel_category + : await findCategory(guild, channel_category_name), }); } From f7e166b7a2f8811b966b4cf392d2a3ece57d6891 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 19:37:44 -0500 Subject: [PATCH 09/32] split more than 50 channels into separate categories --- commands/owner/csCreateChannels.ts | 42 +++++++++++++++++++++++------- utils/channelUtils.ts | 8 ++++++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index f597222..38f7bad 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -7,7 +7,9 @@ import { checkForChannel, cleanChannelString, createTextChannel, + findCategory, moveChannel, + concatCategoryName, } from "../../utils/channelUtils"; function create_default_embed( @@ -56,13 +58,19 @@ export default { return course; }); - const cs_category_name = "COMP SCI CLASSES"; + let category_name = "COMP SCI CLASSES"; + let category_number = 0; + const max_category_size = 50; + const cs_past_category_name = "PAST CLASSES"; let new_channel_count = 0; let move_channel_count = 0; //Move classes no longer in the db to - const cs_category = checkForChannel(msgInt.guild, cs_category_name); + const cs_category = checkForChannel( + msgInt.guild, + concatCategoryName(category_name, category_number) + ); if (cs_category != undefined && cs_category.type == "GUILD_CATEGORY") { const children = Array.from(cs_category.children.values()); for (let index = 0; index < children.length; index++) { @@ -86,29 +94,42 @@ export default { cleaned_courses[index].CODE ); + // Create new channels if (channel === undefined || channel.type !== "GUILD_TEXT") { - // Create new channels - console.log( - chalk.yellow(`Created channel: ${cleaned_courses[index].CODE}`) + let category = await findCategory( + msgInt.guild, + concatCategoryName(category_name, category_number) ); + + // Increment category if category is full + if (category.children.size >= max_category_size) { + ++category_number; + category = await findCategory( + msgInt.guild, + concatCategoryName(category_name, category_number) + ); + } + const new_channel = await createTextChannel( msgInt.guild, cleaned_courses[index].CODE, cleaned_courses[index].INFO, - cs_category_name + category ); ++new_channel_count; console.log(chalk.yellow(`Created channel: ${new_channel.name}`)); //TODO: Write channel id to db - } else { - // Move old channels + } + // Move old channels + else { move_channel_count += await moveChannel( msgInt.guild, channel, "COMP SCI CLASSES" ); + //TODO: Confirm channel ID is in db } } @@ -116,7 +137,10 @@ export default { const title = "Create Classes"; let description = `Created ${new_channel_count} new channels\nMoved ${move_channel_count} channels`; if (move_channel_count > 0) { - description += ` to ${cs_category_name}`; + description += ` to ${concatCategoryName( + category_name, + category_number + )}`; } const embed = create_default_embed(client, title, description); diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 13f6bb8..0e3fd6e 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -75,3 +75,11 @@ export function cleanChannelString(s: string): string { .replace("(", "-") .replace("compsci ", "cs"); } +export function concatCategoryName( + category_name: string, + category_number: number +) { + return category_number == 0 + ? category_name + : category_name + ` ${category_number}`; +} From 60e1daadb285e0bec9544a32f01104c6b4d277f9 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 20:00:04 -0500 Subject: [PATCH 10/32] Clean up createTextChannel --- utils/channelUtils.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 0e3fd6e..d8f284a 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -27,21 +27,24 @@ export async function createTextChannel( channel_category?: CategoryChannel, channel_category_name?: string ) { - if ( - channel_category_name == undefined && - channel_category_name == undefined - ) { + console.log(`Category: ${channel_category}`); + console.log(`Category name: ${channel_category_name}`); + + let channel_parent = undefined; + if (channel_category !== undefined) { + channel_parent = channel_category; + } else if (channel_category_name !== undefined) { + channel_parent = await findCategory(guild, channel_category_name); + } else { throw Error( "Must specify either channel_category or channel_category_name" ); } + return guild.channels.create(channel_name, { type: "GUILD_TEXT", - topic: channel_topic ? channel_topic : "", - parent: - channel_category != undefined - ? channel_category - : await findCategory(guild, channel_category_name), + topic: channel_topic, + parent: channel_parent, }); } From 068deba6216cb7c32582cf2f6d46f5dea51e74cd Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 20:02:32 -0500 Subject: [PATCH 11/32] Further clean up for createTextChannel --- utils/channelUtils.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index d8f284a..4c5b11c 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -22,28 +22,26 @@ export async function findCategory(guild: Guild, category_name: string) { } export async function createTextChannel( guild: Guild, - channel_name: string, - channel_topic: string, - channel_category?: CategoryChannel, - channel_category_name?: string + name: string, + topic: string, + category?: CategoryChannel, + category_name?: string ) { - console.log(`Category: ${channel_category}`); - console.log(`Category name: ${channel_category_name}`); - - let channel_parent = undefined; - if (channel_category !== undefined) { - channel_parent = channel_category; - } else if (channel_category_name !== undefined) { - channel_parent = await findCategory(guild, channel_category_name); + //Determine which arg to use + let channel_parent: CategoryChannel | undefined = undefined; + if (category !== undefined) { + channel_parent = category; + } else if (category_name !== undefined) { + channel_parent = await findCategory(guild, category_name); } else { throw Error( "Must specify either channel_category or channel_category_name" ); } - return guild.channels.create(channel_name, { + return guild.channels.create(name, { type: "GUILD_TEXT", - topic: channel_topic, + topic: topic, parent: channel_parent, }); } From 178acab24d20d992020a106f58ec938e6abadcf8 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 20:03:32 -0500 Subject: [PATCH 12/32] Deepsource --- commands/owner/csCreateChannels.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 38f7bad..2b1aa80 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -58,7 +58,7 @@ export default { return course; }); - let category_name = "COMP SCI CLASSES"; + const category_name = "COMP SCI CLASSES"; let category_number = 0; const max_category_size = 50; From 31e1d5f462a0fa05a04ca886f6bc852723e0c787 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 20:16:23 -0500 Subject: [PATCH 13/32] Fixed issue where you could move a channel into a full category --- commands/owner/csCreateChannels.ts | 26 ++++++++++++-------------- utils/channelUtils.ts | 6 +++--- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 2b1aa80..10b48c3 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -94,22 +94,20 @@ export default { cleaned_courses[index].CODE ); - // Create new channels - if (channel === undefined || channel.type !== "GUILD_TEXT") { - let category = await findCategory( + let category = await findCategory( + msgInt.guild, + concatCategoryName(category_name, category_number) + ); + // Increment category if category is full + if (category.children.size >= max_category_size) { + ++category_number; + category = await findCategory( msgInt.guild, concatCategoryName(category_name, category_number) ); - - // Increment category if category is full - if (category.children.size >= max_category_size) { - ++category_number; - category = await findCategory( - msgInt.guild, - concatCategoryName(category_name, category_number) - ); - } - + } + // Create new channels + if (channel === undefined || channel.type !== "GUILD_TEXT") { const new_channel = await createTextChannel( msgInt.guild, cleaned_courses[index].CODE, @@ -127,7 +125,7 @@ export default { move_channel_count += await moveChannel( msgInt.guild, channel, - "COMP SCI CLASSES" + concatCategoryName(category_name, category_number) ); //TODO: Confirm channel ID is in db diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 4c5b11c..611f7e7 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -61,7 +61,7 @@ export async function moveChannel( console.log( chalk.yellow( - `Moved channel${channel.name} from: ${channel.parent?.name} to: ${category_name}` + `Moved channel ${channel.name} from: ${channel.parent?.name} to: ${category_name}` ) ); return 1; @@ -73,8 +73,8 @@ export function cleanChannelString(s: string): string { return s .toLowerCase() .replace(/[`~!@#$%^&*)|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "") - .replace("(", "-") - .replace("compsci ", "cs"); + .replace("compsci ", "cs") + .replace(/[ (]/gi, "-"); } export function concatCategoryName( category_name: string, From be7fcdf6c93c410d17728994ad687826a75362a1 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 20:20:56 -0500 Subject: [PATCH 14/32] Prevent excessive moving --- commands/owner/csCreateChannels.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 10b48c3..86576c0 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -121,7 +121,10 @@ export default { //TODO: Write channel id to db } // Move old channels - else { + else if ( + channel.parent !== null && + !channel.parent.name.startsWith(category_name) + ) { move_channel_count += await moveChannel( msgInt.guild, channel, From b4071fac8142aad6de8a72b0f68869223281c4a7 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 20:35:47 -0500 Subject: [PATCH 15/32] Clean strings in functions that need it Save course id to db --- commands/owner/csCreateChannels.ts | 15 ++++++++------- utils/channelUtils.ts | 3 ++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 86576c0..4f42b01 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -87,12 +87,9 @@ export default { } } - for (let index = 0; index < cleaned_courses.length; index++) { + for (let index = 0; index < courses.length; index++) { // Iterate through courses in db - const channel = checkForChannel( - msgInt.guild, - cleaned_courses[index].CODE - ); + const channel = checkForChannel(msgInt.guild, courses[index].CODE); let category = await findCategory( msgInt.guild, @@ -110,8 +107,8 @@ export default { if (channel === undefined || channel.type !== "GUILD_TEXT") { const new_channel = await createTextChannel( msgInt.guild, - cleaned_courses[index].CODE, - cleaned_courses[index].INFO, + courses[index].CODE, + courses[index].INFO, category ); @@ -119,6 +116,8 @@ export default { console.log(chalk.yellow(`Created channel: ${new_channel.name}`)); //TODO: Write channel id to db + courses[index].CHANNEL_ID = new_channel.id; + courses[index].save(); } // Move old channels else if ( @@ -132,6 +131,8 @@ export default { ); //TODO: Confirm channel ID is in db + courses[index].CHANNEL_ID = channel.id; + courses[index].save(); } } diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 611f7e7..23e0ff3 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -2,6 +2,7 @@ import { CategoryChannel, Guild, GuildChannel } from "discord.js"; import chalk from "chalk"; export function checkForChannel(guild: Guild, channel_name: string) { + channel_name = cleanChannelString(channel_name); return guild.channels.cache.find((channel) => { return channel.name == channel_name; }); @@ -39,7 +40,7 @@ export async function createTextChannel( ); } - return guild.channels.create(name, { + return guild.channels.create(cleanChannelString(name), { type: "GUILD_TEXT", topic: topic, parent: channel_parent, From 38c2a8dacafe9ca23707bd39f09a4da479b3299c Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Thu, 8 Sep 2022 20:38:03 -0500 Subject: [PATCH 16/32] Deepsource --- commands/owner/deleteChannels.ts | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 commands/owner/deleteChannels.ts diff --git a/commands/owner/deleteChannels.ts b/commands/owner/deleteChannels.ts new file mode 100644 index 0000000..4fe0e29 --- /dev/null +++ b/commands/owner/deleteChannels.ts @@ -0,0 +1,65 @@ +import { MessageEmbed } from "discord.js"; + +import chalk from "chalk"; +import { ICommand } from "wokcommands"; + +export default { + name: "deleteAllChannels", + category: "owner", + description: "Delete all channels not named general or banshee", + slash: true, + testOnly: false, + guildOnly: true, + requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"], + ownerOnly: true, + + callback: async ({ client, interaction: msgInt }) => { + if (msgInt.guild === null) { + console.log(chalk.red("No guild")); + return; + } + + const color = "#0099ff"; + const thumbnail = ""; + const title = "Delete all channels"; + const description = `deleted channels`; + const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; + const footerIcon = ""; + + // Embed construction + const embed = new MessageEmbed() + .setColor(color) + .setTitle(title) + .setThumbnail(thumbnail) + .setDescription(description) + .setFooter({ text: footer, iconURL: footerIcon }); + + await msgInt.deferReply({ ephemeral: true }); + + msgInt.guild.channels.cache.forEach((channel) => { + if ( + !( + channel == undefined || + channel?.name == "general" || // Channels and categories to keep + channel?.name == "banshee" + ) + ) { + console.log(`Deleted channel: ${channel?.name}`); + channel.delete(); + } + }); + + await msgInt.editReply({ embeds: [embed] }); + + // Log the command usage + console.log( + chalk.blue( + `${chalk.green(`[COMMAND]`)} ${chalk.yellow( + msgInt.user.tag + )} used the ${chalk.green( + `/csCreateChannels` + )} command in ${chalk.yellow(msgInt.guild?.name)}` + ) + ); + }, +} as ICommand; From ca6e279759da87314c55f6b3e607d4b18838d388 Mon Sep 17 00:00:00 2001 From: schiltz3 Date: Fri, 9 Sep 2022 01:38:27 +0000 Subject: [PATCH 17/32] Apply auto formatting changes --- commands/owner/deleteChannels.ts | 130 +++++++++++++++---------------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/commands/owner/deleteChannels.ts b/commands/owner/deleteChannels.ts index 4fe0e29..3515034 100644 --- a/commands/owner/deleteChannels.ts +++ b/commands/owner/deleteChannels.ts @@ -1,65 +1,65 @@ -import { MessageEmbed } from "discord.js"; - -import chalk from "chalk"; -import { ICommand } from "wokcommands"; - -export default { - name: "deleteAllChannels", - category: "owner", - description: "Delete all channels not named general or banshee", - slash: true, - testOnly: false, - guildOnly: true, - requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"], - ownerOnly: true, - - callback: async ({ client, interaction: msgInt }) => { - if (msgInt.guild === null) { - console.log(chalk.red("No guild")); - return; - } - - const color = "#0099ff"; - const thumbnail = ""; - const title = "Delete all channels"; - const description = `deleted channels`; - const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; - const footerIcon = ""; - - // Embed construction - const embed = new MessageEmbed() - .setColor(color) - .setTitle(title) - .setThumbnail(thumbnail) - .setDescription(description) - .setFooter({ text: footer, iconURL: footerIcon }); - - await msgInt.deferReply({ ephemeral: true }); - - msgInt.guild.channels.cache.forEach((channel) => { - if ( - !( - channel == undefined || - channel?.name == "general" || // Channels and categories to keep - channel?.name == "banshee" - ) - ) { - console.log(`Deleted channel: ${channel?.name}`); - channel.delete(); - } - }); - - await msgInt.editReply({ embeds: [embed] }); - - // Log the command usage - console.log( - chalk.blue( - `${chalk.green(`[COMMAND]`)} ${chalk.yellow( - msgInt.user.tag - )} used the ${chalk.green( - `/csCreateChannels` - )} command in ${chalk.yellow(msgInt.guild?.name)}` - ) - ); - }, -} as ICommand; +import { MessageEmbed } from "discord.js"; + +import chalk from "chalk"; +import { ICommand } from "wokcommands"; + +export default { + name: "deleteAllChannels", + category: "owner", + description: "Delete all channels not named general or banshee", + slash: true, + testOnly: false, + guildOnly: true, + requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"], + ownerOnly: true, + + callback: async ({ client, interaction: msgInt }) => { + if (msgInt.guild === null) { + console.log(chalk.red("No guild")); + return; + } + + const color = "#0099ff"; + const thumbnail = ""; + const title = "Delete all channels"; + const description = `deleted channels`; + const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; + const footerIcon = ""; + + // Embed construction + const embed = new MessageEmbed() + .setColor(color) + .setTitle(title) + .setThumbnail(thumbnail) + .setDescription(description) + .setFooter({ text: footer, iconURL: footerIcon }); + + await msgInt.deferReply({ ephemeral: true }); + + msgInt.guild.channels.cache.forEach((channel) => { + if ( + !( + channel == undefined || + channel?.name == "general" || // Channels and categories to keep + channel?.name == "banshee" + ) + ) { + console.log(`Deleted channel: ${channel?.name}`); + channel.delete(); + } + }); + + await msgInt.editReply({ embeds: [embed] }); + + // Log the command usage + console.log( + chalk.blue( + `${chalk.green(`[COMMAND]`)} ${chalk.yellow( + msgInt.user.tag + )} used the ${chalk.green( + `/csCreateChannels` + )} command in ${chalk.yellow(msgInt.guild?.name)}` + ) + ); + }, +} as ICommand; From 57d4e88a5f4ff5ffb8560ffec0541cc8c75cb8cb Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Fri, 9 Sep 2022 11:38:09 -0500 Subject: [PATCH 18/32] deepsource --- utils/channelUtils.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 23e0ff3..2151a94 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -1,6 +1,14 @@ import { CategoryChannel, Guild, GuildChannel } from "discord.js"; import chalk from "chalk"; +export function cleanChannelString(s: string): string { + return s + .toLowerCase() + .replace(/[`~!@#$%^&*)|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "") + .replace("compsci ", "cs") + .replace(/[ (]/gi, "-"); +} + export function checkForChannel(guild: Guild, channel_name: string) { channel_name = cleanChannelString(channel_name); return guild.channels.cache.find((channel) => { @@ -70,13 +78,6 @@ export async function moveChannel( return 0; } -export function cleanChannelString(s: string): string { - return s - .toLowerCase() - .replace(/[`~!@#$%^&*)|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "") - .replace("compsci ", "cs") - .replace(/[ (]/gi, "-"); -} export function concatCategoryName( category_name: string, category_number: number From 74d123495ec540751796111d638c8241b5bc30f8 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Sat, 10 Sep 2022 08:51:00 -0500 Subject: [PATCH 19/32] Change string cleaning --- utils/channelUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 2151a94..6f64875 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -4,9 +4,9 @@ import chalk from "chalk"; export function cleanChannelString(s: string): string { return s .toLowerCase() - .replace(/[`~!@#$%^&*)|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "") + .replace(/[`~!@#$%^&*())|+=?;:'",.<>\{\}\[\]\\\/]/gi, "") .replace("compsci ", "cs") - .replace(/[ (]/gi, "-"); + .replace(" ", "-"); } export function checkForChannel(guild: Guild, channel_name: string) { From 3395191d8597dfc4035d776b1162bb560b4a6936 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Sat, 10 Sep 2022 08:51:12 -0500 Subject: [PATCH 20/32] Only clean channel strings --- commands/owner/csCreateChannels.ts | 5 ++++- utils/channelUtils.ts | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 4f42b01..eaee25c 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -89,7 +89,10 @@ export default { for (let index = 0; index < courses.length; index++) { // Iterate through courses in db - const channel = checkForChannel(msgInt.guild, courses[index].CODE); + const channel = checkForChannel( + msgInt.guild, + cleanChannelString(courses[index].CODE) + ); let category = await findCategory( msgInt.guild, diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 6f64875..1ed5392 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -10,7 +10,6 @@ export function cleanChannelString(s: string): string { } export function checkForChannel(guild: Guild, channel_name: string) { - channel_name = cleanChannelString(channel_name); return guild.channels.cache.find((channel) => { return channel.name == channel_name; }); From cfcc96318b20c3322b30b77a40a3eb7b247978b8 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Sat, 10 Sep 2022 08:51:27 -0500 Subject: [PATCH 21/32] WIP: Moving old channels not working --- commands/owner/csCreateChannels.ts | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index eaee25c..e27cf42 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -66,26 +66,37 @@ export default { let new_channel_count = 0; let move_channel_count = 0; - //Move classes no longer in the db to - const cs_category = checkForChannel( + console.log( + `category name: ${concatCategoryName(category_name, category_number)}` + ); + //Move classes no longer in the db to cs_past_category_name + let cs_category = checkForChannel( msgInt.guild, concatCategoryName(category_name, category_number) ); - if (cs_category != undefined && cs_category.type == "GUILD_CATEGORY") { + console.log(`Channel: ${cs_category}`); + + while (cs_category != undefined && cs_category.type == "GUILD_CATEGORY") { + console.log(`Checking category: ${cs_category.name}`); const children = Array.from(cs_category.children.values()); for (let index = 0; index < children.length; index++) { + console.log(`Checking channel: ${children[index].name}`); const match = cleaned_courses.find((course) => { return course.CODE == children[index].name; }); - if (match == undefined) { - await moveChannel( - msgInt.guild, - children[index], - cs_past_category_name - ); + if (match != undefined) { + continue; } + await moveChannel(msgInt.guild, children[index], cs_past_category_name); } + + ++category_number; + cs_category = checkForChannel( + msgInt.guild, + concatCategoryName(category_name, category_number) + ); } + category_number = 0; for (let index = 0; index < courses.length; index++) { // Iterate through courses in db From 8cd44edb04f425c3455c4a3230b70a1d976fa90d Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Sat, 10 Sep 2022 20:24:36 -0500 Subject: [PATCH 22/32] Refresh cache, and lower max number of channels by 1 Also add log messages --- commands/owner/csCreateChannels.ts | 46 +++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index e27cf42..0949ec3 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -77,16 +77,15 @@ export default { console.log(`Channel: ${cs_category}`); while (cs_category != undefined && cs_category.type == "GUILD_CATEGORY") { - console.log(`Checking category: ${cs_category.name}`); const children = Array.from(cs_category.children.values()); for (let index = 0; index < children.length; index++) { - console.log(`Checking channel: ${children[index].name}`); const match = cleaned_courses.find((course) => { return course.CODE == children[index].name; }); if (match != undefined) { continue; } + console.log(`Moving: ${children[index]} to: ${cs_past_category_name}`); await moveChannel(msgInt.guild, children[index], cs_past_category_name); } @@ -105,18 +104,34 @@ export default { cleanChannelString(courses[index].CODE) ); - let category = await findCategory( - msgInt.guild, - concatCategoryName(category_name, category_number) - ); - // Increment category if category is full - if (category.children.size >= max_category_size) { - ++category_number; - category = await findCategory( + let category = await ( + await findCategory( msgInt.guild, concatCategoryName(category_name, category_number) + ) + ).fetch(true); + // Increment category if category is full + if (category.children.size >= max_category_size - 1) { + ++category_number; + category = await ( + await findCategory( + msgInt.guild, + concatCategoryName(category_name, category_number) + ) + ).fetch(true); + console.log( + `old category full, new category: ${concatCategoryName( + category_name, + category_number + )} created` ); } + console.log( + `Working in category: ${concatCategoryName( + category_name, + category_number + )} size: ${category.children.size}` + ); // Create new channels if (channel === undefined || channel.type !== "GUILD_TEXT") { const new_channel = await createTextChannel( @@ -132,12 +147,17 @@ export default { //TODO: Write channel id to db courses[index].CHANNEL_ID = new_channel.id; courses[index].save(); - } - // Move old channels - else if ( + } else if ( channel.parent !== null && !channel.parent.name.startsWith(category_name) ) { + // Move old channels + console.log( + `Moving: ${channel.name} to: ${concatCategoryName( + category_name, + category_number + )}` + ); move_channel_count += await moveChannel( msgInt.guild, channel, From 652990532a5cd41b6e0d7d749239e609b3b35c52 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Sat, 10 Sep 2022 21:29:35 -0500 Subject: [PATCH 23/32] Update cleanString --- utils/channelUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 1ed5392..5f2cc8f 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -4,9 +4,9 @@ import chalk from "chalk"; export function cleanChannelString(s: string): string { return s .toLowerCase() - .replace(/[`~!@#$%^&*())|+=?;:'",.<>\{\}\[\]\\\/]/gi, "") + .replace(/[`~!@#$%^&*))|+=?;:'",.<>\{\}\[\]\\\/]/gi, "") .replace("compsci ", "cs") - .replace(" ", "-"); + .replace(/[ (]/gi, "-"); } export function checkForChannel(guild: Guild, channel_name: string) { From 27ab197aa37a95168e7737273acec0c5e252389d Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Sat, 10 Sep 2022 21:58:48 -0500 Subject: [PATCH 24/32] Change cleaned_courses data structure --- commands/owner/csCreateChannels.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 0949ec3..2e3a44d 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -52,11 +52,15 @@ export default { const courses = await classModel.find({}).sort({ CODE: 1 }); - //create a copy of the courses with cleaned names - const cleaned_courses = Array.from(courses).map((course) => { - course.CODE = cleanChannelString(course.CODE); - return course; - }); + //create an array of the courses with cleaned names + const cleaned_courses: string[] = []; + for (let index = 0; index < courses.length; index++) { + cleaned_courses.push(cleanChannelString(courses[index].CODE)); + } + + // for (let index = 0; index < courses.length; index++) { + // console.log(courses[index].CODE); + // } const category_name = "COMP SCI CLASSES"; let category_number = 0; @@ -80,7 +84,7 @@ export default { const children = Array.from(cs_category.children.values()); for (let index = 0; index < children.length; index++) { const match = cleaned_courses.find((course) => { - return course.CODE == children[index].name; + return course == children[index].name; }); if (match != undefined) { continue; @@ -110,6 +114,7 @@ export default { concatCategoryName(category_name, category_number) ) ).fetch(true); + // Increment category if category is full if (category.children.size >= max_category_size - 1) { ++category_number; @@ -132,11 +137,12 @@ export default { category_number )} size: ${category.children.size}` ); + // Create new channels if (channel === undefined || channel.type !== "GUILD_TEXT") { const new_channel = await createTextChannel( msgInt.guild, - courses[index].CODE, + cleanChannelString(courses[index].CODE), courses[index].INFO, category ); @@ -144,7 +150,6 @@ export default { ++new_channel_count; console.log(chalk.yellow(`Created channel: ${new_channel.name}`)); - //TODO: Write channel id to db courses[index].CHANNEL_ID = new_channel.id; courses[index].save(); } else if ( @@ -164,7 +169,6 @@ export default { concatCategoryName(category_name, category_number) ); - //TODO: Confirm channel ID is in db courses[index].CHANNEL_ID = channel.id; courses[index].save(); } From b19873734a8cd30a7637a9cfc1da8b7dc00f5e53 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Sat, 10 Sep 2022 22:18:49 -0500 Subject: [PATCH 25/32] Update existing channel descriptions --- commands/owner/csCreateChannels.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 2e3a44d..0d036d6 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -163,6 +163,7 @@ export default { category_number )}` ); + channel.edit({ topic: courses[index].INFO }); move_channel_count += await moveChannel( msgInt.guild, channel, @@ -171,6 +172,11 @@ export default { courses[index].CHANNEL_ID = channel.id; courses[index].save(); + } else if ( + channel.parent !== null && + channel.parent.name.startsWith(category_name) + ) { + channel.edit({ topic: courses[index].INFO }); } } From 5b3a1740818d9d627e9f47faa7c3756e56babe08 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Sat, 10 Sep 2022 23:08:43 -0500 Subject: [PATCH 26/32] Ping members --- commands/owner/csCreateChannels.ts | 35 +++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 0d036d6..96e4bd4 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -32,6 +32,21 @@ function create_default_embed( .setFooter({ text: footer, iconURL: footerIcon }); return embed; } + +function cleanRoleString(role_name: string): string { + let clean_role_name: string = role_name + .toLowerCase() + .replace(/[`~!@#$%^&*))|+=?;:'",.<>\{\}\[\]\\\/]/gi, "") + .replace(/[ (]/gi, "-"); + + if (Number.isInteger(role_name[role_name.length - 1])) { + const num = Number(role_name[role_name.length - 1]) - 1; + clean_role_name = role_name.slice(0, role_name.length - 2) + String(num); + return clean_role_name; + } + return clean_role_name; +} + export default { name: "csCreateChannels", category: "owner", @@ -152,11 +167,28 @@ export default { courses[index].CHANNEL_ID = new_channel.id; courses[index].save(); + + // Ping members who have this role + const role = msgInt.guild.roles.cache.find((role) => { + // console.log( + // `${cleanRoleString(role.name)}:${cleanChannelString( + // courses[index].CODE + // )}` + // ); + return ( + cleanRoleString(role.name) == + cleanChannelString(courses[index].CODE) + ); + }); + if (role !== undefined) { + //Ping member + new_channel.send(`Hey! <@&${role.id}> here is a channel for you!`); + } } else if ( channel.parent !== null && !channel.parent.name.startsWith(category_name) ) { - // Move old channels + // Moves and updates old channels console.log( `Moving: ${channel.name} to: ${concatCategoryName( category_name, @@ -176,6 +208,7 @@ export default { channel.parent !== null && channel.parent.name.startsWith(category_name) ) { + // updates old channels channel.edit({ topic: courses[index].INFO }); } } From 3e34c3794bf3f0ed65d81ab667ccf6d1162544bf Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Sat, 10 Sep 2022 23:11:57 -0500 Subject: [PATCH 27/32] Ping roles if you move the course from past courses --- commands/owner/csCreateChannels.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 96e4bd4..0ced656 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -170,11 +170,6 @@ export default { // Ping members who have this role const role = msgInt.guild.roles.cache.find((role) => { - // console.log( - // `${cleanRoleString(role.name)}:${cleanChannelString( - // courses[index].CODE - // )}` - // ); return ( cleanRoleString(role.name) == cleanChannelString(courses[index].CODE) @@ -204,6 +199,17 @@ export default { courses[index].CHANNEL_ID = channel.id; courses[index].save(); + // Ping members who have this role + const role = msgInt.guild.roles.cache.find((role) => { + return ( + cleanRoleString(role.name) == + cleanChannelString(courses[index].CODE) + ); + }); + if (role !== undefined) { + //Ping member + channel.send(`Hey! <@&${role.id}> here is the channel for you!`); + } } else if ( channel.parent !== null && channel.parent.name.startsWith(category_name) From 13564ba621b581c0694f49bedb968046cbde492e Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Mon, 12 Sep 2022 15:16:17 -0500 Subject: [PATCH 28/32] Remove delete command --- commands/owner/deleteChannels.ts | 65 -------------------------------- 1 file changed, 65 deletions(-) delete mode 100644 commands/owner/deleteChannels.ts diff --git a/commands/owner/deleteChannels.ts b/commands/owner/deleteChannels.ts deleted file mode 100644 index 3515034..0000000 --- a/commands/owner/deleteChannels.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { MessageEmbed } from "discord.js"; - -import chalk from "chalk"; -import { ICommand } from "wokcommands"; - -export default { - name: "deleteAllChannels", - category: "owner", - description: "Delete all channels not named general or banshee", - slash: true, - testOnly: false, - guildOnly: true, - requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"], - ownerOnly: true, - - callback: async ({ client, interaction: msgInt }) => { - if (msgInt.guild === null) { - console.log(chalk.red("No guild")); - return; - } - - const color = "#0099ff"; - const thumbnail = ""; - const title = "Delete all channels"; - const description = `deleted channels`; - const footer = `Delivered in: ${client.ws.ping}ms | CSSC-bot | ${process.env.VERSION}`; - const footerIcon = ""; - - // Embed construction - const embed = new MessageEmbed() - .setColor(color) - .setTitle(title) - .setThumbnail(thumbnail) - .setDescription(description) - .setFooter({ text: footer, iconURL: footerIcon }); - - await msgInt.deferReply({ ephemeral: true }); - - msgInt.guild.channels.cache.forEach((channel) => { - if ( - !( - channel == undefined || - channel?.name == "general" || // Channels and categories to keep - channel?.name == "banshee" - ) - ) { - console.log(`Deleted channel: ${channel?.name}`); - channel.delete(); - } - }); - - await msgInt.editReply({ embeds: [embed] }); - - // Log the command usage - console.log( - chalk.blue( - `${chalk.green(`[COMMAND]`)} ${chalk.yellow( - msgInt.user.tag - )} used the ${chalk.green( - `/csCreateChannels` - )} command in ${chalk.yellow(msgInt.guild?.name)}` - ) - ); - }, -} as ICommand; From 7d728e20bc503bd983732dadf8609cb7542f332d Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Mon, 12 Sep 2022 15:22:04 -0500 Subject: [PATCH 29/32] Remove unused code --- commands/owner/csCreateChannels.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 0ced656..58d37a5 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -38,12 +38,7 @@ function cleanRoleString(role_name: string): string { .toLowerCase() .replace(/[`~!@#$%^&*))|+=?;:'",.<>\{\}\[\]\\\/]/gi, "") .replace(/[ (]/gi, "-"); - - if (Number.isInteger(role_name[role_name.length - 1])) { - const num = Number(role_name[role_name.length - 1]) - 1; - clean_role_name = role_name.slice(0, role_name.length - 2) + String(num); - return clean_role_name; - } + console.log(`${role_name}:${clean_role_name}`); return clean_role_name; } From df4e24792b20bcc99f2d6c17b005be5cc9bdffa5 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Mon, 12 Sep 2022 15:32:15 -0500 Subject: [PATCH 30/32] Deepsource --- commands/owner/csCreateChannels.ts | 2 +- utils/channelUtils.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 58d37a5..18523db 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -34,7 +34,7 @@ function create_default_embed( } function cleanRoleString(role_name: string): string { - let clean_role_name: string = role_name + const clean_role_name: string = role_name .toLowerCase() .replace(/[`~!@#$%^&*))|+=?;:'",.<>\{\}\[\]\\\/]/gi, "") .replace(/[ (]/gi, "-"); diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 5f2cc8f..1c8662b 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -36,7 +36,7 @@ export async function createTextChannel( category_name?: string ) { //Determine which arg to use - let channel_parent: CategoryChannel | undefined = undefined; + let channel_parent: CategoryChannel | undefined; if (category !== undefined) { channel_parent = category; } else if (category_name !== undefined) { From 4efe07b84761f6ea1fe9e6f5ea4d0b05c5e91e99 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Mon, 12 Sep 2022 15:36:39 -0500 Subject: [PATCH 31/32] Deepsource --- utils/channelUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 1c8662b..2adaf59 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -83,5 +83,5 @@ export function concatCategoryName( ) { return category_number == 0 ? category_name - : category_name + ` ${category_number}`; + : `${category_name} ${category_number}!`; } From 4afa13d91d114386b71c1b87b3ee75d390995bc5 Mon Sep 17 00:00:00 2001 From: John Schiltz Date: Mon, 12 Sep 2022 15:40:08 -0500 Subject: [PATCH 32/32] Deepsource --- commands/owner/csCreateChannels.ts | 1 - utils/channelUtils.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/commands/owner/csCreateChannels.ts b/commands/owner/csCreateChannels.ts index 18523db..c1977fe 100644 --- a/commands/owner/csCreateChannels.ts +++ b/commands/owner/csCreateChannels.ts @@ -38,7 +38,6 @@ function cleanRoleString(role_name: string): string { .toLowerCase() .replace(/[`~!@#$%^&*))|+=?;:'",.<>\{\}\[\]\\\/]/gi, "") .replace(/[ (]/gi, "-"); - console.log(`${role_name}:${clean_role_name}`); return clean_role_name; } diff --git a/utils/channelUtils.ts b/utils/channelUtils.ts index 2adaf59..7766d4d 100644 --- a/utils/channelUtils.ts +++ b/utils/channelUtils.ts @@ -83,5 +83,5 @@ export function concatCategoryName( ) { return category_number == 0 ? category_name - : `${category_name} ${category_number}!`; + : `${category_name} ${category_number}`; }