Skip to content

Instantly share code, notes, and snippets.

@ash-development
Created July 3, 2023 02:15
const { SlashCommandBuilder } = require("@discordjs/builders");
const fs = require("fs");
const csv = require("csv-parser");
const csvFilePath = "./CSV/Teams/July1.csv";
module.exports = {
data: new SlashCommandBuilder()
.setName("teamrank")
.setDescription("View the rank of a team.")
.addStringOption((option) =>
option
.setName("team")
.setDescription("Select a team from the list.")
.setRequired(true)
.addChoices(fetchTeamChoices())
),
run: async (client, interaction) => {
const teamName = interaction.options.getString("team");
let found = false;
fs.createReadStream(csvFilePath)
.pipe(csv())
.on("data", (row) => {
if (row["Team Name"] === teamName) {
found = true;
interaction.reply(`The rank of ${teamName} is: ${row["Rank"]}`);
}
})
.on("end", () => {
if (!found) {
interaction.reply("Team not found in the database.");
}
});
},
};
function fetchTeamChoices() {
return new Promise((resolve, reject) => {
const choices = [];
fs.createReadStream(csvFilePath)
.pipe(csv())
.on("data", (row) => {
const teamName = row["Team Name"];
choices.push({ name: teamName, value: teamName });
})
.on("end", () => {
if (choices.length === 0) {
reject(new Error("No team choices found."));
} else {
resolve(choices);
}
})
.on("error", (error) => {
reject(error);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment