Search code examples
javascriptdiscorddiscord.js

Discord Bot JS - Mail Verification Input


I'm trying to check the input email that the user provides to my discord bot.

What I want to do is to make sure that the format of the email is correct.

-> $email [email protected]

not $email gdfughdfzug

client.on('messageCreate', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift();
    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;


    //recuperation email
     if (command === 'email') {
        if (!args.length) {
           return message.channel.send(`Merci de fournire votre email.\nExemple: [email protected] !`);           
        }
        if(String(args).value.match(mailformat))
        {
            alert("Valid email address!");
            return message.channel.send(`Merci de nous avoir fournie votre email: ${args}`);    
        }
        
    }


});

In my args variable, I get the parameter after the $email.

But when I run the robot and try to see if it works, I get this error

TypeError : Impossible to read the properties of undefined (read 'match')

Solution

  • Well you didn't post how you get the arguments from the message. If it's made the same as most bots then it should be an array of strings. I guess you need to check only the first string argument which in this case is the email.

    Then you can access it by index 0. And check if the mail is valid like this:

    if (args[0].match(mailformat))
        ...