【问题标题】:Check if bot is only one mentioned检查机器人是否仅被提及
【发布时间】:2021-02-10 01:59:53
【问题描述】:

所以让我把场景布置一下:(这些不是代码实际所说的,但为了解释的篇幅,我把它写得很短)

A 发送此消息:

@Bot slap @membername

机器人回复

@messageauthor slapped @membername

完美执行,代码运行良好。

示例 2

@Bot slap @membername (with any number of args following the second mention) 或者 @bot slap (anything can go here doesn't matter) @membername

机器人回复

@messageauthor no can do

完美执行,代码运行良好

示例 3

@bot slap

机器人回复

@bot slapped @messageauthor because they didn't specify who to slap

完美执行,代码运行良好

示例 4(也是我遇到问题的那个)

@Bot slap membername 没有提及 或者 @Bot slap asdfg

机器人回复

@messageauthor slapped @Bot

我已经尝试了几十种不同的想法来让它发挥作用,以便它会以“不能做”的响应来响应。我有两种可能的解决方案。

  1. 让@bot 像普通前缀一样被解析出来(因为我的前缀不是@,所以不成功,它很愚蠢,没有人会使用,因此发送的所有命令都只与机器人提及一起发送)。
  2. 不知何故使这个(和其他类似的命令)需要第二次提及。
  3. 我也愿意接受其他想法。

代码如下:

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"
}

【问题讨论】:

  • 如果消息是“@Bot 拍你想放在这里的任何东西都无关紧要,只要没有第二次提及”,那么不会发送“不能做”消息,因为args[1] 是真的吗?不确定我是否遗漏了什么。
  • 因为“let user = message.mentions.users.last();”部分,最后提到的用户是机器人,因此“命令”在技术上是有效的,但不需要响应。它接受 slap 命令,然后查找最后提到的用户并找到机器人并说“嘿有效的命令”。
  • 不,我提到您的代码将为消息“@Bot slap any you want to put...”返回“不能做”,因为第二个 if 条件是 @ 987654334@(args[1] 是真实的)。为什么它会返回“@messageauthor slapped @Bot”?
  • 好吧,如果它有那么多 args,我会更正,那么是的,它会失败,所以让我编辑帖子
  • 更正了代码

标签: javascript discord discord.js


【解决方案1】:

解决问题的一个简单方法是确保消息有多次提及,并且第二次提及不是机器人提及。

// We cannot use `message.mentions.users` to get the mentioned users
// since the order of the users returned is not as they appear in the message.
// See https://discord.js.org/#/docs/main/master/class/MessageMentions?scrollTo=users
const mentionedUserIds = message.content
  .match(Discord.MessageMentions.USERS_PATTERN)
  .map(id => id.slice(2, -1));
const [_, userIdToSlap] = mentionedUserIds;

if (!userIdToSlap) {
  return message.channel.send(notargetEmbed);
}

if (userIdToSlap === bot.user.id) {
  return message.channel.send(`<@${message.author.id}>, ` + sarcasm[Math.floor(Math.random() * sarcasm.length)]);
}

整个代码:

const Discord = require("discord.js");
const sarcasm = require("../assets/json/sarcasm.json");

module.exports.run = async (bot, message, args) => {
  const mentionedUserIds = message.content
    .match(Discord.MessageMentions.USERS_PATTERN)
    .map(id => id.slice(2, -1));
  const [_, userIdToSlap] = mentionedUserIds;

  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 (!userIdToSlap) {
    return message.channel.send(notargetEmbed);
  }

  if (userIdToSlap === bot.user.id) {
    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 <@` +
        userIdToSlap +
        `> across the face!`
    )
    .setImage("my_image_gif_here.gif")
    .setFooter("Discord Server Name", "icon_here.png");

  message.channel.send(slapEmbed);
};

注意对于输入"@Bot slap @member for something",上面的代码会打@member。如果您不希望这种行为,您可以添加另一个提前返回。

if (args.length > 1) {
  return message.channel.send(`<@${message.author.id}> Usage: @Bot slap @member`);
}

【讨论】:

  • @Kaspr 不客气。刚刚用评论更新了我的答案,解释了为什么不应该使用message.mentions.users 来获取提到的用户。
  • 这个版本给了我更多“不是函数”的错误,以前的版本没有任何问题
  • @Kaspr 很奇怪。你能分享堆栈跟踪吗?
  • 不确定此链接是否有效,但这是有效的帖子。 stackoverflow.com/revisions/…
  • @Kaspr 刚刚更新了我的答案以解决您提到的问题。可以试试吗?
猜你喜欢
  • 2019-05-11
  • 2020-09-15
  • 2021-07-08
  • 2021-05-02
  • 2018-10-11
  • 2021-07-17
  • 2021-01-08
  • 2021-06-26
  • 2012-11-18
相关资源
最近更新 更多