I am making a fairly basic Discord bot for my server, its mainly just random responses and some user interaction. I've figured out how to send random messages through a command but I don't know where to start with adding user interaction.
I am looking to allow a user to perform an action using a slash command plus mentioning the user, for example /hug @dean
or something like that, and then have it mention the user and have a little message in an embed that can preferably be random if possible.
This is the gist of what I have so far but its not much and certainly doesn't work, most of what ive tried either doesn't get a response or gives me an error for an invalid string format, probably with the user thing but I can't figure out how to do it properly and the documentation doesn't seem to tell me how or I'm misunderstanding it.
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName("Hug")
.setDescription ('Give a friend a hug!'),
async execute(interaction) {
const embed = new EmbedBuilder()
.setTitle("ProtoOwner throws a stick!")
.setColor(0x0099ff)
.addFields([
{ name: "[{User} hugs {user}!]", value: reply[randomNum] }
]);
await interaction.reply({ embeds: [embed] });
}
}
I have tried a few different ways but still can't manage to get anything to work, if there is a better way to do this rather than using a slash command I'm open to suggestions.
You are on the right track I believe with using SlashCommandBuilder
. The main parts you're missing are telling the command to accept a user as an argument and then retrieving that user from the interaction. You can do this by using .addUserOption()
when building the command, and then interaction.options.getUser()
inside the execute function to get the user who was mentioned.
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName("hug")
.setDescription('Give a friend a hug!')
.addUserOption(option =>
option.setName('target')
.setDescription('The user to hug')
.setRequired(true)), // This makes the option mandatory.
async execute(interaction) {
// This gets the user who was mentioned in the 'target' option.
const target = interaction.options.getUser('target');
// This gets the user who ran the command.
const user = interaction.user;
const embed = new EmbedBuilder()
.setColor(0x0099ff)
.setDescription(`${user} gives ${target} a big hug!`);
await interaction.reply({ embeds: [embed] });
}
};