Discord.js timing out members

Issue

I’m using node.js to make a discord bot for my server, I am currently trying to timeout someone on command, e.g. ‘Me-bot.timeout’… Response-‘Bot-Who would you like to timeout?’ then you type there username and it times them out.
I thought this code would work but it doesn’t, how do I fix it/what am I doing wrong?

message.guild.members.cache.get('username#1234').timeout(9000, 'Admin timed you out.')

Solution

Firstly, as byxor mentioned, you should be using message.guild.members.fetch in Discord.js v12 and above.

That being said, fetch is going to return a Promise for a user, and not the user object itself. You need to handle this promise to get the resolved user before attempting to time them out.

This is all explained in the Discord.js documentation, but this is generally what you’re looking for (with all possible errors caught if you need them):

message.guild.members.fetch('id')
  .then(user => {
    user.timeout(9000, 'Admin timed you out.')
    .then(() => {
      console.log('Timed user out for 9000 seconds.')
    })
    .catch(console.error)
  })
  .catch(console.error)

I would highly recommend you read up on Promises, as Discord.js is going to utilize them heavily.

Answered By – iced

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.