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

Time:06-12

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

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`)
})

CodePudding user response:

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`)
})

  • Related