I have a bot that's trying to kick a mentioned user in the slash command but when I mention a user in the "user" option it says User not found
and gives an error of x is not a valid function
(x is whatever I'm trying). I'm trying to find a valid function to actually define the user mentioned but can't seem to find one. Any help is appreciated since I am new to javascript and discord.js
const { SlashCommandBuilder, OptionType } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('tkick')
.setDescription('Kicks the specified user by tag (Moderation)')
.addMentionableOption((option) =>
option
.setName('user')
.setDescription('The user to be kicked')
.setRequired(true),
),
async execute(interaction, client) {
if (!interaction.member.permissions.has(['KICK_MEMBERS']))
return interaction.reply(
'You do not have permission to use this command.',
);
// Get the user from the interaction options
const user = interaction.user;
if (!user) return interaction.reply('User not found.');
// Kick the user
await interaction.option.('user').kick(); // This is where I am having the issue
// Reply with the text
await interaction.reply({
content: 'User has been kicked.',
});
},
};
I tried looking at different questions I looked up but it's either in an older version where it isn't there or got removed. Discord.js v13 got released in December 2022 but I can only find posts before that.
CodePudding user response:
interaction.option.('user').kick()
is not a valid syntax and interaction.user
is not a GuildMember
, but a User
object. To be able to kick someone, you need a GuildMember
object which can be obtained by using the interaction.options.getMentionable
method if you use addMentionableOption
.
However, it accepts roles and users. If you don't want your bot to accept mentions of roles, which would make your life a bit more difficult, it's better to use the addUserOption
instead of addMentionableOption
. With addUserOption
, you can get the GuildMember
object by using the interaction.options.getMember
method:
module.exports = {
data: new SlashCommandBuilder()
.setName('tkick')
.setDescription('Kicks the specified user by tag (Moderation)')
. addUserOption((option) =>
option
.setName('user')
.setDescription('The user to be kicked')
.setRequired(true),
),
async execute(interaction) {
if (!interaction.member.permissions.has(['KICK_MEMBERS']))
return interaction.reply(
'You do not have permission to use this command.',
);
try {
await interaction.options.getMember('user').kick();
interaction.reply('User has been kicked.');
} catch (err) {
console.error(error);
interaction.reply('⚠️ There was an error kicking the member');
}
},
};