【发布时间】: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
我已经尝试了几十种不同的想法来让它发挥作用,以便它会以“不能做”的响应来响应。我有两种可能的解决方案。
- 让@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"
}
【问题讨论】:
-
如果消息是“@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