1

My code for a command called ping looks like this:

const { SlashCommandBuilder } = require('@discordjs/builders');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('ping')
        .setDescription('Replies with Pong!'),

    options: [
        {
            name: 'message',
            description: 'Replies with your message!',
            type: 'string',
            required: false,
            default: 'Pong!'
        }
    ],
    async execute(interaction, args) {
        const [ message ] = args;
        return interaction.reply(ping);
    },
};

And currently, when I run this command (and deploy the slash commands first), there is no user enterable parameter, and it throws an error.

6
  • What is args defined as? Commented Jan 10, 2022 at 18:26
  • I don't know actually, I put it there because I thought that is how you parse the arguments 🤔 Commented Jan 10, 2022 at 18:26
  • Log args and see what it is, seems like it's not a string array Commented Jan 10, 2022 at 18:30
  • alright, will try that now Commented Jan 10, 2022 at 18:40
  • it's undefined - I just want to know how to access that parameter - or even set one in the first place Commented Jan 10, 2022 at 18:42

2 Answers 2

1

ik i answering too late, but you can create variable and use it to get content from the input:

const message = interaction.options.getString('message')

getString('message') instead of the message inside brackets you can put whatever the name of the option or command is.

for example:

.addStringOption(option =>
            option 
                .setName('off')
                .setDescription('anti-spam is off')
        )

now i need to get this 'off' inside option, so easy,

const SpamOn = interaction.options.getString('off')
Sign up to request clarification or add additional context in comments.

Comments

0

In order to access that parameter in a slash command, the user must be able to enter a parameter, so when you register the command, you must indicate how many parameters you want to receive and what type.

Documentation on how to add parameters to the commands here.

Documentation on how to read the arguments of a user-entered command here enter link description here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.