【问题标题】:How to make discord.js bots check if the channel is NSFW and reply?如何让 discord.js 机器人检查频道是否为 NSFW 并回复?
【发布时间】:2021-01-08 04:29:47
【问题描述】:

当有人在普通频道或 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”。

【问题讨论】:

  • 试试this.nsfw === Boolean(true.nsfw);单 = 用于赋值,双或三用于测试(分别为值或值和类型)。
  • message.channel.nsfw 返回一个布尔值

标签: javascript node.js discord discord.js


【解决方案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.");
        }
    }
});

我鼓励你阅读:

【讨论】:

    猜你喜欢
    • 2021-10-14
    • 2022-12-16
    • 2021-05-13
    • 2021-04-13
    • 2021-08-09
    • 2021-08-18
    • 2023-02-20
    • 2019-03-18
    • 2021-12-13
    相关资源
    最近更新 更多