检测 discord.js 中的 @ 字符或电子邮件

Detect the @ character or emails in discord.js

我希望在写电子邮件时,该成员在 discord 中扮演角色。但是因为我不知道该怎么做,所以我尝试了这个,问题是 if (message.content === "@"){ 只有在我输入 @ 时才有效,我希望它包含 @,我不能'不要使用 If @ in message.content,也不要使用 message.content.startswithcontains

完整代码

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);

如果有人能帮助我,我将不胜感激,我已经阅读了几个小时的不同页面,但我找不到解决方案,或者我只是不知道如何很好地应用它

在 Python 中,您可以检查 in operator.

中是否存在子字符串

在这种情况下,您可以使用以下方法检查“@”:

if "@" in "my.email@example.com":
    print("String contains the @ character")

但是,这不是一个好方法,因为任何带有“@”的字符串都将被视为 E-mail。 检查电子邮件的更好方法是使用正则表达式或 built-in parseaddr(address) 实用程序。

在 javascript 中(如您的示例),您正在寻找的方法是 String.prototype.includes()

示例:

if ("my.email@example.com".includes("@")) {
  // your logic here
}

但是这里我们遇到了之前解释过的相同问题,正则表达式是一个更好的选择(已经回答here

使用.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);