SlavyanDesu / BocchiBot

BocchiBot is a multipurpose WhatsApp bot using wa-automate-nodejs library!
MIT License
309 stars 408 forks source link

personal error with leveling system #490

Closed fsrastdyyf closed 1 year ago

fsrastdyyf commented 1 year ago

Hi, i was using this template, but when I send a message, the xp system gives an error (TypeError: Cannot read properties of undefined (reading 'getLevelingLevel') at C:\Users\Home\Desktop\dvstBot-main\index.js:81:44 at Client. (C:\Users\Home\Desktop\dvstBot-main\node_modules\@open-wa\wa-automate\dist\api\Client.js:803:92) at Generator.next () at fulfilled (C:\Users\Home\Desktop\dvstBot-main\node_modules\@open-wa\wa-automate\dist\api\Client.js:28:58) at process.processTicksAndRejections (node:internal/process/task_queues:95:5))

I have modified something in the code, and i deleted things, but i can't understand where is the error... 
I put the interested part of the code

Level.js: (that it's the same)
`const fs = require('fs-extra')

/**

const getLevelingLevel = (userId, _dir) => { let pos = null let found = false Object.keys(_dir).forEach((i) => { if (_dir[i].id === userId) { pos = i found = true } }) if (found === false && pos === null) { const obj = { id: userId, xp: 0, level: 1 } _dir.push(obj) fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir)) return 1 } else { return _dir[pos].level } }

/**

/**

/**

/**

// Cooldown XP gains to prevent spam const xpGain = new Set()

/**

/**

module.exports = { getLevelingId, getLevelingLevel, getLevelingXp, addLevelingLevel, addLevelingXp, getUserRank, isGained, addCooldown }`

Index.js: `bot.onMessage(async message => {

    const { id, from, isGroupMsg, chat, caption } = message
    const _leveling = JSON.parse(fs.readFileSync('./database/group/leveling.json'))
    const groupId = isGroupMsg ? chat.groupMetadata.id : ''
    const isLevelingOn = isGroupMsg ? _leveling.includes(groupId) : false
    const { level } = require('./level.js')
    const _level = JSON.parse(fs.readFileSync('./database/user/level.json'))
    let { body } = message

        // Leveling [BETA] by Slavyan
    if (isGroupMsg && isLevelingOn) {
        try {

            const currentLevel = level.getLevelingLevel(message.author, _level)
            const amountXp = Math.floor(Math.random() * (15 - 25 + 1) + 15)
            const requiredXp = 5 * Math.pow(currentLevel, 2) + 50 * currentLevel + 100
            level.addLevelingXp(message.author, amountXp, _level)
            if (requiredXp <= level.getLevelingXp(message.author, _level)) {
                level.addLevelingLevel(message.author, 1, _level)
                const userLevel = level.getLevelingLevel(message.author, _level)
                const fetchXp = 5 * Math.pow(userLevel, 2) + 50 * userLevel + 100
                const pushname = message.sender.pushname;
                await bot.reply(message.from, `*── 「 LEVEL UP 」 ──*\n\n➸ *Name*: ${pushname}\n➸ *XP*: ${level.getLevelingXp(message.author, _level)} / ${fetchXp}\n➸ *Level*: ${currentLevel} -> ${level.getLevelingLevel(message.author, _level)} 🆙 \n\n\n-BocchiBot`, id)
            }
        } catch (err) {
            console.error(err)
        }
    }`

    bot is the session for @open-wa/wa-automate
    sometimes i use the whatsapp-web.js library

    Thanks
github-actions[bot] commented 1 year ago

Hello! Thanks for submiting the issue, we will respond to you ASAP.

SlavyanDesu commented 1 year ago

This can be fixed by copying function folder into your bot folder.

SlavyanDesu commented 1 year ago

The bot directory structure would be like this.

Bot-name
├── function
├─ index.js
rest of directory