Honoka
Honoka

Reputation: 9

deleting previous message discord bot


I'm trying to making bot that'll send message on last line and delete it after someone send something then send a new one

example:
someone1: hello
bot: hi
someone2: hello
bot: deleting previous message and send new hi

here is my code (still incomplete because I don't know what to do)

  const lastmessage = client.user.lastMessage
  if (lastmessage) { 
    message.channel.send('hi').then(sentMessage => {
     sentMessage.delete({ timeout: 6000 })
  });

Upvotes: 0

Views: 1629

Answers (1)

Elitezen
Elitezen

Reputation: 6710

You can get the member object of the client by using client.me, This will allow you to find the last message sent by the bot using the lastMessage property.

Getting the bot's last message

const { lastMessage } = client.me;

For deleting it once someone else says hello. Make sure you delete the last message before sending the new one, otherwise the newly sent message will be instantly deleted.

Also. The timeout option on Message#delete will soon be deprecated. Use a setTimeout() function instead.

lastMessage.delete();
message.channel.send('hi');

Upvotes: 2

Related Questions