Skip to content

An architecture for managing dynamic messages in discord, inspired by the flux architecture.

License

Notifications You must be signed in to change notification settings

Olian04/ChatFlux

Repository files navigation

ChatFlux

An extended flux implementation designed for usage with chat bots.

Install: npm i chatflux

import { ChatFlux, Reducer, Renderer } from 'ChatFlux';
import { db } from './db';

type CountState = {
  count: number;
}
type CountAction = 'increment' | 'decrement';

const reducer: Reducer<CountState, CountAction> = (state = { count: 0 }, action) => {
  switch (action) {
    case 'increment':
      return ({
        ...state,
        count: state.count + 1,
      });
    case 'decrement':
      return ({
        ...state,
        count: state.count - 1,
      });
    default:
      return state;
  }
};

const renderer: Renderer<CountState, string> = state => `Count: ${state.count}`;

const Counter = new ChatFlux<CountState, CountAction>({
  database: db,
  reduce: reducer,
  render: renderer,
});

app.on('message', async (message) => {
  if (message.content !== '!count') return;
  const msg = message.channel.send('Loading...');
  const body = await Counter.create(msg.id);
  msg.edit(body);

  setInterval(async () => {
    await Counter.dispatch(msg.id, 'increment');
    const newBody = await Counter.render(msg.id);
    msg.edit(newBody);
  }, 3000 /* 3 seconds */);
});

About

An architecture for managing dynamic messages in discord, inspired by the flux architecture.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published