npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@discordjs-mvc/session

v1.0.5

Published

Session plugin for discordjs-mvc

Downloads

14

Readme

Session Plugin for DiscordJs Mvc

Installation

npm install @discordjs-mvc/session
# or
yarn add @discordjs-mvc/session

Usage

You need to extend your Context type with the SessionFlavor type from the plugin.

import { Base } from 'discord.js';
import { SessionFlavor } from '@discordjs-mvc/session';
import { BaseContext } from 'discord.js-mvc';

export type Context<T extends Base> = BaseContext<T> & SessionFlavor;

Registering the plugin

import { SessionPlugin, InMemoryStorage } from '@discordjs-mvc/session';
import { Router } from 'discord.js-mvc';

export const router = new Router()
    .plugin(
        SessionPlugin(
            // Pass the session store here, this is required
            new InMemoryStore(),
            // You can pass initial data to the session store here, this is optional
            { foo: 'bar' }
        )
    )
    .middleware(
        // Your Middleware
    )
    .routes(
        // Your Routes
    );

Using the session

// controllers/ping.controller.ts
import {ChatInputCommandInteraction} from 'discord.js';
import { Context } from './context';
import { Controller } from 'discord.js-mvc';

export const pingController: Controller<Context<ChatInputCommandInteraction>> = async (ctx) => {
    // check if user already used the command
    const used = await ctx.session.get('ponged');
    if (used) {
        return await ctx.reply('You already used this command');
    }
    // if not, set the session value to true
    await ctx.session.set('ponged', true);
    // reply with pong
    return await ctx.reply('pong');
};

Session Storage

The plugin comes with two session storage implementations, InMemoryStorage and FileStorage. Both storages are not recommended for production use : the InMemoryStorage is not persistent and the FileStorage is not optimized as it store the whole session data in a single file.

InMemoryStore

The InMemoryStorage is the easiest to use, it stores the session data in memory, but it is not persistent, so the session data will be lost when the bot restarts. The implementation is described in the previous section.

DO NOT USE THIS IN PRODUCTION

FileStorage

The FileStorage stores the session data in a file, it is persistent but not optimized. It's the same implementation as the InMemoryStorage but it uses the fs module to store the data in a file so it is persistent.

The FileStorage constructor takes a path to the file as first argument and an optional writeIntervalDelay, this is the delay between each write to the file, by default it is set to 60 000 (1 minute).

import { SessionPlugin, FileStorage } from '@discordjs-mvc/session';
import { Router } from 'discord.js-mvc';

export const router = new Router()
    .plugin(
        SessionPlugin(
            // Pass the session store here, this is required
            new FileStorage('./session.json', 60_000),
            // You can pass initial data to the session store here, this is optional
            { foo: 'bar' }
        )
    )
    .middleware(
        // Your Middleware
    )
    .routes(
        // Your Routes
    );

Create your own storage

You can create your own storage by implementing the Storage interface.

interface Storage {
    get(key?: string): any | Promise<any>;
    set(key: string, value: any): void | Promise<void>;
    delete(key: string): void | Promise<void>;
}

So you can create a storage that uses a database or a cache system. All you need is to create a class that has the get, set and delete methods.

import { Storage } from '@discordjs-mvc/session';

export class MyStorage implements Storage {
    
    public async get(key?: string): Promise<any> {
        // get the value from the database
    }

    public async set(key: string, value: any): Promise<void> {
        // set the value in the database
    }

    public async delete(key: string): Promise<void> {
        // delete the value from the database
    }
}