所以让我布置一下场景:(这些不是代码实际所说的,但为了解释的空间,我把它留得很短)
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
我已经尝试了几十种不同的想法来实现这一目标,以便它以“无能为力”的方式做出回应。我有两种可能的解决方案。
代码如下:
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"
}
答案 0 :(得分: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`);
}