izy521 / discord.io

A small, single-file library for creating DiscordApp clients from Node.js or the browser
https://discord.gg/0MvHMfHcTKVVmIGP
MIT License
535 stars 155 forks source link

'limit' input property on getMessages does not appear to work. #264

Closed tulser closed 6 years ago

tulser commented 6 years ago

Hi, I'm rather novice at Javascript, but I'm having an issue where I'm trying to get the bot to get and then delete certain number of messages before the trigger message. So far the code below gets messages, but when I debug, getMessages does not appear to return an array with the correct number of terms but rather returns all the messages before the trigger message, which leads me to believe the 'limit' input may not work.

if (cmd == 'deletesome') {
            var dellim = args[0];
            if (dellim < 101) {
                logger.info(event.d.id);
                logger.info(dellim); //Log limit
                bot.getMessages({
                    channelID: channelID,
                    limit: dellim,
                    before: event.d.id,
                }, function(error, messageArray) {
                    logger.info(messageArray.map(a => a.id));
                    bot.deleteMessages({
                        channelID: channelID,
                        messageIDs: messageArray.map(a => a.id),
                })
                })
                bot.sendMessage({
                    to: channelID,
                    message: 'Done!'
                })
cloudrac3r commented 6 years ago

Despite dellim containing only numbers, it is extracted from a string and is therefore also a string. options.limit only accepts numbers, so you must convert it to a number before using it.

You can convert dellim to a number in several ways: +dellim, Number(dellim), parseInt(dellim) and parseFloat(dellim) will all work. Pick your favourite, and either assign the result to a variable or use it directly in .getMessages.

By the way, fix your indentation. Please. I cannot understand how people code like this. And tabs too? Oof.

Don't forget to join the discord.io server so you can get help there in future rather than posting GitHub issues. https://discord.gg/0MvHMfHcTKVVmIGP

tulser commented 6 years ago

Ah, okay, I initially I thought it had set it up to be a number but I guess not. Thanks!