【问题标题】:How Do we read the message which is send to our bot by some user?我们如何阅读某个用户发送给我们的机器人的消息?
【发布时间】:2020-01-26 04:16:48
【问题描述】:

我已经设计我的机器人 2-3 周了。我已经通过我可以向任何属于我们服务器成员的人发送消息发出不和谐机器人 DM 命令.. 但是如何阅读他们对该机器人的回复

【问题讨论】:

  • 你能在你提到的上下文中提供一个代码示例吗?

标签: javascript discord.js


【解决方案1】:

使用“消息”事件,您在 DM 中收到的消息与收到普通消息一样。 要查看消息是否在 DM 中发送,请检查 message.guild 是否存在。 例如:

if (!message.guild) {return console.log(`New Message in DMs: ${message.content}`)}

根据您的评论“i want to see that message in a specific channel and with their names”,您必须检查Channel ID。可以通过Message'sAuthor属性获取消息作者姓名。

这是一个例子:

const Discord = require("discord.js");
const Client = new Discord.Client();

Client.on("message", (message) => {
    if (message.author.bot) return false; // If the message is sent by a bot, we ignore it.
    if (message.channel.id == "661567766444376085") { // Checking if the message is sent in a certain channel.
        let Channel = message.client.channels.get("661567766444376085"); // Getting the channel object.
        console.log(`New message in #${Channel.name} from ${message.author.tag}: ${message.content}`);
    };
});

Client.login("TOKEN");

输出应该是:New message in #channel_name from Author#0000: Message Content!

【讨论】:

  • 您能提供任何信息吗?你遇到了什么错误?
  • 对不起,我在其他代码中有错误..我尝试它工作正常
  • 我想在特定频道和他们的名字中看到该消息
  • 好的。我将根据您的评论编辑我的答案。
【解决方案2】:
const Discord = require('discord.js');
const client = new Discord.Client();

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  if (msg.content === 'ping') { //read messages
    msg.reply('pong');
  }
});

client.login('token');

点击here查看官方文档。

【讨论】:

  • 不是这个兄弟,我知道这个 .. 我问的是,当我的机器人向任何人发送 dm 时。如何知道他在 dm 中回复了那个机器人 ..
  • 如果我通过我的机器人直接向你发送消息......你回复了那个机器人我怎么知道你回复了什么?
【解决方案3】:

Discord 没有与用户进行对话的内置功能。为了组织这样的交流,有几种选择。您可以使用channel.fetchMessages 方法,它将对话框中的所有消息作为集合返回,但这不是很方便。您可以创建一个服务器,并在此服务器中为每个用户创建一个频道并将他的消息发送到那里,并将您的消息发送给他在 dm 中。有许多实现选项,但所有这些选项都需要认真研究才能正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-30
    • 2019-05-10
    • 2021-04-09
    • 2017-11-06
    • 2019-08-06
    • 2021-01-19
    • 2019-09-25
    • 2020-06-30
    相关资源
    最近更新 更多