繁体   English   中英

如何让 discord.js 机器人检查频道是否为 NSFW 并回复?

[英]How to make discord.js bots check if the channel is NSFW and reply?

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

我遵循了文档,但我不太明白。 我在下面写了测试命令:

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”。

您不能在匿名函数中使用this来引用TextChannel (此外, this在箭头函数中始终undefined )您可以使用存储在message变量中的Message类访问TextChannel


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.");
        }
    }
});

我鼓励你阅读的内容:

现在你可以这样做了

if(!message.channel.nsfw) return message.channel.send('a message to send to channel')

这是使用 discord.js 版本 13.6.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM