Add countChars function
This commit is contained in:
@@ -4,19 +4,19 @@ module.exports = {
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('count_chars')
|
||||
.setDescription('Counts the number of characters in a string')
|
||||
.addStringOption( option =>
|
||||
.addStringOption(option =>
|
||||
option.setName('string')
|
||||
.setDescription("String to count characters in")
|
||||
.setRequired(false)
|
||||
)
|
||||
.addStringOption( option =>
|
||||
option.setName('messageid')
|
||||
.setDescription("String to count characters in")
|
||||
.setRequired(false)
|
||||
)
|
||||
.addStringOption(option =>
|
||||
option.setName('messageid')
|
||||
.setDescription("Message ID of message to count characters in")
|
||||
.setRequired(false)
|
||||
),
|
||||
async execute(interaction) {
|
||||
),
|
||||
async execute(interaction, client) {
|
||||
var inputString;
|
||||
|
||||
|
||||
if (interaction.options.getString('string') !== null) {
|
||||
inputString = interaction.options.getString('string');
|
||||
} else if (interaction.options.getString('messageid') !== null) {
|
||||
@@ -24,9 +24,9 @@ module.exports = {
|
||||
const messagefromId = await client.channels.cache.get(interaction.channel.id).messages.fetch(messageId);
|
||||
inputString = messagefromId.content;
|
||||
}
|
||||
|
||||
|
||||
await interaction.reply("Input: `" + inputString + "`\n" + "Output: " + codeBlock("", countChars(inputString)));
|
||||
console.log("User " + interaction.user.tag + " ran /count_chars");
|
||||
console.log("User " + interaction.user.tag + " ran /count_chars");
|
||||
},
|
||||
};
|
||||
|
||||
@@ -37,13 +37,13 @@ function countChars(string) {
|
||||
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) {
|
||||
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";
|
||||
outputString += "Number of " + String.fromCharCode(i + 97).toUpperCase() + "'s: " + letterCount[i] + "\n";
|
||||
}
|
||||
}
|
||||
return outputString;
|
||||
|
Reference in New Issue
Block a user