Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 94 additions & 89 deletions commands/owner/csClassPoll.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,113 @@
import chalk from "chalk";
import { MessageEmbed, MessageActionRow, MessageSelectMenu, MessageActionRowComponent } from "discord.js";
import { MessageEmbed, MessageActionRow, MessageSelectMenu, MessageSelectOptionData } from "discord.js";
import { ICommand } from "wokcommands";
import { checkForRoles } from "../../rolesOps";
import csClassOptions from "../../csClasses";
import classes from "../../data/COMPSCI-Fall-Class-List.json"
import { generate } from 'short-uuid';
export interface Class {
code: string,
title: string,
info: string
}

export default {
name: "csClassPoll",
category: "owner",
description: "Posts the Cs Class Poll",
slash: true,
testOnly: false,
guildOnly: true,
requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"],
ownerOnly: true,
name: "csClassPoll",
category: "owner",
description: "Posts the Cs Class Poll",
slash: true,
testOnly: false,
guildOnly: true,
requiredPermissions: ["MANAGE_GUILD", "MANAGE_ROLES"],
ownerOnly: true,

callback: async ({ client, interaction: msgInt }) => {
// Define embeds used in this command
const infoEmbed = new MessageEmbed()
.setTitle("Choose a role")
.setColor("#0099ff")
.setDescription("Select the option(s) with your current COMPSCI classes.")
.setFooter({
text: `Delivered in: ${client.ws.ping}ms | CSSC-Bot | ${process.env.VERSION}`,
iconURL: "https://playantares.com/resources/CSSC-bot/icon.jpg",
});
callback: async ({ client, interaction: msgInt }) => {
// 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
)
}`
)
);

// Crashes the call
// if (!checkForRoles(msgInt.guild!, 2)) {
// msgInt.reply("Please run the `/ createRoles` command in this server to create the necessary roles for this poll!");
// return
// }

// Create some vars
let csClassOptionsChunks = [];
let lessThan25Row = new MessageActionRow()
let greaterThan25RowList: MessageActionRowComponent[] = [];
console.log(`Number of classes ${classes.length}`)

// Check if csClassOptions is larger than 25 items
if (csClassOptions.length > 25) {
// Split csClassOptions into chunks of 25
for (let i = 0; i < csClassOptions.length; i += 25) {
csClassOptionsChunks.push(csClassOptions.slice(i, i + 25));
console.log(csClassOptions.slice(i, i + 25)) //! DEBUG
console.log("got here") //! DEBUG
}
}
// Loop through csClassOptionsChunks
for (let i = 0; i < csClassOptionsChunks.length; i++) {
// Loop through csClassOptionsChunks[i]
let menu = new MessageSelectMenu()
menu.setCustomId(`csClassPoll+${i}`)
console.log(`csClassPoll+${i}`) //! DEBUG
menu.setPlaceholder("Select an option")
lessThan25Row.addComponents(menu);
for (let j = 0; j < csClassOptionsChunks[i].length; j++) {
// Create row one of the buttons for the poll
menu.addOptions(
{
label: csClassOptionsChunks[i][j].label,
value: csClassOptionsChunks[i][j].label,
description: csClassOptionsChunks[i][j].description,
}
)
}
greaterThan25RowList.push(menu);
}
// split classes into chunks of 25
const class_chunks: Class[][] = split_list(classes, 25)

//TODO: remove logging
console.log(`Number of chunks: ${class_chunks.length}`)
//TODO: remove logging
class_chunks.forEach(
(class_chunk, index) => {
console.log(`Chunk: ${index + 1},\t length: ${class_chunk.length}`)
}
)

// Check if greaterThan25RowList contains any items, and if it does, loop through greaterThan25RowList
if (greaterThan25RowList.length > 0) {
// Create menus from chunks
class_chunks.forEach(
(class_chunk, index) => {
const menu = new MessageSelectMenu()
menu.setCustomId(`csClassPoll+${index}`)
menu.setPlaceholder("Select an option")
// create a new list of options from the classes and add to menu
menu.addOptions(class_chunk.map(create_option_from_class))

// console.log(greaterThan25RowList) //! DEBUG

for (let i = 0; i < greaterThan25RowList.length; i++) {
// Loop through greaterThan25RowList[i]
let row = new MessageActionRow()
row.addComponents(greaterThan25RowList[i])
msgInt.channel!.send({ embeds: [infoEmbed], components: [row] });
}
} else {
msgInt.reply({ embeds: [infoEmbed], components: [lessThan25Row] });
}
// Add single message to action row
const row = new MessageActionRow()
row.addComponents(menu)

if (index == 0) {
// Define embeds used in this command
const infoEmbed = new MessageEmbed()
.setTitle("Choose a role")
.setColor("#0099ff")
.setDescription("Select the option(s) with your current 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], components: [row] });
}
else {
msgInt.channel!.send({ components: [row] });
}
}
)
},
} as ICommand;



// Splits any size list into lists of at most `max_list_len`
function split_list(list: Array<any>, max_list_len: number) {
let class_chunks = []
for (let i = 0; i < list.length; i += max_list_len) {
class_chunks.push(list.slice(i, i + max_list_len))

// Send the embed and message component rows
// rows.forEach((row) => {
// msgInt.reply({ embeds: [infoEmbed], components: [row] });
// });
}
return class_chunks
}

// consumes a Class and returns Message Selec tOption data
function create_option_from_class(_class: Class): MessageSelectOptionData {
if (_class.code.length > 100) {
console.log("\n")
console.log("TOO_LONG: class code")
console.log(_class)
console.log("\n")

// if (!checkForRoles(msgInt.guild!, 2)) {
// msgInt.reply("Please run the `/createRoles` command in this server to create the necessary roles for this poll!");
// } else {
// rows.forEach((row) => {
// msgInt.reply({ embeds: [infoEmbed], components: [row] });
// });
// }
}
return {
label: _class.code,
value: generate(),
description: _class.title,
}
}

// 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;
192 changes: 0 additions & 192 deletions csClasses.ts

This file was deleted.

Loading