最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parse Arguments from Discord interactions - Stack Overflow

programmeradmin6浏览0评论

I am currently coding a discord bot and wanted to play around with the new interaction feature (aka slash-mands).

I managed to get a "/test" Interaction working, and the bot receives the mand and executes my code for it (following discord.js manual). Now I know from interactionmandName what name for example the mand has (in this case "test"), but I don’t seem to be able to get the arguments that are passed in.

Following is my "test.js", which is for the "/test" mand:

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

module.exports = {
    data: new SlashCommandBuilder()
        .setName('test')
        .setDescription('Test with arguments!')
    .addStringOption(option =>
      option.setName("device")
        .setDescription("Some argument")
        .setRequired(true)
        .addChoice("Arg A", "A")
        .addChoice("Arg B", "B")
    ),
    async execute(interaction, log) {
        // log is a function to log to a file...
        // Ignore it, since it is not *currently* in use.
 
        return interaction.reply("You selected the argument ????")
    
        // Delete message after 5 seconds
        .then(setTimeout(() => interaction.deleteReply(), 5000));
    },
};

The mand itself gets called by a function that is also from the documentation of discord.js:

client.on('interactionCreate', async(interaction) => {
  if (!interaction.isCommand()) return;

    const mand = clientmands.get(interactionmandName);

    if (!mand) return;

    try {
        await mand.execute(interaction, log);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this mand!', ephemeral: true });
    }
});

I looked through the documentation and searched quite a bit online, but I don’t seem to find anything. If anyone could help me that would be great.

I am currently coding a discord bot and wanted to play around with the new interaction feature (aka slash-mands).

I managed to get a "/test" Interaction working, and the bot receives the mand and executes my code for it (following discord.js manual). Now I know from interaction.mandName what name for example the mand has (in this case "test"), but I don’t seem to be able to get the arguments that are passed in.

Following is my "test.js", which is for the "/test" mand:

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

module.exports = {
    data: new SlashCommandBuilder()
        .setName('test')
        .setDescription('Test with arguments!')
    .addStringOption(option =>
      option.setName("device")
        .setDescription("Some argument")
        .setRequired(true)
        .addChoice("Arg A", "A")
        .addChoice("Arg B", "B")
    ),
    async execute(interaction, log) {
        // log is a function to log to a file...
        // Ignore it, since it is not *currently* in use.
 
        return interaction.reply("You selected the argument ????")
    
        // Delete message after 5 seconds
        .then(setTimeout(() => interaction.deleteReply(), 5000));
    },
};

The mand itself gets called by a function that is also from the documentation of discord.js:

client.on('interactionCreate', async(interaction) => {
  if (!interaction.isCommand()) return;

    const mand = client.mands.get(interaction.mandName);

    if (!mand) return;

    try {
        await mand.execute(interaction, log);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this mand!', ephemeral: true });
    }
});

I looked through the documentation and searched quite a bit online, but I don’t seem to find anything. If anyone could help me that would be great.

Share Improve this question edited Aug 29, 2021 at 9:46 MrMythical 9,0393 gold badges21 silver badges48 bronze badges asked Aug 29, 2021 at 0:49 PlayWolfYTPlayWolfYT 1491 gold badge3 silver badges10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Warning: I did not test this code CommandInteractions have an options property. You can use the get method on it.

//the option name is 'TEST' and the user inputted 'hey'
let option = interaction.options.get("TEST")
console.log(option.value)

.value shows the input as shown here. It can be empty if there is no input (option was not required and user did not input that option)

发布评论

评论列表(0)

  1. 暂无评论