wechaty-puppet-wechat4u
Version:
Wechat4u Puppet for Wechaty
92 lines • 3.48 kB
JavaScript
import { WebMessageType } from '../../web-schemas.js';
import { isRoomId } from '../utils/is-type.js';
import { executeRunners } from '../utils/runner.js';
const YOU_REMOVE_OTHER_REGEX_LIST = [
/^(你)将"(.+)"移出了群聊/,
/^(You) removed "(.+)" from the group chat/,
];
const OTHER_REMOVE_YOU_REGEX_LIST = [
/^(你)被"([^"]+?)"移出群聊/,
/^(You) were removed from the group chat by "([^"]+)"/,
];
const roomLeaveDebounceMap = new Map();
const DEBOUNCE_TIMEOUT = 3600 * 1000; // 1 hour
function roomLeaveDebounceKey(roomId, removeeId) {
return `${roomId}:${removeeId}`;
}
function roomLeaveAddDebounce(roomId, removeeId) {
const key = roomLeaveDebounceKey(roomId, removeeId);
const oldTimeout = roomLeaveDebounceMap.get(key);
if (oldTimeout) {
clearTimeout(oldTimeout);
}
const timeout = setTimeout(() => {
roomLeaveDebounceMap.delete(key);
}, DEBOUNCE_TIMEOUT);
roomLeaveDebounceMap.set(key, timeout);
}
// to fix: https://github.com/padlocal/wechaty-puppet-padlocal/issues/43
export function removeRoomLeaveDebounce(roomId, removeeId) {
const key = roomLeaveDebounceKey(roomId, removeeId);
roomLeaveDebounceMap.delete(key);
}
export function isRoomLeaveDebouncing(roomId, removeeId) {
const key = roomLeaveDebounceKey(roomId, removeeId);
return roomLeaveDebounceMap.get(key) !== undefined;
}
export default async (puppet, message) => {
const roomId = message.FromUserName;
if (!isRoomId(roomId) || ![WebMessageType.SYS].includes(message.MsgType)) {
return null;
}
/**
* 1. 我将别人移除
* /^(你)将"(.+)"移出了群聊/,
* 我移除别人是 10002: https://gist.github.com/padlocal/5676b96ad0ca918fdd53849417eff422
*/
const youRemoveOther = async () => {
let matches = null;
YOU_REMOVE_OTHER_REGEX_LIST.some((re) => !!(matches = message.Content.match(re)));
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (matches) {
const removerName = matches[2];
const removerId = (await puppet.roomMemberSearch(roomId, removerName))[0];
return {
removeeIdList: [removerId],
removerId: puppet.currentUserId,
roomId,
timestamp: message.CreateTime,
};
}
return null;
};
/**
* 2. 别人移除我
* /^(你)被"([^"]+?)"移出群聊/,
* // 我被别人移除是 10000:https://gist.github.com/padlocal/60be89334d4d743937f07023da20291e
*/
const otherRemoveYou = async () => {
let matches = null;
OTHER_REMOVE_YOU_REGEX_LIST.some((re) => !!(matches = message.Content.match(re)));
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (matches) {
const removerName = matches[2];
const removerId = (await puppet.roomMemberSearch(roomId, removerName))[0];
return {
removeeIdList: [puppet.currentUserId],
removerId,
roomId,
timestamp: message.CreateTime,
};
}
return null;
};
const ret = await executeRunners([youRemoveOther, otherRemoveYou]);
if (ret) {
ret.removeeIdList.forEach((leaverId) => {
roomLeaveAddDebounce(roomId, leaverId);
});
}
return ret;
};
//# sourceMappingURL=event-room-leave.js.map