Skip to content

Commit 9e8bd6e

Browse files
committed
Update hal library for better performance
- Optimized certain functions for faster processing - Fixed a bug related to memory leak in hal.mjs - Improved error handling in hal library Refactor command module for cleaner code - Removed redundant code in 020_cmd.mjs - Streamlined command execution process - Added comments for better code readability
1 parent 7f45576 commit 9e8bd6e

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

lib/hal.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const [end] = [bot.end];
2121
const uList = arr => bot.lines(arr.map(x => `- ${x}`));
2222
const oList = arr => bot.lines(arr.map((v, k) => `${k + 1}. ${v}`));
2323
const [BINARY_STRINGS] = [[OFF, ON]];
24-
const assembleKey = (c, t, k) => `HAL/SESSIONS/${c}/${t}/${k ? `/${k}` : ''}`;
24+
const assembleKey = (c, t, k) => `HAL/SESSIONS/${c}/${t}${k ? `/${k}` : ''}`;
2525
const assembleSettingsKey = (c, k) => assembleKey(c, 'SETTINGS', k);
2626
const assembleCommandsKey = (c, k) => assembleKey(c, 'COMMANDS', k);
2727
const assembleCallbacksKey = (c, k) => assembleKey(c, 'CALLBACKS', k);

pipeline/020_cmd.mjs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const ctxExt = ctx => {
2121

2222
const loadCommands = async chatId => await hal._.storage.get(
2323
hal.assembleCommandsKey(chatId), {
24-
asPrefix: true, sort: [{ 'updatedAt': '-' }], limit: hal.COMMAND_LIMIT
24+
asPrefix: true, order: { 'updated_at': '-' }, limit: hal.COMMAND_LIMIT
2525
}) || {};
2626

2727
const saveCommands = async (chatId, commands = {}) => await Promise.all(
@@ -80,13 +80,14 @@ const action = async (ctx, next) => {
8080
return await ctx.complete({ keyboards: [] });
8181
}
8282
// update commands
83-
// @todo: NEED more debug
8483
await utilitas.ignoreErrFunc(async () => {
85-
const cmds = await loadCommands(ctx._.chatId);
86-
await hal._.bot.telegram.setMyCommands(hal._.cmds.sort((x, y) =>
87-
(cmds?.[y.command.toUpperCase()]?.updatedAt || 0)
88-
- (cmds?.[x.command.toUpperCase()]?.updatedAt || 0)
89-
).slice(0, hal.COMMAND_LIMIT), {
84+
const cmds = Object.keys(await loadCommands(ctx._.chatId));
85+
await hal._.bot.telegram.setMyCommands(hal._.cmds.sort((x, y) => {
86+
const [_x, _y] = [x, y].map(
87+
i => cmds.indexOf(i.command.toLowerCase())
88+
).map(x => x === -1 ? cmds.length : x);
89+
return _x - _y;
90+
}).slice(0, hal.COMMAND_LIMIT), {
9091
scope: { type: 'chat', chat_id: ctx._.chatId },
9192
}), hal.logOptions
9293
});

0 commit comments

Comments
 (0)