admin管理员组

文章数量:1391925

I'm trying to get the last message of a specific channel but I've couldn't do that, I want that if i write a mand in another channel (Channel 1) that mand gives me the last message of another channel (channel 2).

My code is:

client.on('message', (message)=>{
    if(message.channel.id === '613553889433747477'){
        if(message.content.startsWith('start')){

                message.channel.fetchMessages({ limit: 1 }).then(messages => {
                    let lastMessage = messages.first();
                    console.log(lastMessage.content);
                  })
                  .catch(console.error);
        }
    }
    });

I'm trying to get the last message of a specific channel but I've couldn't do that, I want that if i write a mand in another channel (Channel 1) that mand gives me the last message of another channel (channel 2).

My code is:

client.on('message', (message)=>{
    if(message.channel.id === '613553889433747477'){
        if(message.content.startsWith('start')){

                message.channel.fetchMessages({ limit: 1 }).then(messages => {
                    let lastMessage = messages.first();
                    console.log(lastMessage.content);
                  })
                  .catch(console.error);
        }
    }
    });
Share Improve this question edited Jan 29, 2020 at 15:26 Victor Escalona asked Aug 23, 2019 at 7:12 Victor EscalonaVictor Escalona 6151 gold badge9 silver badges16 bronze badges 4
  • message.channel.id is the id of channel 2? – Abdulhaq Shah Commented Aug 23, 2019 at 7:34
  • Yes it is. It's the id of the channel 2 – Victor Escalona Commented Aug 23, 2019 at 12:28
  • So its show any error or just stuck? Its passing all your if statements ? – Abdulhaq Shah Commented Aug 23, 2019 at 12:37
  • I'm stuck, in the console it doesn't show me any error – Victor Escalona Commented Aug 23, 2019 at 14:44
Add a ment  | 

1 Answer 1

Reset to default 3

I cleaned up you code a bit, and added some ments explaining what is happening.
If you have more questions, i would remend visiting the official Discord.js Discord server.
https://discord.gg/bRCvFy9

client.on('message', message => {

  // Check if the message was sent in the channel with the specified id.
  // NOTE, this defines the message variable that is going to be used later.
  if(message.channel.id === '613553889433747477'){
    if(message.content.startsWith('start')) {

      // Becuase the message varibable still refers to the mand message,
      // this method will fetch the last message sent in the same channel as the mand message.
      message.channel.fetchMessages({ limit: 1 }).then(messages => {
        const lastMessage = messages.first()
        console.log(lastMessage.content)
      }).catch(err => {
        console.error(err)
      })
    }
  }
})

If you want to get a message from another channel, you can do something like this.
And use the mand start #channel

client.on('message', message => {

  // Check if the message was sent in the channel with the specified id.
  if(message.channel.id === '613553889433747477'){
    if(message.content.startsWith('start')) {

      // Get the channel to fetch the message from.
      const channelToCheck = message.mentions.channels.first()

      // Fetch the last message from the mentioned channel.
      channelToCheck.fetchMessages({ limit: 1 }).then(messages => {
        const lastMessage = messages.first()
        console.log(lastMessage.content)
      }).catch(err => {
        console.error(err)
      })
    }
  }
})

More about mentioning channels in messages can be found here. https://discord.js/#/docs/main/stable/class/Message?scrollTo=mentions

本文标签: javascriptHow to get the last message of a specific channel discordjsStack Overflow