Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SyntaxError: await is only valid in async functions and the top level bodies of modules. Discord,js

Error:

SyntaxError: await is only valid in async functions and the top level bodies of modules

[Photo error] https://i.stack.imgur.com/a0hWM.png

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const client = require('../index.js');

client.on('ready', () => {
  let servers = await client.guilds.cache.size
  let servercount = await client.guilds.cache.reduce((a,b) => a+b.memberCount, 0)

  const activites = [
    `?help | ${servers} servers`,
    `Invite me now! | Watching ${servercount} members`
  ]

  setInterval(()=>{
    const status = activities[Math.floor(Math.random()*activities.length)]
    client.user.setPresence({ activities : [{name : `${status}`}]})
  }, 5000)

  
  console.log(`✅ ${client.user.tag} is ready`)
})

>Solution :

You can use await with only async functions. Checkout the below code where I have changed your client.on callback function to async.

const client = require('../index.js');

client.on('ready', async() => {
  let servers = await client.guilds.cache.size
  let servercount = await client.guilds.cache.reduce((a,b) => a+b.memberCount, 0)

  const activites = [
    `?help | ${servers} servers`,
    `Invite me now! | Watching ${servercount} members`
  ]

  setInterval(()=>{
    const status = activities[Math.floor(Math.random()*activities.length)]
    client.user.setPresence({ activities : [{name : `${status}`}]})
  }, 5000)

  
  console.log(`✅ ${client.user.tag} is ready`)
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading