UNPKG

n8n-nodes-onebot-api

Version:

OneBot node for n8n workflow, now you can send message to your QQ friend or group. This version includes optimized group and private chat operations and enhanced permission grading. OneBot 节点用于 n8n 工作流,现在您可以向您的 QQ 朋友或群组发送消息。此版本包括优化的群聊和私聊操作以及增强的权限分级。

207 lines 9.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeMemberOperation = void 0; const GenericFunctions_1 = require("./GenericFunctions"); async function executeMemberOperation(index) { const operation = this.getNodeParameter('operation', index); let responseData = {}; let body = {}; let method = 'POST'; let endpoint = ''; switch (operation) { case 'set_group_leave': const groupId = this.getNodeParameter('group_id', index); const isDismiss = this.getNodeParameter('is_dismiss', index, false); body = { group_id: parseInt(groupId.toString(), 10), is_dismiss: isDismiss }; endpoint = 'set_group_leave'; console.log(`执行退群操作, 群ID: ${groupId}, 是否解散: ${isDismiss}`); break; case 'delete_friend': const userId = this.getNodeParameter('user_id', index); body = { user_id: parseInt(userId.toString(), 10) }; endpoint = 'delete_friend'; console.log(`执行删除好友操作, 好友QQ: ${userId}`); break; case 'batch_operation': const mode = this.getNodeParameter('batch_mode', index); const useWhitelist = this.getNodeParameter('use_whitelist', index, false); if (mode === 'groups' || mode === 'both') { const groupResponse = await handleGroups.call(this, index, useWhitelist); responseData.groupResults = groupResponse; } if (mode === 'friends' || mode === 'both') { const friendResponse = await handleFriends.call(this, index, useWhitelist); responseData.friendResults = friendResponse; } return responseData; case 'random_delete_friends': const randomResponse = await handleRandomFriendDelete.call(this, index); return randomResponse; default: throw new Error(`未知的成员关系操作: ${operation}`); } console.log(`执行成员关系操作: ${operation}, 端点: ${endpoint}, 请求体:`, JSON.stringify(body)); responseData = await GenericFunctions_1.apiRequest.call(this, method, endpoint, body); return responseData; } exports.executeMemberOperation = executeMemberOperation; async function handleGroups(index, useWhitelist) { const groupListResponse = await GenericFunctions_1.apiRequest.call(this, 'GET', 'get_group_list'); if (!(groupListResponse === null || groupListResponse === void 0 ? void 0 : groupListResponse.data) || !Array.isArray(groupListResponse.data) || groupListResponse.data.length === 0) { return { success: false, message: '获取群列表失败或没有加入任何群聊' }; } const allGroups = groupListResponse.data; console.log(`获取到 ${allGroups.length} 个群聊`); let groupsToProcess = [...allGroups]; if (useWhitelist) { const whitelistGroups = this.getNodeParameter('whitelist_groups', index, []); if (whitelistGroups.length > 0) { const whitelistGroupIds = whitelistGroups.map(id => parseInt(id.toString(), 10)); groupsToProcess = allGroups.filter(group => !whitelistGroupIds.includes(group.group_id)); console.log(`白名单群聊数: ${whitelistGroups.length}, 需要处理的群聊数: ${groupsToProcess.length}`); } } const isDismiss = this.getNodeParameter('is_dismiss_batch', index, false); const successGroups = []; const failedGroups = []; for (const group of groupsToProcess) { try { await GenericFunctions_1.apiRequest.call(this, 'POST', 'set_group_leave', { group_id: group.group_id, is_dismiss: isDismiss }); successGroups.push(group.group_id); } catch (error) { failedGroups.push({ id: group.group_id, name: group.group_name, error: error instanceof Error ? error.message : String(error) }); } await new Promise(resolve => setTimeout(resolve, 1000)); } return { success: true, total: groupsToProcess.length, processed: successGroups.length, failed: failedGroups.length, successGroups, failedGroups }; } async function handleFriends(index, useWhitelist) { const friendListResponse = await GenericFunctions_1.apiRequest.call(this, 'GET', 'get_friend_list'); if (!(friendListResponse === null || friendListResponse === void 0 ? void 0 : friendListResponse.data) || !Array.isArray(friendListResponse.data) || friendListResponse.data.length === 0) { return { success: false, message: '获取好友列表失败或没有好友' }; } const allFriends = friendListResponse.data; console.log(`获取到 ${allFriends.length} 个好友`); let friendsToProcess = [...allFriends]; if (useWhitelist) { const whitelistFriends = this.getNodeParameter('whitelist_friends', index, []); if (whitelistFriends.length > 0) { const whitelistFriendIds = whitelistFriends.map(id => parseInt(id.toString(), 10)); friendsToProcess = allFriends.filter(friend => !whitelistFriendIds.includes(friend.user_id)); console.log(`白名单好友数: ${whitelistFriends.length}, 需要处理的好友数: ${friendsToProcess.length}`); } } const successFriends = []; const failedFriends = []; for (const friend of friendsToProcess) { try { await GenericFunctions_1.apiRequest.call(this, 'POST', 'delete_friend', { user_id: friend.user_id }); successFriends.push(friend.user_id); } catch (error) { failedFriends.push({ id: friend.user_id, name: friend.remark || friend.nickname, error: error instanceof Error ? error.message : String(error) }); } await new Promise(resolve => setTimeout(resolve, 1000)); } return { success: true, total: friendsToProcess.length, processed: successFriends.length, failed: failedFriends.length, successFriends, failedFriends }; } async function handleRandomFriendDelete(index) { const maxFriendsToDelete = this.getNodeParameter('max_friends_to_delete', index); const useWhitelist = this.getNodeParameter('use_whitelist_random', index, false); const friendListResponse = await GenericFunctions_1.apiRequest.call(this, 'GET', 'get_friend_list'); if (!(friendListResponse === null || friendListResponse === void 0 ? void 0 : friendListResponse.data) || !Array.isArray(friendListResponse.data) || friendListResponse.data.length === 0) { return { success: false, message: '获取好友列表失败或没有好友' }; } const allFriends = friendListResponse.data; console.log(`获取到 ${allFriends.length} 个好友`); let friendsPool = [...allFriends]; if (useWhitelist) { const whitelistFriends = this.getNodeParameter('whitelist_friends_random', index, []); if (whitelistFriends.length > 0) { const whitelistFriendIds = whitelistFriends.map(id => parseInt(id.toString(), 10)); friendsPool = allFriends.filter(friend => !whitelistFriendIds.includes(friend.user_id)); console.log(`白名单好友数: ${whitelistFriends.length}, 可随机删除的好友数: ${friendsPool.length}`); } } const availableFriendsCount = friendsPool.length; if (availableFriendsCount === 0) { return { success: false, message: '没有可删除的好友(所有好友都在白名单中)' }; } const actualMaxToDelete = Math.min(maxFriendsToDelete, availableFriendsCount); const friendsToDeleteCount = Math.floor(Math.random() * actualMaxToDelete) + 1; console.log(`将随机删除 ${friendsToDeleteCount} 个好友(最大允许: ${actualMaxToDelete})`); const shuffledFriends = shuffleArray([...friendsPool]); const friendsToDelete = shuffledFriends.slice(0, friendsToDeleteCount); const successFriends = []; const failedFriends = []; for (const friend of friendsToDelete) { try { await GenericFunctions_1.apiRequest.call(this, 'POST', 'delete_friend', { user_id: friend.user_id }); successFriends.push({ id: friend.user_id, name: friend.remark || friend.nickname }); } catch (error) { failedFriends.push({ id: friend.user_id, name: friend.remark || friend.nickname, error: error instanceof Error ? error.message : String(error) }); } await new Promise(resolve => setTimeout(resolve, 1000)); } return { success: true, requestedMax: maxFriendsToDelete, randomlyChosen: friendsToDeleteCount, processed: successFriends.length, failed: failedFriends.length, successFriends, failedFriends }; } function shuffleArray(array) { const result = [...array]; for (let i = result.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [result[i], result[j]] = [result[j], result[i]]; } return result; } //# sourceMappingURL=MemberActions.js.map