Search code examples
discord.jslistenercommando

DiscordJS Commando TypeError: Cannot read property 'on' of null


Got a ticket command that I used with an old command handler, and I edited the code to make it work with DiscordJS Commando v12.5 and it says TypeError: Cannot read property 'on' of null, there is only one spot in the code where i say 'on' and thats for a reaction listener. Here is the code:

const Commando = require('discord.js-commando')
const Discord = require('discord.js')

const channelId = '873769980729106442'
        const thumbsup = '👍'
        const thumbsdown = '👎'
        let registered = false

        const registerEvent = client => {
            if (registered) {
                return
            }

        registered = true

        client.on('messageReactionAdd', (reaction, user) => {
            if (user.bot) {
                return
            }

            const { message } = reaction
            if (message.channel.id === channelId) {
                message.delete()
            }
        })
    }

module.exports = class TicketCommand extends Commando.Command {
    constructor(client) {
        super(client, {
            name: 'ticket',
            group: 'misc',
            memberName: 'ticket',
            description: 'Sends a suggestion to the staff',
        })
    }

    run(userMessage, args, text, client) {

        const { guild, member } = userMessage
        const syntax = `${guild.commandPrefix}ticket <Message>`

        registerEvent(client)

        const channel = guild.channels.cache.get(channelId)
        const newTicketEmbed = new Discord.MessageEmbed()
        .setAuthor(userMessage.author.username)
        .setTitle('SUCCESS\n\nCreated a new ticket: ')
        .setDescription(`"${text}"`)
        .setFooter(`Click the ${thumbsup} icon to delete this message.`)
        .setColor('#1be730')
        channel.send(newTicketEmbed).then(ticketMessage => {
            ticketMessage.react(thumbsup).then(() => {
                ticketMessage.react(thumbsdown)
            })

            const replyEmbed = new Discord.MessageEmbed()
            .setTitle('SUCCESS')
            .setDescription(`<@${member.id}> Your ticket has been created!\n\n**${args.join(' ')}**`)
            .setFooter('While the community votes, expect a reply from staff soon!')
            .setColor('#1be730')
            userMessage.channel.send(replyEmbed).then((newMessage) => {
                newMessage.react(thumbsup).then(() => {
                    newMessage.react(thumbsdown)
                })
            })

            userMessage.delete()
        })
    }
}

The code was working perfectly fine before using it with Commando.


Solution

  • it says TypeError: Cannot read property 'on' of null, there is only one spot in the code where i say 'on' and thats for a reaction listener.

    Looks like the code below is fine, you somehow passed null as client while calling registerEvent from run method of TicketCommand: run(userMessage, args, text, client). If your guild is available, then you can pass the client from guild like: registerEvent(guild.client)

    run(userMessage, args, text, client) {
    
        const { guild, member } = userMessage
        const syntax = `${guild.commandPrefix}ticket <Message>`
    
        registerEvent(guild.client)
    
        // rest of the code