如何让Discord.js漫游器检查频道是否为NSFW并回复?

时间:2020-09-22 04:28:35

标签: javascript node.js discord discord.js

当任何人在普通频道或NSFW频道中键入命令时,我想让不和谐的bot发送不同的消息。

我遵循了文档,但并没有完全理解。我在下面编写了测试命令:

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');
    }
})

我认为它不起作用。该漫游器仅在两个通道上都响应“ no NSFW”。

1 个答案:

答案 0 :(得分:1)

您不能在匿名函数中使用this来引用TextChannel。 (此外,箭头功能中this始终是undefined。您可以使用存储在message变量中的TextChannel类来访问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.");
        }
    }
});

我鼓励您阅读的内容: