Safasfly
Safasfly

Reputation: 25

.addArgument is not a function Discord bot

Here is my Discord command module code, for some reason this is spitting out that .addArgument is not a function.

const Discord = require('discord.js');
const { SlashCommandBuilder } = require('@discordjs/builders');

data = new SlashCommandBuilder()
    .setName('purge')
    .setDescription('Purges a number of messages from the channel.')
    .addArgument('number', 'The number of messages to purge.')
    .addArgument('channel', 'The channel to purge messages from.')
    .setExecute(async (interaction, args) => {
        const channel = interaction.guild.channels.cache.find(c => c.name === args.channel);
        if (!channel) return interaction.reply('Channel not found.');
        const messages = await channel.messages.fetch({ limit: args.number });
        await channel.bulkDelete(messages);
        interaction.reply(`Purged ${messages.size} messages.`);
    }

);

It just gives me this:

TypeError: (intermediate value).setName(...).setDescription(...).addArgument is not a function

Upvotes: 1

Views: 94

Answers (1)

Caladan
Caladan

Reputation: 2337

The reason you are getting this error is because there is nothing called .addArgument() in the SlashCommandBuilder. If you want to add arguments like numbers or channels, you need to use .addNumberOption() or .addChannelOption(). The way you will need to use to add a channel and number option is like this:

.addNumberOption(option => {
    return option
        .setName('number') // Set the name of the argument here
        .setDescription('The number of messages to purge.') // Set the description of the argument here
        .setRequired() // This is optional. If you want the option to be filled, you can just pass in true inside the .setRequired() otherwise you can just remove it
})
.addChannelOption(option => {
    return option
        .setName('channel') 
        .setDescription('The channel to purge messages from.')
        .setRequired()
})

You can learn more about slash commands and options here => Options | discord.js

Upvotes: 1

Related Questions