123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- /* eslint-disable @typescript-eslint/no-unsafe-return */
- /* eslint-disable @typescript-eslint/member-delimiter-style */
- /* eslint-disable prefer-arrow/prefer-arrow-functions */
- import * as fs from 'fs';
- import * as path from 'path';
- import { relativeDate } from './datetime';
- import { getLogger } from './loggers';
- import { parseAction } from './twitter';
- const logger = getLogger('command');
- function parseCmd(message: string): {
- cmd: string;
- args: string[];
- } {
- message = message.trim();
- message = message.replace('\\\\', '\\0x5c');
- message = message.replace('\\\"', '\\0x22');
- message = message.replace('\\\'', '\\0x27');
- const strs = message.match(/'[\s\S]*?'|(?:\S+=)?"[\s\S]*?"|\S+/mg);
- const cmd = strs?.length ? strs[0].length ? strs[0].substring(0, 1) === '/' ? strs[0].substring(1) : '' : '' : '';
- const args = (strs ?? []).slice(1).map(arg => {
- arg = arg.replace(/^(\S+=)?["']+(?!.*=)|["']+$/g, '$1');
- arg = arg.replace('\\0x27', '\\\'');
- arg = arg.replace('\\0x22', '\\\"');
- arg = arg.replace('\\0x5c', '\\\\');
- return arg;
- });
- return {
- cmd,
- args,
- };
- }
- function sub(chat: IChat, args: string[], reply: (msg: string) => any,
- lock: ILock, lockfile: string
- ): void {
- if (chat.chatType === ChatType.Temp) {
- return reply('请先添加机器人为好友。');
- }
- const index = lock.subscribers.findIndex(({chatID, chatType}) =>
- chat.chatID === chatID && chat.chatType === chatType
- );
- if (index > -1) return reply('此聊天已订阅 IDOLY PRIDE BWIKI 更新提醒。');
- lock.subscribers.push(chat);
- logger.warn(`chat ${JSON.stringify(chat)} has subscribed to updates`);
- fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
- reply('已为此聊天订阅 IDOLY PRIDE BWIKI 更新提醒。');
- }
- function unsub(chat: IChat, args: string[], reply: (msg: string) => any,
- lock: ILock, lockfile: string
- ): void {
- if (chat.chatType === ChatType.Temp) {
- return reply('请先添加机器人为好友。');
- }
- const index = lock.subscribers.findIndex(({chatID, chatType}) =>
- chat.chatID === chatID && chat.chatType === chatType
- );
- if (index === -1) return reply('此聊天未订阅 IDOLY PRIDE BWIKI 更新提醒。');
- lock.subscribers.splice(index, 1);
- logger.warn(`chat ${JSON.stringify(chat)} has unsubscribed from updates`);
- fs.writeFileSync(path.resolve(lockfile), JSON.stringify(lock));
- reply('已为此聊天退订 IDOLY PRIDE BWIKI 更新提醒。');
- }
- function status(chat: IChat, _: string[], reply: (msg: string) => any, lock: ILock): void {
- const lastAction = lock.lastActions.sort(
- (a1, a2) => Date.parse(a2.timestamp) - Date.parse(a1.timestamp)
- )[0];
- reply(`IDOLY PRIDE 官方推特追踪情况:
- 上次更新时间:${relativeDate(lastAction.timestamp || '')}
- 上次更新内容:${parseAction(lastAction)}
- 上次检查时间:${relativeDate(lock.updatedAt || '')}`);
- }
- export { parseCmd, sub, unsub, status };
|