kokkoro-plugin-kfc 3.0.2 → 3.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/index.ts +4 -7
- package/src/service.ts +15 -3
package/README.md
CHANGED
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useCommand, useLogger } from '@kokkoro/core';
|
|
2
2
|
|
|
3
|
-
import { fetchCrazyThursday
|
|
3
|
+
import { fetchCrazyThursday } from './service';
|
|
4
4
|
|
|
5
5
|
const logger = useLogger();
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ const BURGER_BRANDS = /(?:麦当劳|金拱门|华莱士|汉堡王|德克士|塔
|
|
|
11
11
|
|
|
12
12
|
const KEYWORDS = [PAYMENT, KFC, THURSDAY];
|
|
13
13
|
const SHORTCUT = new RegExp([...KEYWORDS, BURGER_BRANDS].map(pattern => pattern.source).join('|'), 'i');
|
|
14
|
-
const
|
|
14
|
+
const WEEKDAY_FORMATTER = new Intl.DateTimeFormat('en-US', {
|
|
15
15
|
timeZone: 'Asia/Shanghai',
|
|
16
16
|
weekday: 'long',
|
|
17
17
|
});
|
|
@@ -20,17 +20,14 @@ function shouldTrigger(content: string) {
|
|
|
20
20
|
if (KEYWORDS.some(pattern => pattern.test(content))) {
|
|
21
21
|
return true;
|
|
22
22
|
}
|
|
23
|
-
return
|
|
23
|
+
return WEEKDAY_FORMATTER.format(new Date()) === 'Thursday' && BURGER_BRANDS.test(content);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export default () => {
|
|
27
27
|
useCommand('/疯狂星期四', async context => {
|
|
28
28
|
if (shouldTrigger(context.content)) {
|
|
29
|
-
|
|
29
|
+
const result = await fetchCrazyThursday(logger);
|
|
30
30
|
|
|
31
|
-
const result = await fetchCrazyThursday();
|
|
32
|
-
|
|
33
|
-
logger.debug('收到疯狂星期四响应', result);
|
|
34
31
|
logger.info('已获取疯狂星期四文案');
|
|
35
32
|
|
|
36
33
|
return result.success ? result.text : result.message;
|
package/src/service.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type Logger } from '@kokkoro/core';
|
|
2
|
+
|
|
1
3
|
export interface CrazyThursday {
|
|
2
4
|
readonly success: boolean;
|
|
3
5
|
readonly message: string;
|
|
@@ -6,12 +8,22 @@ export interface CrazyThursday {
|
|
|
6
8
|
|
|
7
9
|
export const KFC_API = 'https://kfc.yuki.sh';
|
|
8
10
|
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
+
/**
|
|
12
|
+
* 请求随机的疯狂星期四文案,并返回完整的接口响应。
|
|
13
|
+
*
|
|
14
|
+
* @param logger - 在 debug 日志中记录请求地址和完整接口响应。
|
|
15
|
+
*/
|
|
16
|
+
export async function fetchCrazyThursday(logger?: Logger): Promise<CrazyThursday> {
|
|
17
|
+
logger?.debug('发送疯狂星期四请求', { method: 'GET', url: KFC_API });
|
|
18
|
+
|
|
11
19
|
const response = await fetch(KFC_API);
|
|
12
20
|
|
|
13
21
|
if (!response.ok) {
|
|
14
22
|
throw new Error(`接口请求失败,状态码 ${response.status}`);
|
|
15
23
|
}
|
|
16
|
-
|
|
24
|
+
const result = <CrazyThursday>await response.json();
|
|
25
|
+
|
|
26
|
+
logger?.debug('收到疯狂星期四响应', result);
|
|
27
|
+
|
|
28
|
+
return result;
|
|
17
29
|
}
|