discord-session-manager
v0.1.0
Published
A discordjs library for managing user/guild/channel/member sessions.
Downloads
4
Readme
discord-session-manager
A discordjs library for managing user/guild/channel/member sessions.
import { Client } from 'discord.js';
import { SessionManager } from 'discord-session-manager';
import { DefaultValues } from 'discord-session-manager/plugins/DefaultValues';
import { FirebaseDB } from 'discord-session-manager-plugin-firebase';
const SM = new SessionManager({
middleware: [
new DefaultValues({
guild: (guild) => ({ id: guild.id }),
user: (user) => ({ id: user.id }),
member: (guildMember, ctx) => ({
user: ctx.getUserSession(guildMember.user),
guild: ctx.getGuildSession(guildMemeber.guild),
}),
channel: (channel) => ({ id: channel.id, type: channel.type }),
}),
new FirebaseDB({
credentials: { /* Firebase credentials */ },
}),
],
});
const client = new Client();
client.on('ready', () => {
client.on('message', (message) => {
// Get the session
const session = SM.getSession(message);
session.user // SM.getUserSession
session.guild // SM.getGuildSession
session.member // SM.getMemeberSession
session.channel // SM.getChannelSession
// Update the session
session.member.sentMessages = (session.member.sentMessages || 0) + 1;
})
});
client.login(/* Discord secret */);
import { Plugin } from 'discord-session-manager';
class MyPlugin extends Plugin {
getUserSession(userSession, user, ctx) {
return userSession;
}
setUserSession(userSession, user, ctx) {
}
getGuildSession(guildSession, guild, ctx) {
return guildSession;
}
setGuildSession(guildSession, guild, ctx) {
}
getMemberSession(memberSession, member, ctx) {
return memberSession;
}
setMemberSession(memberSession, member, ctx) {
}
getChannelSession(channelSession, channel, ctx) {
return channelSession;
}
setChannelSession(channelSession, channel, ctx) {
}
}
interface SessionObject<U extends Object, G extends Object, M extends Object, C extends Object> {
user: U;
guild: G;
member: M;
channel: C;
}
type Target = Message | Guild | User | GuildMember | TextChannel | DMChannel | GroupDMChannel;
class SessionManager<
SB extends SessionObject<U, G, M, C>,
U extends Object = {},
G extends Object = {},
M extends Object = {},
C extends Object = {}> {
getSession(entry: Target): SB {
return {
user: {},
guild: {},
member: {},
channel: {},
} as SB;
};
getUserSession(entry: Target): U {
return {} as U;
};
getGuildSession(entry: Target): G {
return {} as G;
};
getMemeberSession(entry: Target): M {
return {} as M;
};
getChannelSession(entry: Target): C {
return {} as C;
};
}
const SM = new SessionManager<{
user: { id: number },
guild: {},
member: {},
channel: {},
}>();
SM.getSession('').user.id