evogram-gramjs 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commands/Accounts.command.d.ts +1 -1
- package/lib/commands/Accounts.command.js +11 -6
- package/lib/sessions/SessionManager.d.ts +6 -1
- package/lib/sessions/SessionManager.js +12 -0
- package/package.json +1 -1
- package/src/commands/Accounts.command.ts +11 -6
- package/src/sessions/SessionManager.ts +21 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Command, CommandContext, InlineQueryContext, MessageContext } from 'evogram';
|
|
2
2
|
export declare class AccountsCommand extends Command {
|
|
3
|
-
execute(context: CommandContext, sessionId?: string): Promise<any>;
|
|
3
|
+
execute(context: CommandContext, sessionId?: string, page?: number): Promise<any>;
|
|
4
4
|
handleMessage(context: CommandContext, message: MessageContext): Promise<any>;
|
|
5
5
|
inlineExecute(context: InlineQueryContext): Promise<void>;
|
|
6
6
|
}
|
|
@@ -7,10 +7,11 @@ const evogram_1 = require("evogram");
|
|
|
7
7
|
const EvogramGramJS_1 = require("../EvogramGramJS");
|
|
8
8
|
const AddAccount_command_1 = require("./AddAccount.command");
|
|
9
9
|
let AccountsCommand = AccountsCommand_1 = class AccountsCommand extends evogram_1.Command {
|
|
10
|
-
execute(context, sessionId) {
|
|
10
|
+
execute(context, sessionId, page) {
|
|
11
11
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
12
12
|
var _a, _b;
|
|
13
|
-
if (!sessionId)
|
|
13
|
+
if (!sessionId) {
|
|
14
|
+
const sessions = yield EvogramGramJS_1.EvogramGramJS.sessionManager.getAllLoadedSessions();
|
|
14
15
|
//@ts-ignore
|
|
15
16
|
return (_a = context[context.callbackQuery ? 'edit' : 'send']) === null || _a === void 0 ? void 0 : _a.call(context, {
|
|
16
17
|
// prettier-ignore
|
|
@@ -20,12 +21,15 @@ let AccountsCommand = AccountsCommand_1 = class AccountsCommand extends evogram_
|
|
|
20
21
|
reply_markup: {
|
|
21
22
|
inline_keyboard: [
|
|
22
23
|
[
|
|
23
|
-
{ text: '
|
|
24
|
-
{ text: '
|
|
24
|
+
{ text: '📋 Список аккаунтов', switch_inline_query_current_chat: ' ', command: AccountsCommand_1 },
|
|
25
|
+
{ text: '➕ Добавить аккаунт', command: AddAccount_command_1.AddAccountCommand },
|
|
25
26
|
],
|
|
27
|
+
...sessions.slice((Number(page) || 0) * 10, (Number(page) || 0) * 10 + 10).map((x) => { var _a, _b, _c; return [{ text: `[${((_a = x.db) === null || _a === void 0 ? void 0 : _a.error) ? '❌' : '✅'}] ${((_b = x.db) === null || _b === void 0 ? void 0 : _b.firstName) || ((_c = x.db) === null || _c === void 0 ? void 0 : _c.username) || 'Безымянный'}`, command: AccountsCommand_1, payload: { sessionId: x.sessionId } }]; }),
|
|
28
|
+
(0, evogram_1.Pagination)({ pages: Math.ceil(sessions.length / 10), currentPage: Number(page) || 0, command: AccountsCommand_1 }),
|
|
26
29
|
],
|
|
27
30
|
},
|
|
28
31
|
});
|
|
32
|
+
}
|
|
29
33
|
const session = EvogramGramJS_1.EvogramGramJS.sessionManager.getSession(sessionId);
|
|
30
34
|
const db = yield (session === null || session === void 0 ? void 0 : session.db());
|
|
31
35
|
const me = yield (session === null || session === void 0 ? void 0 : session.client.getMe().then((x) => x).catch(() => null));
|
|
@@ -75,8 +79,9 @@ let AccountsCommand = AccountsCommand_1 = class AccountsCommand extends evogram_
|
|
|
75
79
|
exports.AccountsCommand = AccountsCommand;
|
|
76
80
|
tslib_1.__decorate([
|
|
77
81
|
tslib_1.__param(1, (0, evogram_1.CommandArgument)('sessionId?')),
|
|
82
|
+
tslib_1.__param(2, (0, evogram_1.CommandArgument)('page?')),
|
|
78
83
|
tslib_1.__metadata("design:type", Function),
|
|
79
|
-
tslib_1.__metadata("design:paramtypes", [evogram_1.CommandContext, String]),
|
|
84
|
+
tslib_1.__metadata("design:paramtypes", [evogram_1.CommandContext, String, Number]),
|
|
80
85
|
tslib_1.__metadata("design:returntype", Promise)
|
|
81
86
|
], AccountsCommand.prototype, "execute", null);
|
|
82
87
|
tslib_1.__decorate([
|
|
@@ -86,5 +91,5 @@ tslib_1.__decorate([
|
|
|
86
91
|
tslib_1.__metadata("design:returntype", Promise)
|
|
87
92
|
], AccountsCommand.prototype, "handleMessage", null);
|
|
88
93
|
exports.AccountsCommand = AccountsCommand = AccountsCommand_1 = tslib_1.__decorate([
|
|
89
|
-
(0, evogram_1.CommandD)({ name: 'accounts' })
|
|
94
|
+
(0, evogram_1.CommandD)({ name: 'accounts', backButton: 'Главное меню' })
|
|
90
95
|
], AccountsCommand);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
|
-
import { TelegramClient } from 'telegram';
|
|
2
|
+
import { Api, TelegramClient } from 'telegram';
|
|
3
|
+
import { Session as SessionEntity } from '../entities/Session.entity';
|
|
3
4
|
import { DatabaseService } from '../services/DatabaseService';
|
|
4
5
|
import { SessionConfig, SessionInfo } from '../types/session.types';
|
|
5
6
|
import { SessionLogger } from './SessionLogger';
|
|
@@ -57,6 +58,10 @@ export declare class SessionManager extends EventEmitter {
|
|
|
57
58
|
* @returns Массив информации о всех сессиях
|
|
58
59
|
*/
|
|
59
60
|
getAllSessions(): SessionInfo[];
|
|
61
|
+
getAllLoadedSessions(): Promise<({
|
|
62
|
+
db: SessionEntity | null;
|
|
63
|
+
user: Api.User | null;
|
|
64
|
+
} & Omit<SessionInfo, 'db' | 'user'>)[]>;
|
|
60
65
|
/**
|
|
61
66
|
* Получает список всех ID сессий
|
|
62
67
|
*
|
|
@@ -157,6 +157,18 @@ class SessionManager extends events_1.EventEmitter {
|
|
|
157
157
|
getAllSessions() {
|
|
158
158
|
return Array.from(this.sessions.values());
|
|
159
159
|
}
|
|
160
|
+
getAllLoadedSessions() {
|
|
161
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
162
|
+
const sessions = Array.from(this.sessions.values());
|
|
163
|
+
const results = yield Promise.all(sessions
|
|
164
|
+
.filter((x) => x.sessionId)
|
|
165
|
+
.map((session) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
166
|
+
return Object.assign(Object.assign({}, session), { db: yield session.db().catch(() => null), user: yield session.user().catch(() => null) });
|
|
167
|
+
})));
|
|
168
|
+
//@ts-ignore
|
|
169
|
+
return results;
|
|
170
|
+
});
|
|
171
|
+
}
|
|
160
172
|
/**
|
|
161
173
|
* Получает список всех ID сессий
|
|
162
174
|
*
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { Command, CommandArgument, CommandContext, CommandD, CommandUpdate, InlineQueryContext, MessageContext } from 'evogram'
|
|
1
|
+
import { Command, CommandArgument, CommandContext, CommandD, CommandUpdate, InlineQueryContext, MessageContext, Pagination } from 'evogram'
|
|
2
2
|
import { EvogramGramJS } from '../EvogramGramJS'
|
|
3
3
|
import { AddAccountCommand } from './AddAccount.command'
|
|
4
4
|
|
|
5
|
-
@CommandD({ name: 'accounts' })
|
|
5
|
+
@CommandD({ name: 'accounts', backButton: 'Главное меню' })
|
|
6
6
|
export class AccountsCommand extends Command {
|
|
7
|
-
public async execute(context: CommandContext, @CommandArgument('sessionId?') sessionId?: string) {
|
|
8
|
-
if (!sessionId)
|
|
7
|
+
public async execute(context: CommandContext, @CommandArgument('sessionId?') sessionId?: string, @CommandArgument('page?') page?: number) {
|
|
8
|
+
if (!sessionId) {
|
|
9
|
+
const sessions = await EvogramGramJS.sessionManager.getAllLoadedSessions()
|
|
10
|
+
|
|
9
11
|
//@ts-ignore
|
|
10
12
|
return context[context.callbackQuery ? 'edit' : 'send']?.({
|
|
11
13
|
// prettier-ignore
|
|
@@ -17,12 +19,15 @@ export class AccountsCommand extends Command {
|
|
|
17
19
|
reply_markup: {
|
|
18
20
|
inline_keyboard: [
|
|
19
21
|
[
|
|
20
|
-
{ text: '
|
|
21
|
-
{ text: '
|
|
22
|
+
{ text: '📋 Список аккаунтов', switch_inline_query_current_chat: ' ', command: AccountsCommand },
|
|
23
|
+
{ text: '➕ Добавить аккаунт', command: AddAccountCommand },
|
|
22
24
|
],
|
|
25
|
+
...sessions.slice((Number(page) || 0) * 10, (Number(page) || 0) * 10 + 10).map((x) => [{ text: `[${x.db?.error ? '❌' : '✅'}] ${x.db?.firstName || x.db?.username || 'Безымянный'}`, command: AccountsCommand, payload: { sessionId: x.sessionId } }]),
|
|
26
|
+
Pagination({ pages: Math.ceil(sessions.length / 10), currentPage: Number(page) || 0, command: AccountsCommand }),
|
|
23
27
|
],
|
|
24
28
|
},
|
|
25
29
|
})
|
|
30
|
+
}
|
|
26
31
|
|
|
27
32
|
const session = EvogramGramJS.sessionManager.getSession(sessionId)
|
|
28
33
|
const db = await session?.db()
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { EventEmitter } from 'events'
|
|
2
|
-
import { TelegramClient } from 'telegram'
|
|
2
|
+
import { Api, TelegramClient } from 'telegram'
|
|
3
3
|
import { UpdateConnectionState } from 'telegram/network'
|
|
4
4
|
import { StringSession } from 'telegram/sessions'
|
|
5
5
|
import { EvogramGramJS } from '../EvogramGramJS'
|
|
6
|
+
import { Session as SessionEntity } from '../entities/Session.entity'
|
|
6
7
|
import { DatabaseService } from '../services/DatabaseService'
|
|
7
8
|
import { SessionConfig, SessionEvent, SessionEventData, SessionInfo, SessionInvalidEventData } from '../types/session.types'
|
|
8
9
|
import { Session } from './Session'
|
|
@@ -161,6 +162,25 @@ export class SessionManager extends EventEmitter {
|
|
|
161
162
|
return Array.from(this.sessions.values())
|
|
162
163
|
}
|
|
163
164
|
|
|
165
|
+
async getAllLoadedSessions(): Promise<({ db: SessionEntity | null; user: Api.User | null } & Omit<SessionInfo, 'db' | 'user'>)[]> {
|
|
166
|
+
const sessions = Array.from(this.sessions.values())
|
|
167
|
+
|
|
168
|
+
const results = await Promise.all(
|
|
169
|
+
sessions
|
|
170
|
+
.filter((x) => x.sessionId)
|
|
171
|
+
.map(async (session) => {
|
|
172
|
+
return {
|
|
173
|
+
...session,
|
|
174
|
+
db: await session.db().catch(() => null),
|
|
175
|
+
user: await session.user().catch(() => null),
|
|
176
|
+
}
|
|
177
|
+
})
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
//@ts-ignore
|
|
181
|
+
return results
|
|
182
|
+
}
|
|
183
|
+
|
|
164
184
|
/**
|
|
165
185
|
* Получает список всех ID сессий
|
|
166
186
|
*
|