I'm using Discord.js V13 (Node JS v16.8.0) and wanted to pass a string as an option
module.exports = {
data: new SlashCommandBuilder()
.setName('punish')
.setDescription('Punish a Guild Member')
.addUserOption(option =>
option.setName('target')
.setDescription('Select a user')
.setRequired(true))
.addStringOption(option =>
option.setName('category')
.setDescription('Choose punishment')
.setRequired(true)
.addChoice('Kick', 'kick')
.addChoice('Ban', 'ban')),
async execute(interaction) {
const member = interaction.options.getMember('target');
const type = interaction.options.getString('type')
await member.type(`Reason: Spam`) //member.ban() / member.kick()
await interaction.reply("Kicked member successfully.")
},
};
I've got the user's selection (category) and would like to execute a punishment in accordance with it.
When I execute member.type() I of course get an error since I can not pass a string as an option, Is there any way I could possibly do this?
I know you can do if (type == 'kick') { //... } if (type == 'ban') { //... } but I think my solution is more convenient and useful
You can access a property of member by its name (and call its methods) like this:
member["propertyName"]();
So in your use case:
member[type]('Reason: Spam');