Reputation: 1616
So lemme lay out the scene: (these aren't what the code actually says but for the sake of space in the explanation, I left it short)
Person A sends this message:
@Bot slap @membername
Bot replies with
@messageauthor slapped @membername
Perfect execution and that code works just fine.
example 2
@Bot slap @membername (with any number of args following the second mention)
or
@bot slap (anything can go here doesn't matter) @membername
Bot replies with
@messageauthor no can do
Perfect execution and that code works just fine
example 3
@bot slap
Bot replies with
@bot slapped @messageauthor because they didn't specify who to slap
Perfect execution and that code works just fine
example 4 (and the one I'm having trouble with)
@Bot slap membername
with no mention
or
@Bot slap asdfg
Bot replies with
@messageauthor slapped @Bot
I have tried several dozens of different ideas to get this working so that it will respond with the "no can do" response. There are 2 possible solutions that I am after.
Here is the code:
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"
}
Upvotes: 1
Views: 164
Reputation: 11915
A simple way to solve the issue is to make sure the message has a couple of mentions and that the second mention is not a bot mention.
// 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)]);
}
The whole code:
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);
};
Note that for the input "@Bot slap @member for something"
, the above code will slap @member. If you don't want that behavior, you can add another early return.
if (args.length > 1) {
return message.channel.send(`<@${message.author.id}> Usage: @Bot slap @member`);
}
Upvotes: 1