Search code examples
discord.jsunhandled-promise-rejection

(node:44564) UnhandledPromiseRejectionWarning: TypeError: client.on is not a function


I'm currently making a discord bot with a multitude of different commands and after implementing a ?beg and a ?bal command to beg for my imaginary currency called 'bits', it seemed to break a-lot of code. I was able to fix everything except one error, which comes from typing ?verify. When you type ?verify the bot sends an embed to the chat you send ?verify in and asks the member to react to the embed with a tick to give the 'Member' role. Upon typing ?verify and pressing enter the embed appears and the bot also reacts to itself with the tick, although upon reacting the member doesn't get the role. When i looked in the terminal this error showed up,

(node:44564) UnhandledPromiseRejectionWarning: TypeError: client.on is not a function
    at Object.execute (C:\Users\013933\Desktop\Vixe\commands\verify.js:20:16)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(node:44564) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:44564) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Which is odd because client.on is a function that is defined here at the top of the code.

async execute(message, client, args, Discord) {

I've searched it up on stack overflow but it's just people saying that I incorrectly declared 'Client' although when 'correctly' declaring client i just get another error saying 'client' is already defined which it is.

Here is the full code,

module.exports = {
    name: 'verify',
    description: 'Allows members to react to a message to verify themselves.',
    async execute(message, client, args, Discord) {
        const channel = message.channel.id;
        const memberRole = message.guild.roles.cache.find(role => role.name === 'Member');

        const memberEmoji = '✅';
        const { MessageEmbed } = require('discord.js');

        let embed = new MessageEmbed()
            .setColor('#800080')
            .setTitle('Verification')
            .setDescription('React to this embed with the :white_check_mark: to verify yourself and gain access to the server.\n'
                + `Removing your reaction to this embed will un-verify you.`);

        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(memberEmoji);

        client.on('messageReactionAdd', async (reaction, user) => {
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;

            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === memberEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(memberRole);
                }
            } else {
                return;
            }
        });

        client.on('messageReactionRemove', async (reaction, user) => {
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;

            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === memberEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(memberRole);
                }
            } else {
                return;
            }
        });
    }
}

And here is the message.js that handles all the command,

const profileModel = require('../../models/profileSchema');

module.exports = async (Discord, client, message) => {
    const prefix = '?';
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    let profileData;
    try {
        profileData = await profileModel.findOne({ userID: message.author.id });
        if(!profileData) {
            let profile = await profileModel.create({
                userID: message.author.id,
                serverID: message.guild.id,
                bits: 1000,
                bank: 0,
            });
            profile.save();
        }
    } catch (err) {
        console.log(err);
    }

    const args = message.content.slice(prefix.length).split(/ +/);
    const cmd = args.shift().toLowerCase();

    const command = client.commands.get(cmd);

    try {
        command.execute(message, args, cmd, client, Discord, profileData);
    } catch (err) {
        message.reply('There was an error executing this command.');
        console.log(err);
    }
};

Solution

  • This is an easy fix just find this line :

    async execute(message, client, args, Discord)
    

    and change it to

    async execute(message, args, cmd, client, Discord, profileData)
    

    the problem was that in message.js at command.execute(message, args, cmd, client, Discord, profileData);

    the execute parameters where "message, args, cmd, client, Discord, profileData" but at your verfiy file the parameters where "message, client, args, Discord" you didnt add "args, cmd" before the client so the client was defined wrong

    another thing that could mess up your code in the future! Take this line const { MessageEmbed } = require('discord.js'); and put it at the top..