Héctor Paez
Héctor Paez

Reputation: 13

Detect the @ character or emails in discord.js

I want that when writing an email, the member is given a role in discord. But since I don't know how to do it, I tried this, the issue is that if (message.content === "@"){ only works when I put @, I want it to include @, I couldn't do it with If @ in message.content, nor with neither message.content.startswith nor contains

FULL CODE

const Discord = require("discord.js");
const client = new Discord.Client();
const mySecret = process.env['token']

client.on("ready", () => {
    console.log("ready");
 });
 
client.on("message", message => {
  if(message.channel.id === "963510775215968266"){
    if(message.author.bot) return;
    
    if (message.content === "@"){
      message.member.roles.add("963515178174021642");
      message.author.send("Gracias por verificarte");
      message.delete();
    }
    else{
      message.author.send("¿Tienes problemas? Comunicate con un staff.");
      message.delete();
    }
  }
});

 
 client.login(mySecret);

If someone can give me a hand I would really appreciate it, I've been reading different pages for hours and I can't find the solution, or I just don't know how to apply it well

Upvotes: 0

Views: 213

Answers (2)

Azer
Azer

Reputation: 496

Use .includes()

const Discord = require("discord.js");
const client = new Discord.Client();
const mySecret = process.env['token']

client.on("ready", () => {
    console.log("ready");
 });
 
client.on("message", message => {
  if(message.channel.id === "963510775215968266"){
    if(message.author.bot) return;
    
    if (message.content.includes("@")){
      message.member.roles.add("963515178174021642");
      message.author.send("Gracias por verificarte");
      message.delete();
    }
    else{
      message.author.send("¿Tienes problemas? Comunicate con un staff.");
      message.delete();
    }
  }
});

 
 client.login(mySecret);

Upvotes: 1

TimP4w
TimP4w

Reputation: 16

In Python you can check if a substring is present with the in operator.

In this case you can check for an "@" with:

if "@" in "[email protected]":
    print("String contains the @ character")

However, this is not a good way to do it, as any string with an "@" would be considered an E-mail. A better way to check for an email is to use regular expressions or the built-in parseaddr(address) util.

In javascript (as your example) the method you're looking for is String.prototype.includes().

Example:

if ("[email protected]".includes("@")) {
  // your logic here
}

But here we have the same problem explained previously, and regex is a much better option (as already answered here)

Upvotes: 0

Related Questions