簡體   English   中英

如何提及使用 Chat Discord.JS 發送消息的用戶?

[英]How can I mention a user who sent a message using Chat 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");
});

我需要它在一段時間內在聊天中發送隨機消息

所以, 很多已經這個問題; 詢問之前一些研究


我還需要機器人提及最后一個在聊天中發送消息的用戶

您可以結合使用MessageManager.fetch()Collection.prototype.first() 首先,您必須使用MessageManager.fetch()來獲取通道中的最后一條消息。 此函數返回消息集合,您只需要其中的第一個(因為您正在獲取通道中的第一條消息)。

您可以在本答案的第一部分中找到從 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);
 }
}

您可以使用它來提及用戶:

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

你也可以使用它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM