Javascript 检查bot是否只是提到的一个

Javascript 检查bot是否只是提到的一个,javascript,discord,discord.js,Javascript,Discord,Discord.js,让我来安排一下场景:(这些并不是代码实际所说的,但是为了在解释中留出空间,我把它留了下来) 人员A发送此消息: @Bot slap@membername Bot回复为 @messageauthor打了@membername一巴掌 完美的执行,并且代码运行良好 例2 @Bot slap@membername(在第二次提及后有任意数量的参数) 或 @bot slap(任何东西都可以放在这里都没关系)@membername Bot回复为 @messageauthor no can do 完美的执行,并

让我来安排一下场景:(这些并不是代码实际所说的,但是为了在解释中留出空间,我把它留了下来)

人员A发送此消息:

@Bot slap@membername

Bot回复为

@messageauthor打了@membername一巴掌

完美的执行,并且代码运行良好

例2

@Bot slap@membername(在第二次提及后有任意数量的参数)
@bot slap(任何东西都可以放在这里都没关系)@membername

Bot回复为

@messageauthor no can do

完美的执行,并且代码运行良好

例3

@bot slap

Bot回复为

@bot打了@messageauthor一巴掌,因为他们没有指定打谁

完美的执行,并且代码运行良好

例4(和我遇到麻烦的那个)

@Bot slap membername
未提及 或
@Bot slap asdfg

Bot回复为

@messageauthor打了@Bot一巴掌

我尝试了几十种不同的想法来让它工作,这样它就会以“不行”的回应来回应。有两种可能的解决方案,我是在追求

  • 像解析普通前缀一样解析@bot(由于我的前缀不是@,所以没有成功,这是一个愚蠢的长前缀,没有人会使用它,因此发送的所有命令都只包含bot引用)
  • 不知何故,这(和其他命令类似)需要再次提及
  • 我对其他想法也持开放态度
  • 代码如下:

    const Discord = require('discord.js')
    const sarcasm = require('../assets/json/sarcasm.json'); //has variations of no can do but with major sarcasm in it
    
    module.exports.run = async (bot, message, args) => {
        let user = message.mentions.users.last();
    
        const notargetEmbed = new Discord.MessageEmbed()
            .setColor('#ff6600')
            .setTitle('Slap')
            .setDescription(`<@` + bot.user.id + `> slapped <@` + message.author.id + `> because they didn't specify who to slap, so it boomeranged.`)
            .setImage('my_image_gif_here.gif')
            .setFooter('Discord Server Name', 'icon_here.png')
    
        if (!args.length) {
            return message.channel.send(notargetEmbed);
        }
    
        if (args[1]) {
            return message.channel.send(`<@${message.author.id}>, ` + sarcasm[Math.floor(Math.random() * sarcasm.length)]);
        }
    
        const slapEmbed = new Discord.MessageEmbed()
            .setColor('#ff6600')
            .setTitle('Slap')
            .setDescription(`<@` + message.author.id + `> slapped <@` + user.id + `> across the face!`)
            .setImage('my_image_gif_here.gif')
            .setFooter('Discord Server Name', 'icon_here.png')
    
        message.channel.send(slapEmbed);
    };
    
    module.exports.help = {
        name: "slap"
    }
    
    const Discord=require('Discord.js')
    const sarcasm=require('../assets/json/sarcasm.json')//它有很多变体,只是带有强烈的讽刺意味
    module.exports.run=async(bot、消息、args)=>{
    let user=message.indications.users.last();
    const notargetEmbed=new Discord.MessageEmbed()
    .setColor(“#ff6600”)
    .setTitle('Slap')
    .setDescription(`slapped,因为他们没有指定要打谁,所以它回飞镖了。`)
    .setImage('my\u image\u gif\u here.gif')
    .setFooter('Discord服务器名称','icon_here.png')
    如果(!参数长度){
    返回message.channel.send(notargetEmbed);
    }
    if(args[1]){
    返回message.channel.send(`,`+sarcasm[Math.floor(Math.random()*sarcasm.length)];
    }
    const slapEmbed=new Discord.MessageEmbed()
    .setColor(“#ff6600”)
    .setTitle('Slap')
    .setDescription(`slapsed-cross-the-face!`)
    .setImage('my\u image\u gif\u here.gif')
    .setFooter('Discord服务器名称','icon_here.png')
    message.channel.send(slapEmbed);
    };
    module.exports.help={
    名字:“拍打”
    }
    
    解决问题的一个简单方法是确保消息中有几处提到,第二处提到的内容不是机器人提到的内容

    //我们不能使用`message.indications.users`来获取提到的用户
    //因为返回的用户顺序与消息中显示的不同。
    //看https://discord.js.org/#/docs/main/master/class/MessageMentions?scrollTo=users
    const-referenceduserids=message.content
    .match(Discord.messagenotices.USERS\u模式)
    .map(id=>id.slice(2,-1));
    const[\uu,userIdToSlap]=提及的序列号;
    如果(!userIdToSlap){
    返回message.channel.send(notargetEmbed);
    }
    if(userIdToSlap==bot.user.id){
    返回message.channel.send(`,`+sarcasm[Math.floor(Math.random()*sarcasm.length)];
    }
    
    整个代码:

    const Discord=require(“Discord.js”);
    const sarcasm=require(“../assets/json/sarcasm.json”);
    module.exports.run=async(bot、消息、args)=>{
    const-referenceduserids=message.content
    .match(Discord.messagenotices.USERS\u模式)
    .map(id=>id.slice(2,-1));
    const[\uu,userIdToSlap]=提及的序列号;
    const notargetEmbed=new Discord.MessageEmbed()
    .setColor(“#ff6600”)
    .setTitle(“Slap”)
    .setDescription(
    `打耳光是因为他们没有指定要打谁,所以这是自食其果`
    )
    .setImage(“my_image\u gif\u here.gif”)
    .setFooter(“Discord服务器名称”,“icon_here.png”);
    如果(!userIdToSlap){
    返回message.channel.send(notargetEmbed);
    }
    if(userIdToSlap==bot.user.id){
    返回message.channel.send(
    `, ` +
    sarcasm[Math.floor(Math.random()*sarcasm.length)]
    );
    }
    const slapEmbed=new Discord.MessageEmbed()
    .setColor(“#ff6600”)
    .setTitle(“Slap”)
    .setDescription(
    `打在脸上`
    )
    .setImage(“my_image\u gif\u here.gif”)
    .setFooter(“Discord服务器名称”,“icon_here.png”);
    message.channel.send(slapEmbed);
    };
    
    注意,对于输入
    “@Bot slap@member for something”
    ,上面的代码将slap@member。如果您不希望出现这种行为,您可以添加另一个提前退货

    if(args.length>1){
    返回message.channel.send(`Usage:@Bot slap@member`);
    }
    
    如果消息是“@Bot slap,只要没有第二次提及,你想在这里放什么都不重要”,那么“不行”消息不会被发送,因为
    args[1]
    是真实的吗?不确定我是否遗漏了什么。由于“let user=message.indications.users.last();”部分,最后提到的用户是机器人,因此“命令”在技术上是有效的,但不需要响应。它接受slap命令,然后查找最后提到的用户,找到bot并说“hey valid command”。不,我提到的是,您的代码将为消息“@bot slap which you want put…”返回“No can do”,因为第二个
    if
    条件将是
    true
    参数[1]
    是真实的)。为什么它会返回“@messageauthor slapped@Bot”?好吧,如果它有那么多参数,我会更正的,那么是的,它会失败,所以请让我编辑后更正代码,谢谢you@Kaspr不客气。刚刚更新了我的答案,并解释了原因