Skip to content

Commit

Permalink
Modify: 添加允许转发的账号
Browse files Browse the repository at this point in the history
  • Loading branch information
xleat committed Nov 1, 2024
1 parent 2430ec4 commit b26761d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/client/TelegramBotClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
6 changes: 6 additions & 0 deletions src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
6 changes: 6 additions & 0 deletions src/i18n/locales/zh.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const zh = {
command: {
aad: {
success: '添加成功',
fail: '添加失败',
exist: '已存在',
noUser: '没有用户',
},
description: {
help: '使用说明',
start: '开始',
Expand Down
30 changes: 18 additions & 12 deletions src/service/BindItemService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -58,27 +61,31 @@ 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 + ','
} else {
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
}
})
})
})
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b26761d

Please sign in to comment.