【问题标题】:JavaScript Discord How can the message be detected on a server and on the channel, on a specific channel?JavaScript Discord 如何在服务器和频道、特定频道上检测到消息?
【发布时间】:2019-04-03 23:40:26
【问题描述】:

我希望机器人检测来自服务器的消息并将其发送到我的个人服务器中的特定频道。我如何制作该代码以及如何将其合并到我的代码中?

我的代码

const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'xdd';

client.login(token); // login the bot with your token.

client.on('message', message => {

        if(message.content.toLowerCase() === 'help')
                message.channel.send("hello " + message.author + "!");
        else if(message.content.toLowerCase().includes('time'))
        {
                var date = new Date();
                message.channel.send('The time is ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds());
        }

});

【问题讨论】:

    标签: javascript discord channel discord.js


    【解决方案1】:

    message 事件将监听所有公会的消息,因此您可以保持原样。您想收听哪些消息完全取决于您。

    确定需要发送的消息后:

    您可以使用 guilds 属性获取服务器:

      var guild = client.guilds.array().find(ch => ch.name === "Server Name");
    

    然后获取该服务器内的频道

      var channel = guild.channels.array().find(ch => ch.name === "Channel Name");
    

    然后使用send 方法将消息发送到该频道。

    channel.send("Hello World!");
    

    希望这会有所帮助!

    DJS 文档:https://discord.js.org/#/

    编辑:修改我的答案以使用 client 而不是 bot,因为我看到这就是您使用的 Client 实例。

    【讨论】:

    • 但我想要的是,当有人发出“!订单”时,该消息会到达我的服务器,这样我就可以去为他服务
    • 此示例假定您让机器人跨多个服务器进行侦听。该机器人将在它加入的公会中侦听消息 (client.on('message')。在您的情况下,当message 事件被触发时,您将寻找包含! Order 的消息。 if 确实如此,然后使用上述步骤将该消息发送到您的服务器,其中上述代码中的 "Server Name" 是您要将消息发送到的服务器,"Channel Name" 是您想要的服务器中的通道要发送的消息。
    猜你喜欢
    • 2021-07-06
    • 2021-10-28
    • 2020-07-06
    • 2020-10-01
    • 2023-03-27
    • 2020-02-25
    • 2020-10-01
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多