Skip to content

VBots/session-storage

Repository files navigation

SessinStorage Add-on for Session vk-io library

NPM version Used Dependencies

Powered by Lowdb

Usage

NPM

npm i @vbots/session-storage

Yarn

yarn add @vbots/session-storage

Example

const { VK } = require('vk-io');
const { SessionManager } = require('@vk-io/session');
const { SessionStorage } = require('@vbots/session-storage');

const vk = new VK({
    token: process.env.TOKEN
});

async function startBot({ updates }) {
    // 
    const storage = new SessionStorage({ name: 'anotherBot' });
    // Init DB folder & file
    await storage.init();

    const sessionManager = new SessionManager({
        storage,
        // For test
        getStorageKey: (context) => context.userId ? (`${context.userId}_${context.userId}`) : (`${context.peerId}_${context.senderId}`)
    });

    // Init user session storage
    updates.on('message', sessionManager.middleware);

    // ...
    updates.hear('/counter', async (context) => {
        const { session } = context;

        if (!session.counter) {
            session.counter = 0;
        }

        session.counter += 1;

        await context.send(`You turned to the bot (${session.counter}) times`);
    });

    // ...
    updates.start().catch(console.error);
}

// ...
startBot(vk);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published