Bladeren bron

重新被添加到群组数据重置

Taio_O 1 maand geleden
bovenliggende
commit
04b9a7db3a
3 gewijzigde bestanden met toevoegingen van 62 en 20 verwijderingen
  1. 10 7
      admin/config/initDb.js
  2. 50 12
      admin/index.js
  3. 2 1
      admin/数据文件路径

+ 10 - 7
admin/config/initDb.js

@@ -22,11 +22,14 @@ const initDatabase = async () => {
         await pool.query(`
             CREATE TABLE IF NOT EXISTS groups (
                 id INT AUTO_INCREMENT PRIMARY KEY,
-                group_id VARCHAR(50) NOT NULL UNIQUE,
-                group_name VARCHAR(100) NOT NULL,
-                group_type ENUM('personal', 'public', 'private') NOT NULL DEFAULT 'public' COMMENT '群组类型:个人用户/公开群聊/隐私群聊',
-                is_active BOOLEAN DEFAULT TRUE,
-                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+                group_id VARCHAR(255) NOT NULL UNIQUE,
+                group_name VARCHAR(255) NOT NULL,
+                group_type ENUM('personal', 'public') NOT NULL,
+                creator_id VARCHAR(255) NOT NULL,
+                is_active BOOLEAN DEFAULT true,
+                last_join_time DATETIME DEFAULT CURRENT_TIMESTAMP,
+                created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
+                updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
             )
         `);
 
@@ -37,8 +40,8 @@ const initDatabase = async () => {
                 ADD COLUMN group_type ENUM('personal', 'public', 'private') NOT NULL DEFAULT 'public' COMMENT '群组类型:个人用户/公开群聊/隐私群聊' AFTER group_name,
                 ADD COLUMN fee_rate DECIMAL(5,2) DEFAULT 0.00 COMMENT '费率' AFTER group_type,
                 ADD COLUMN exchange_rate DECIMAL(10,4) DEFAULT 1.0000 COMMENT '汇率' AFTER fee_rate,
-                ADD COLUMN creator_id INT NOT NULL COMMENT '创建人ID' AFTER exchange_rate,
-                ADD COLUMN admin_id INT NOT NULL COMMENT '管理员ID' AFTER creator_id
+                ADD COLUMN admin_id INT NOT NULL COMMENT '管理员ID' AFTER exchange_rate,
+                ADD COLUMN last_join_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '机器人最后加入时间' AFTER is_active
             `);
         } catch (error) {
             // 如果列已存在,忽略错误

+ 50 - 12
admin/index.js

@@ -160,8 +160,35 @@ bot.on('new_chat_members', async (msg) => {
             
             // 检查群组是否在允许列表中
             const chatIdStr = chatId.toString();
-            if (!data.allowedGroups.includes(chatIdStr)) {
-                try {
+            try {
+                // 先检查数据库中是否存在该群组
+                const existingGroup = await Group.findByGroupId(chatIdStr);
+                
+                if (existingGroup) {
+                    // 如果群组存在,更新群组状态为活跃,同时更新群组名称和加入时间
+                    await pool.query(`
+                        UPDATE groups 
+                        SET is_active = true,
+                            group_name = ?,
+                            last_join_time = CURRENT_TIMESTAMP
+                        WHERE group_id = ?
+                    `, [msg.chat.title || existingGroup.group_name, chatIdStr]);
+
+                    // 更新内存中的群组列表
+                    if (!data.allowedGroups.includes(chatIdStr)) {
+                        data.allowedGroups.push(chatIdStr);
+                        saveData();
+                    }
+                    // 发送欢迎消息并显示当前账单
+                    await sendMessage(chatId, '感谢重新添加我为群组成员!');
+                    const billMessage = await generateBillMessage(chatId);
+                    if (billMessage) {
+                        await sendMessage(chatId, billMessage, {
+                            reply_markup: generateInlineKeyboard(chatId)
+                        });
+                    }
+                } else {
+                    // 如果群组不存在,创建新群组
                     const groupData = {
                         groupId: chatIdStr,
                         groupName: msg.chat.title || '未命名群组',
@@ -180,21 +207,19 @@ bot.on('new_chat_members', async (msg) => {
                         // 更新内存中的群组列表
                         data.allowedGroups.push(chatIdStr);
                         saveData();
-                        sendMessage(chatId, '感谢添加我为群组成员!使用 /help 查看可用命令。');
+                        await sendMessage(chatId, '感谢添加我为群组成员!使用 /help 查看可用命令。');
                     } else {
-                        sendMessage(chatId, '添加群组失败,请联系管理员。');
+                        await sendMessage(chatId, '添加群组失败,请联系管理员。');
                     }
-                } catch (error) {
-                    console.error('创建群组失败:', error);
-                    sendMessage(chatId, '添加群组失败,请联系管理员。');
                 }
-            } else {
-                sendMessage(chatId, '感谢添加我为群组成员!使用 /help 查看可用命令。');
+            } catch (error) {
+                console.error('处理群组加入失败:', error);
+                await sendMessage(chatId, '添加群组失败,请联系管理员。');
             }
         } else {
             // 其他新成员
             console.log(`新成员加入群组: ${member.username || member.first_name} (${member.id})`);
-            sendMessage(chatId, `欢迎 ${member.username || member.first_name} 加入群组!`);
+            await sendMessage(chatId, `欢迎 ${member.username || member.first_name} 加入群组!`);
         }
     }
 });
@@ -406,13 +431,26 @@ bot.onText(/\/help/, (msg) => {
 // 生成账单消息
 async function generateBillMessage(chatId) {
     try {
-        // 获取最近的交易记录
+        // 获取群组的最后加入时间
+        const [groupInfo] = await pool.query(
+            'SELECT last_join_time FROM groups WHERE group_id = ?',
+            [chatId.toString()]
+        );
+
+        if (!groupInfo || groupInfo.length === 0) {
+            return '暂无交易记录';
+        }
+
+        const lastJoinTime = groupInfo[0].last_join_time;
+
+        // 获取机器人加入后的交易记录
         const [records] = await pool.query(`
             SELECT * FROM transactions 
             WHERE group_id = ? 
+            AND time >= ?
             ORDER BY time DESC
             LIMIT 10
-        `, [chatId.toString()]);
+        `, [chatId.toString(), lastJoinTime]);
 
         if (!records || records.length === 0) {
             return '暂无交易记录';

+ 2 - 1
admin/数据文件路径

@@ -16,6 +16,7 @@
   "withdrawals": [],
   "lastUpdate": "2025-05-07T03:00:11.021Z",
   "allowedGroups": [
-    "4754375683"
+    "4754375683",
+    "-4757139029"
   ]
 }