discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

IntegerOption throw error on 0 and negative number #6851

Closed maxime-lep closed 3 years ago

maxime-lep commented 3 years ago

Issue description

Hello there, I'm encountering kind of a weird error with IntegerOption when I enter 0 or a negativ int:

  1. Discord don't stop me like an string
  2. My code throw me this error:

C:\Users\Maxime\source\repos\Bot-discord.js_V13\node_modules\discord.js\src\rest\RequestHandler.js:298 throw new DiscordAPIError(data, res.status, request); ^

DiscordAPIError: Invalid Form Body limit: int value should be greater than or equal to 1. at RequestHandler.execute (C:\Users\Maxime\source\repos\Bot-discord.js_V13\node_modules\discord.js\src\rest\RequestHandler.js:298:13) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async RequestHandler.push (C:\Users\Maxime\source\repos\Bot-discord.js_V13\node_modules\discord.js\src\rest\RequestHandler.js:50:14) at async MessageManager._fetchMany (C:\Users\Maxime\source\repos\Bot-discord.js_V13\node_modules\discord.js\src\managers\MessageManager.js:219:18) at async TextChannel.bulkDelete (C:\Users\Maxime\source\repos\Bot-discord.js_V13\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:328:20) { method: 'get', path: '/channels/889932783160528896/messages?limit=-1', code: 50035, httpStatus: 400, requestData: { json: undefined, files: [] } }

which is not event catch by my interactionCreate event handler.

I've already send this on your discord and I know that i can check if my option is > 0 but isn't it a good idea to directly integrate this in the lib ?

Also i'm very concerned about the fact that this error is not catch by the guide event handler: https://discordjs.guide/creating-your-bot/command-handling.html#dynamically-executing-commands

### Code sample

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

module.exports = {
    data: new SlashCommandBuilder()
        .setName('clear')
        .setDescription('Delete multiple messages')
        .addIntegerOption(option =>
            option.setName('number')
                .setDescription('The number of messages to delete')
                .setRequired(true)),

    async execute(interaction) {
        const nb = interaction.options.getInteger('number');
        interaction.channel.bulkDelete(nb).then(() => {interaction.reply({ content: (nb < 2) ? `I've deleted ${nb} message` : `I've deleted ${nb} messages`, ephemeral: true });});
    },
};

discord.js version

^13.1.0

Node.js version

v16.9.1

Operating system

Windows

Priority this issue should have

Low (slightly annoying)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

GUILDS, GUILD_MESSAGES

I have tested this issue on a development release

No response

DTrombett commented 3 years ago

Even if discordjs checked if it's greater than 0 it would still throw an error so I don't understand what's the problem

maxime-lep commented 3 years ago

Firstly why not control the user input (like discordjs does by contolling if it's not a string). Secondly why is this error not catch by the event handler ?

vladfrangu commented 3 years ago
  1. It is on you to validate the arguments you receive for them being too big, or small (or negative)
  2. nodejs throws hard errors on unhandled promise rejections, and you are not awaiting your bulkDelete call / attached any error handlers on it, thus your crash.
maxime-lep commented 3 years ago

Ok, ty for you time and answers.

kyranet commented 3 years ago

As a side note, Discord is releasing ranges soon, so you'll be able to define a minimum of 1 (and a maximum of 100) and remove validations from the code:

https://github.com/discord/discord-api-docs/pull/3967