如何提及使用Chat Discord.JS发送消息的用户?

时间:2020-10-27 00:39:12

标签: javascript discord discord.js

对不起,英语不是我的母语,并且我是JS的新手。 我正在使我的机器人不和谐,我需要它在一段时间内在聊天中发送随机消息,这部分我下面有代码。 但是我还需要机器人提及聊天中最后一个发送消息的用户 而且我不知道如何将其添加到我的代码中

async function notifLoop(){
  while(true){
    client.channels.cache.get('764266751734054953').send("test text");
    await Sleep(10000)
  }
}

function Sleep(milliseconds) {
    return new Promise(resolve => setTimeout(resolve, milliseconds));
}

client.on('ready', function(){
    notifLoop();
    console.log("Ready");
});

2 个答案:

答案 0 :(得分:0)

我需要它在一段时间内在聊天中发送随机消息

So many people have already asked this questionplease do some research before asking


我还需要机器人提及聊天中最后一个发送消息的用户

您可以结合使用MessageManager.fetch()Collection.prototype.first()。首先,您必须使用MessageManager.fetch()来获取通道中的最后一条消息。此函数返回消息的集合,您只需要其中的第一条消息(因为在通道中获得第一条消息)。

您可以在this answer的第一部分的User / GuildMember对象中找到提及用户的方法列表。

async function notifLoop() {
 const channel = client.channels.cache.get('764266751734054953');
 while (true) {
  const user = channel.messages
   .fetch({ limit: 1 })
   .then((msg) => channel.send(`The user was <@${msg.author.id}>`));
  await Sleep(10000);
 }
}

答案 1 :(得分:0)

您可以使用它来提及用户:

  message.reply(`${message.author}`);

,您也可以使用它:

  message.reply(`<@${message.author.id}>`);