From b26761dba2daa34f3042c2ce8905876d0df46c0e Mon Sep 17 00:00:00 2001 From: xleat Date: Fri, 1 Nov 2024 23:54:03 +0800 Subject: [PATCH] =?UTF-8?q?Modify=EF=BC=9A=20=E6=B7=BB=E5=8A=A0=E5=85=81?= =?UTF-8?q?=E8=AE=B8=E8=BD=AC=E5=8F=91=E7=9A=84=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/TelegramBotClient.ts | 2 +- src/i18n/locales/en.ts | 6 ++++++ src/i18n/locales/zh.ts | 6 ++++++ src/service/BindItemService.ts | 30 ++++++++++++++++++------------ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/client/TelegramBotClient.ts b/src/client/TelegramBotClient.ts index edc25f3..49ddad7 100644 --- a/src/client/TelegramBotClient.ts +++ b/src/client/TelegramBotClient.ts @@ -704,7 +704,7 @@ export class TelegramBotClient extends BaseClient { } // 在bot的聊天使用添加到全部的群组 if (ctx.chat.id === this._chatId) { - this.bindItemService.addAllowEntityByChat(null, allows).then(() => { + this.bindItemService.addAllowEntityByChat(-1, allows).then(() => { ctx.reply(this.t('command.aad.success')) }).catch(() => { ctx.reply(this.t('command.aad.fail')) diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts index a20154d..408fe1b 100644 --- a/src/i18n/locales/en.ts +++ b/src/i18n/locales/en.ts @@ -1,5 +1,11 @@ const en = { command: { + aad: { + success: 'add successfully', + fail: 'add failed', + exist: 'exists', + noUser: 'there is no user', + }, description: { help: 'Help', start: 'Start', diff --git a/src/i18n/locales/zh.ts b/src/i18n/locales/zh.ts index 140cc45..3c23205 100644 --- a/src/i18n/locales/zh.ts +++ b/src/i18n/locales/zh.ts @@ -1,5 +1,11 @@ const zh = { command: { + aad: { + success: '添加成功', + fail: '添加失败', + exist: '已存在', + noUser: '没有用户', + }, description: { help: '使用说明', start: '开始', diff --git a/src/service/BindItemService.ts b/src/service/BindItemService.ts index 7e2ef34..d8e0dc8 100644 --- a/src/service/BindItemService.ts +++ b/src/service/BindItemService.ts @@ -39,14 +39,17 @@ export class BindItemService extends AbstractSqlService { /** * 更新 chatId 的 allow_entities !!注意没有 chatId 会更新所有的 - * @param chatId 群聊id + * @param chatId 群聊id -1 为更新所有 * @param allows */ public async addAllowEntityByChat(chatId: number, allows: string[]) { this.db.serialize(() => { // 查询已经存在的 allow_entities this.db.get('SELECT json_array(allow_entities) FROM tb_bind_item WHERE chat_id = ?', [chatId], (err, row) => { - this.logError('addAllowEntityByChat Error: ', err) + if (err) { + this.logError('addAllowEntityByChat Error: ', err) + throw err + } let exitAllows: string[] = [] if (row) { exitAllows = row as string[] @@ -58,7 +61,7 @@ export class BindItemService extends AbstractSqlService { let allowEntitiesJsonArraySql = '' const params = [] for (let i = 0; i < insertAllows.length; i++) { - const sql = `'$[' || (json_array_length(allow_entities) + ${i}) || ']', ?` + const sql = `'$[' || (json_array_length(COALESCE(allow_entities, '[]')) + ${i}) || ']', ?` params.push(insertAllows[i]) if (i !== insertAllows.length - 1) { allowEntitiesJsonArraySql += sql + ',' @@ -66,19 +69,23 @@ export class BindItemService extends AbstractSqlService { allowEntitiesJsonArraySql += sql } } - const updateAllowEntitiesSql = `json_insert(allow_entities, ${allowEntitiesJsonArraySql})` - const sql = chatId ? `UPDATE tb_bind_item + // json_array_append(COALESCE(allow_entities, '[]'), '$', ?) + const updateAllowEntitiesSql = `json_insert(COALESCE(allow_entities, '[]'), ${allowEntitiesJsonArraySql})` + const sql = chatId !== -1 ? `UPDATE tb_bind_item SET allow_entities = ${updateAllowEntitiesSql} WHERE chat_id = ?` : `UPDATE tb_bind_item SET allow_entities = ${updateAllowEntitiesSql}` - if (chatId) { + if (chatId !== -1) { params.push(chatId) } // console.log('SQL:', sql) // console.log('Params:', params) this.db.prepare(sql, params).run().finalize((err) => { - this.logError('addAllowEntityByChat Error: ', err) + if (err) { + this.logError('addAllowEntityByChat Error: ', err) + throw err + } }) }) }) @@ -445,11 +452,10 @@ export class BindItemService extends AbstractSqlService { stmt.run(bind.wechat_id, bind.chat_id) stmt.finalize() - const stmt1 = this.db.prepare(`INSERT INTO tb_bind_item ( - name, chat_id, type, bind_id, alias, wechat_id, - avatar, has_bound, forward, avatar_hash, allow_entities, room_number - ) - VALUES (?, ?, ?, ?, ?, ?, ?, 1, 1, ?, ?, ?)`) + const stmt1 = this.db.prepare(`INSERT INTO tb_bind_item (name, chat_id, type, bind_id, alias, wechat_id, + avatar, has_bound, forward, avatar_hash, + allow_entities, room_number) + VALUES (?, ?, ?, ?, ?, ?, ?, 1, 1, ?, ?, ?)`) stmt1.run( bind.name, bind.chat_id, bind.type, bind.bind_id, bind.alias, bind.wechat_id, bind.avatar, bind.avatar_hash, bind.allow_entities, bind.room_number