DiscordJS Commando TypeError: Cannot read property 'on' of null
DiscordJS Commando TypeError: Cannot read property 'on' of null
我得到了一个与旧命令处理程序一起使用的票证命令,我编辑了代码以使其与 DiscordJS Commando v12.5 一起使用,它说 TypeError: Cannot read property 'on' of null
,代码中只有一个位置我说 'on'
的地方是反应听众。这是代码:
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 一起使用之前运行良好。
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.
看起来下面的代码没问题,您在从 TicketCommand
的 run
方法调用 registerEvent
时以某种方式将 null
作为 client
传递:run(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
我得到了一个与旧命令处理程序一起使用的票证命令,我编辑了代码以使其与 DiscordJS Commando v12.5 一起使用,它说 TypeError: Cannot read property 'on' of null
,代码中只有一个位置我说 'on'
的地方是反应听众。这是代码:
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 一起使用之前运行良好。
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.
看起来下面的代码没问题,您在从 TicketCommand
的 run
方法调用 registerEvent
时以某种方式将 null
作为 client
传递:run(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