Skip to main content

What is Flux?

Discord manages its state with Flux: a central dispatcher broadcasts actions (a message arrives, a channel is selected, the user’s settings change), and stores listen for those actions and hold the resulting state. If you want to know what Discord currently knows (the current user, a guild, the selected channel) you read a store. If you want to know when something happens, you subscribe to the dispatcher. Both come from Metro: stores via findStore, the dispatcher and common stores pre-resolved on metro.common and metro.stores.

Reading stores

A store is a singleton exposing getters over Discord’s state. Find it once (lazily, so you don’t search at import) and read from it whenever you need.
import { metro, utils } from '@unbound-app/api';

const UserStore = utils.lazy(() => metro.findStore('User'));

// later, inside start() or a handler:
const me = UserStore.getCurrentUser();
The stores you’ll reach for most (Users, Guilds, Theme) are already resolved on metro.stores, so you can skip the search entirely.

Subscribing to events

The dispatcher emits a stream of typed actions. Subscribe to one by its type to run code whenever Discord fires it, then unsubscribe when your plugin stops.
import { metro } from '@unbound-app/api';

export default {
	start() {
		this.handler = ({ channelId }) => {
			console.log('Channel selected:', channelId);
		};

		metro.common.Dispatcher.subscribe('CHANNEL_SELECT', this.handler);
	},
	stop() {
		metro.common.Dispatcher.unsubscribe('CHANNEL_SELECT', this.handler);
	},
};
Always unsubscribe in stop with the same handler reference you subscribed with. A subscription that outlives a disabled plugin keeps firing, and keeps your plugin’s code running when it shouldn’t be.

Common action types

Discord dispatches hundreds of action types. A few you’ll see often:
MESSAGE_CREATE, MESSAGE_UPDATE, MESSAGE_DELETE, fired as messages come and go.
CONNECTION_OPEN, fired once the client is connected and stores are populated. A good moment for setup that needs live state.