0
votes

Sorry, English is not my native language, and I'm new to JS. I'm making my bot discord, I need it to send random messages in the chat in a period of time, this part I've got the code below. however I also need the bot to mention the last user to have sent a message in the chat and I have no idea how to add this to my code

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

2 Answers

0
votes

I need it to send random messages in the chat in a period of time

So many people have already asked this question; please do some research before asking


I also need the bot to mention the last user to have sent a message in the chat

You can use a combination of MessageManager.fetch() and Collection.prototype.first(). First, you have to use MessageManager.fetch() to fetch the last message in the channel. This function returns a collection of messages, of which you only need the first of (as you are getting the first message in the channel).

You can find a list of ways to mention a user from the User/GuildMember object in the first part of this answer

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);
 }
}
0
votes

You can use this to mention the user :

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

and you can use this too :

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