Home > Software engineering >  Discord.js check if a user nickname contains Numbers and Letter, if yes add calculation to the numbe
Discord.js check if a user nickname contains Numbers and Letter, if yes add calculation to the numbe

Time:11-24

I'm looking for a way to check if a discord users name contains numbers and certain letter, and if it does then apply a calculation to it.

For example I have a command that right now checks if the user has anything in the nickname, this adds the additional number next to the previous one (if there is one), if he doesn't have anything as a nickname it makes the nickname UserName - {number} PRs. I'm looking for a way a to check if the user has {number} PRs in their name and if so, add the multiplyNum calculation to the existing number and preserve the rest of the name.

module.exports = {
    name: 'test',
    cooldown: 1000,
    run: async(client, message, args) => {
        let member = message.mentions.members.first();
        var multiplyNum = args[0] * ((100 - 50) / 100) * 0.23;
        var finalNum = multiplyNum.toFixed(2);

        let memberNick = member ? member.displayName : null;

        if (member.nickname){
            if(!member) return message.channel.send("Invalid User");
            member.setNickname(`${memberNick}`   ", "   `${finalNum}`   " PRs");

        }else{
            if(!member) return message.channel.send("Invalid User");
            member.setNickname(`${memberNick}`   " - "   `${finalNum}`   " PRs");

        };

    }

CodePudding user response:

This should do I think. What you do is that you make nickname into array that splited by spaces and if last key is PRs and last key -1 is !NaN you run the calculation otherwise nothing is done.

module.exports = {
    name: 'test',
    cooldown: 1000,
    run: async(client, message, args) => {
        let member = message.mentions.members.first();
        var multiplyNum = args[0] * ((100 - 50) / 100) * 0.23;
        var finalNum = multiplyNum.toFixed(2);

        let memberNick = member ? member.displayName : null;

        if (member.nickname){
            if(!member) return message.channel.send("Invalid User");
            member.setNickname(`${memberNick}`   ", "   `${finalNum}`   " PRs");

        } else {
            if(!member) return message.channel.send("Invalid User");
            const nicknameAsArray = memberNick.split(" ");
            var number = nicknameAsArray[nicknameAsArray.length - 2];
            if (
              nicknameAsArray[nicknameAsArray.length - 1] === "PRs" &&
              !isNaN(Number(nicknameAsArray[nicknameAsArray.length - 2]))
            ) {
              nicknameAsArray[nicknameAsArray.length - 2] = Number(number) * multiplyNum;

              member.setNickname(nicknameAsArray.join(" "));
            }
        };
    }
}
  • Related