javascriptnode.jsbotsvk

Try to use Users.Get method in vk-io


I need a create command: Ban [@durov] And i got error.

i try deal this:

let uid = `${message.$match[1]}`
let rrr = uid.includes('@')
if(rrr == true){
let realid = uid.replace(/[@]/g, '')
await vk.api.users.get({ user_ids: realid }).then(res => {
    
let id = res[0].id
message.send(`⛔ [id${id}|Пользователь] был забанен.\n📝 Причина: Testing`)
vk.api.messages.removeChatUser({ chat_id: message.chatId, member_id: id, user_id: id })
mongo.models.chatsusers.updateOne({ vkid: Number(id), join_chat: Number(message.chatId), banned: String(false) }, { $set: { banned: true } })
})
}

I got this error message:

TypeError: Cannot read properties of undefined (reading 'id')
    at C:\Users\User\Desktop\Nemo-MongoTest\bot.js:318:18
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async C:\Users\User\Desktop\Nemo-MongoTest\bot.js:316:2
    at async C:\Users\User\Desktop\Nemo-MongoTest\node_modules\middleware-io\lib\index.js:280:9

If i trying this code in eval its working.


Solution

  • Fixed:

    let uid = `${message.$match[1]}`
    const rrr = uid.includes('@')
    if(rrr == true){
    let rud = message.$match[1].replace("@", "")
    await vk.api.utils.resolveScreenName({ screen_name: rud }).then(async (rsersatys) => {
    console.log(rsersatys.object_id)
    const id = await rsersatys.object_id
    message.send(`⛔ [id${id}|Пользователь] был забанен.\n📝 Причина: Testing 4 Re: ${rud}, ${rsersatys.object_id}, id: ${id}`)
    vk.api.messages.removeChatUser({ chat_id: message.chatId, member_id: id, user_id: id })
    mongo.models.chatsusers.updateOne({ vkid: Number(id), join_chat: Number(message.chatId), banned: String(false) }, { $set: { banned: true } })
    })
    }