John Doe
John Doe

Reputation: 87

TypeError: value.split is not a function

I'm creating a discord.js bot v12 and I get this error on line 2 when I use the purge command in discord and I'm assuming value.split is not a function and wondering if I should be doing something else since I'm using v12 of discord.js:

Uncaught Promise Error: 
TypeError: args.split is not a function or its return value is not iterable
    at Object.module.exports.run (c:\Users\Kazzu\Desktop\src\commands\prune.js:2:34)
    at module.exports (c:\Users\Kazzu\Desktop\src\events\message.js:33:9)
    at Client.emit (events.js:323:22)
    at MessageCreateAction.handle (c:\Users\Kazzu\Desktop\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (c:\Users\Kazzu\Desktop\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (c:\Users\Kazzu\Desktop\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (c:\Users\Kazzu\Desktop\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (c:\Users\Kazzu\Desktop\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (c:\Users\Kazzu\Desktop\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:311:20)

This is my code:

module.exports.run = async (client, message, args) => {
    let [ userId, limit ] = args.split(/\s+/);
        if(!userId && !limit) {
            let deletedMessages = await message.channel.bulkDelete();
            message.channel.send(`${deletedMessages.size} messages were deleted.`);
        }
        if(!userId || !limit) return message.channel.send('Please provide the correct arguments.');
        let r = new RegExp(/^\d+$/);
        if(!r.test(userId)) return message.channel.send('Please provide a valid user id.');
        if(isNaN(limit)) return message.channel.send('Please provide a numeric value for limit');
        if(limit > 100) return message.channel.send('Limit must be less than or equal to 100.');
        try {
            let fetchedMessages = await message.channel.messages.fetch({ limit });
            let filteredMessages = fetchedMessages.filter(message => message.author.id === userId);
            let deletedMessages = await message.channel.bulkDelete(filteredMessages);
            message.channel.send(`${deletedMessages.size} messages were deleted.`);
        }
        catch(err) {
            console.log(err);
        }
}

module.exports.help = {
    name: "purge",
    description: "Deletes a number of messages from a user in a channel."
}

module.exports.requirements = {
    userPerms: [],
    clientPerms: [],
    ownerOnly: false
}

Upvotes: 2

Views: 8272

Answers (2)

Murat Yıldız
Murat Yıldız

Reputation: 12070

It seems that args is not type of string. When you call .split() for something other than a string, JavaScript runtime cannot find the .split() method for that type. So, make sure you are passing a string to your function or try something like that:

if (typeof string === args) {
    var str = args.toString();
}

Upvotes: 3

John Doe
John Doe

Reputation: 87

Turns out I needed to replace value.split to value.slice

Upvotes: 1

Related Questions