-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.js
More file actions
116 lines (112 loc) · 2.9 KB
/
session.js
File metadata and controls
116 lines (112 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// DiscordBot session middleware and stores
import fs from 'fs'
/**
* In-memory session store for DiscordBot.
* @class
*/
class MemorySessionStore {
/**
* Create a new MemorySessionStore.
*/
constructor() {
this.sessions = {}
}
/**
* Get session data for a user.
* @param {string} id - User ID.
* @returns {Promise<object>} Session object.
*/
async get(id) {
return this.sessions[id] || {}
}
/**
* Set session data for a user.
* @param {string} id - User ID.
* @param {object} session - Session object.
* @returns {Promise<void>}
*/
async set(id, session) {
this.sessions[id] = session
}
/**
* Clear session data for a user.
* @param {string} id - User ID.
* @returns {Promise<void>}
*/
async clear(id) {
delete this.sessions[id]
}
}
/**
* File-based session store for DiscordBot.
* @class
*/
class FileSessionStore {
/**
* Create a new FileSessionStore.
* @param {string} [filePath='sessions.json'] - Path to session file.
*/
constructor(filePath = 'sessions.json') {
this.filePath = filePath
if (!fs.existsSync(this.filePath)) fs.writeFileSync(this.filePath, '{}')
}
_read() {
return JSON.parse(fs.readFileSync(this.filePath, 'utf8'))
}
_write(sessions) {
fs.writeFileSync(this.filePath, JSON.stringify(sessions, null, 2))
}
/**
* Get session data for a user.
* @param {string} id - User ID.
* @returns {Promise<object>} Session object.
*/
async get(id) {
const sessions = this._read()
return sessions[id] || {}
}
/**
* Set session data for a user.
* @param {string} id - User ID.
* @param {object} session - Session object.
* @returns {Promise<void>}
*/
async set(id, session) {
const sessions = this._read()
sessions[id] = session
this._write(sessions)
}
/**
* Clear session data for a user.
* @param {string} id - User ID.
* @returns {Promise<void>}
*/
async clear(id) {
const sessions = this._read()
delete sessions[id]
this._write(sessions)
}
}
/**
* Session middleware for DiscordBot. Attaches session to context.
* @param {object} [options] - Options for session store.
* @param {string} [options.type='memory'] - Store type ('memory' or 'file').
* @param {string} [options.filePath='sessions.json'] - Path to session file.
* @returns {function(Context, function):Promise<void>} Middleware function.
*/
export function session(options = {}) {
const { type = 'memory', filePath = 'sessions.json' } = options
const store =
type === 'file' ? new FileSessionStore(filePath) : new MemorySessionStore()
return async (ctx, next) => {
const chatId = ctx.chat?.id || ctx.from?.id || 'default'
ctx.session = await store.get(chatId)
await next()
await store.set(chatId, ctx.session)
}
}
/**
* Session store classes for DiscordBot.
* @type {FileSessionStore}
*/
export { FileSessionStore, MemorySessionStore }