2023-04-04 16:16:24 -05:00
|
|
|
const { SlashCommandBuilder, Discord, codeBlock } = require('discord.js');
|
2023-04-04 16:12:56 -05:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
data: new SlashCommandBuilder()
|
|
|
|
.setName('count_chars')
|
|
|
|
.setDescription('Counts the number of characters in a string')
|
|
|
|
.addStringOption( option =>
|
|
|
|
option.setName('string')
|
|
|
|
.setDescription("String to count characters in")
|
2023-04-04 17:15:56 -05:00
|
|
|
.setRequired(false)
|
|
|
|
)
|
|
|
|
.addStringOption( option =>
|
|
|
|
option.setName('messageid')
|
|
|
|
.setDescription("Message ID of message to count characters in")
|
|
|
|
.setRequired(false)
|
|
|
|
),
|
2023-04-04 16:12:56 -05:00
|
|
|
async execute(interaction) {
|
2023-04-04 17:15:56 -05:00
|
|
|
var inputString;
|
|
|
|
|
|
|
|
if (interaction.options.getString('string') !== null) {
|
|
|
|
inputString = interaction.options.getString('string');
|
|
|
|
} else if (interaction.options.getString('messageid') !== null) {
|
|
|
|
var messageId = interaction.options.getString('messageid');
|
|
|
|
const messagefromId = await client.channels.cache.get(interaction.channel.id).messages.fetch(messageId);
|
|
|
|
inputString = messagefromId.content;
|
|
|
|
}
|
|
|
|
|
2023-04-04 16:18:28 -05:00
|
|
|
await interaction.reply("Input: `" + inputString + "`\n" + "Output: " + codeBlock("", countChars(inputString)));
|
2023-04-04 16:12:56 -05:00
|
|
|
console.log("User " + interaction.user.tag + " ran /count_chars");
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function countChars(string) {
|
|
|
|
var outputString = "";
|
|
|
|
string = string.toLowerCase();
|
|
|
|
var letterCount = Array(26).fill(0); // Creates an array with 26 values, all equaling 0.
|
|
|
|
for (let i = 0; i < string.length; ++i) {
|
|
|
|
let currentChar = string.charAt(i);
|
|
|
|
let arrayIndex = currentChar.charCodeAt(0) - 97;
|
|
|
|
if (string.charAt(i).search(/^[a-z]+$/) === 0) {
|
|
|
|
letterCount[arrayIndex]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i = 0; i < letterCount.length; ++i) {
|
|
|
|
if (letterCount[i] > 0) {
|
|
|
|
outputString += "Number of " + String.fromCharCode(i + 97).toUpperCase() + "'s: " + letterCount[i] + "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return outputString;
|
|
|
|
}
|
|
|
|
//console.log(countChars("test string"));
|