[英]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
SyntaxError: await 仅在异步函数和模块的顶层主体中有效
[Photo error] https://i.stack.imgur.com/a0hWM.png
[照片错误] 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`)
})
You can use await with only async functions.您只能将 await 与异步函数一起使用。 Checkout the below code where I have changed your client.on callback function to async.
查看以下代码,我已将您的 client.on 回调函数更改为异步。
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`) })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.