#javascript #node.js #discord #discord.js
#javascript #node.js #Discord #discord.js
Вопрос:
Я хочу, чтобы мой бот discord отправлял разные сообщения, когда кто-либо вводит команду в обычном канале или каналах NSFW.
Я следил за документацией, которую я не совсем понял. Я написал команды тестирования ниже:
client.on('message', message => {
if (command === 'testnsfw') {
if (this.nsfw = Boolean(true.nsfw)) {
return message.channel.send('yes NSFW');
} else return message.channel.send('no NSFW');
}
})
Я думаю, что это не работает. Бот отвечает только «нет NSFW» на обоих каналах.
Комментарии:
1. Попробуйте
this.nsfw === Boolean(true.nsfw)
; одинарный=
для назначения, двойной или тройной для тестирования (значение или значение и тип соответственно).2.
message.channel.nsfw
возвращает логическое
Ответ №1:
Вы не можете ссылаться на TextChannel
использование this
в анонимной функции. (Кроме того, this
всегда undefined
находится в функции со стрелкой) Вы можете получить доступ TextChannel
к Message
классу, хранящемуся в message
переменной.
client.on("message", message => {
// Making the sure the author of the message is not a bot.
// Without this line, the code will create an infinite loop of messages, because even if a message is sent by a bot account, the client will emit the message event.
if (message.author.bot) return false;
// message.content is a String, containing the entire content of the message.
// Before checking if it equals to "testnsfw", I would suggest to transform it to lowercase first.
// So "testNSFW", "testnsfw", "TeStNsFw", etc.. will pass the if statement.
if (message.content.toLowerCase() == "testnsfw") {
// You can get the Channel class (which contains the nsfw property) using the Message class.
if (message.channel.nsfw) {
message.channel.send("This channel is NSFW.");
} else {
message.channel.send("This channel is SFW.");
}
}
});
Что я рекомендую вам прочитать:
Ответ №2:
теперь вы можете просто сделать это вместо
if(!message.channel.nsfw) return message.channel.send('a message to send to channel')
это использование
discord.js версия 13.6.0