12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- /* eslint-disable @typescript-eslint/no-unsafe-return */
- /* eslint-disable @typescript-eslint/member-delimiter-style */
- /* eslint-disable prefer-arrow/prefer-arrow-functions */
- import { Message } from './koishi';
- import { queryByRegExp } from './twitter';
- 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 view(_chat: IChat, args: string[], reply: (msg: string) => any): void {
- if (!Number.isInteger(Number(args[0]))) return reply('查询格式有误。格式:/nanatsuu_view〈整数〉');
- queryByRegExp('t7s_staff', /今週の『それゆけ!ナナスタ☆通信』第(\d+)話はこちら!/, 3600 * 24 * 6)
- .then(match => {
- if (!match) throw Error();
- let query = Number(args[0] || Number(match[1]));
- if (query < 0) query += Number(match[1]);
- if (args[0] === '0' || query < 0 || query > Number(match[1])) {
- return reply(`查询取值范围有误。当前可用的取值范围:${1 - Number(match[1])}~${Number(match[1])}`);
- }
- reply(`第 ${query} 话:\n` +
- Message.Image(`https://d2n19nac4w0gh6.cloudfront.net/resource/images/webview/comic/story/comic_${
- String(query).padStart(3, '0')
- }.jpg`)
- );
- }).catch((err: Error) => reply(`查询失败,请稍后重试。${err.message ? `原因:${err}`: ''}`));
- }
- export { parseCmd, view };
|