【问题标题】:DiscordJS Commando TypeError: Cannot read property 'on' of nullDiscordJS Commando TypeError:无法读取 null 的属性“on”
【发布时间】:2021-11-08 20:19:36
【问题描述】:

获得了一个与旧命令处理程序一起使用的票证命令,我编辑了代码以使其与 DiscordJS Commando v12.5 一起使用,它显示TypeError: Cannot read property 'on' of null,代码中只有一个位置我说@ 987654322@ 这就是反应听众。代码如下:

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()
        })
    }
}

在与 Commando 一起使用之前,该代码运行良好。

【问题讨论】:

    标签: discord.js listener commando


    【解决方案1】:

    上面写着TypeError: Cannot read property 'on' of null,代码中只有一个地方我写着'on',那是一个反应监听器。

    看起来下面的代码没问题,你以某种方式将null 传递为client,同时从runrun 方法调用registerEventrun(userMessage, args, text, client)。如果您的guild 可用,那么您可以从guild 传递客户端,例如: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
    

    【讨论】:

      猜你喜欢
      • 2020-01-29
      • 1970-01-01
      • 2022-01-12
      • 2021-12-04
      • 2021-12-17
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多