generated from l-collect/ts-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@zhinjs/core": patch | ||
--- | ||
|
||
fix: release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { AdapterBot, Plugin } from 'zhin'; | ||
import { OneBotV12Adapter } from '@zhinjs/onebot-12'; | ||
const pusher = new Plugin('消息推送'); | ||
type PusherConfig = { | ||
unique_id: string; | ||
group_id: string; | ||
service: string; | ||
interval: number; | ||
}; | ||
type PushResult = { | ||
unique_id: string; | ||
group_id: string; | ||
message: string; | ||
}; | ||
type Service = (group_id: string) => Promise<string[]>; | ||
const createSicauService = (key: '四川农业大学通知服务' | '四川农业大学二课提醒') => { | ||
let cacheMap: Map<string, string[]> = new Map<string, string[]>(); | ||
return async (group_id: string): Promise<string[]> => { | ||
if (cacheMap.has(key)) return cacheMap.get(key)!; | ||
const res = await fetch('https://api.sicau.cn/api/v1/group/list'); | ||
const data = await res.json(); | ||
cacheMap.set('四川农业大学通知服务', data.notice || []); | ||
cacheMap.set('四川农业大学二课提醒', data.isicau || []); | ||
setTimeout( | ||
() => { | ||
cacheMap.clear(); | ||
}, | ||
1000 * 60 * 60, | ||
); // 缓存一小时清理 | ||
return cacheMap.get(key)!; | ||
}; | ||
}; | ||
const serviceMap: Map<string, Service> = new Map<string, Service>(); | ||
serviceMap.set('四川农业大学通知服务', createSicauService('四川农业大学通知服务')); | ||
serviceMap.set('四川农业大学二课提醒', createSicauService('四川农业大学二课提醒')); | ||
|
||
let timer: NodeJS.Timeout; | ||
pusher.command('添加推送').action<OneBotV12Adapter>(async ({ bot, prompt }) => { | ||
const groupList = await bot.getGroupList(); | ||
const pushGroup = await prompt.pick('请选择推送的群', { | ||
type: 'text', | ||
options: groupList.map(group => { | ||
return { | ||
label: group.group_name, | ||
value: group.group_id, | ||
}; | ||
}), | ||
}); | ||
const useService = await prompt.pick('请选择推送服务', { | ||
type: 'text', | ||
options: Array.from(serviceMap.keys()).map(name => { | ||
return { | ||
label: name, | ||
value: name, | ||
}; | ||
}), | ||
}); | ||
pusher.jsondb.push('functions.pusher.Configs', { | ||
unique_id: bot.unique_id, | ||
group_id: pushGroup, | ||
service: useService, | ||
}); | ||
return '添加成功'; | ||
}); | ||
pusher.mounted(app => { | ||
const receiveAndPush = async () => { | ||
const configs = pusher.jsondb.get<PusherConfig[]>('functions.pusher.Configs', []) || []; | ||
for (const config of configs) { | ||
const bot = app.adapters | ||
.get('onebot-12') | ||
?.bots.find(b => b.unique_id === config.unique_id) as AdapterBot<OneBotV12Adapter>; | ||
if (!bot) return; | ||
const service = serviceMap.get(config.service); | ||
if (service) { | ||
const messages = await service(config.group_id); | ||
for (const message of messages) { | ||
await bot.sendGroupMsg(config.group_id, message); | ||
pusher.jsondb.push<PushResult>('functions.pusher.infos', { | ||
unique_id: bot.unique_id, | ||
group_id: config.group_id, | ||
message, | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
const pushWithLoop = (delay: number) => { | ||
timer = setTimeout(() => { | ||
receiveAndPush().then(() => { | ||
pushWithLoop(delay); | ||
}); | ||
}, delay); | ||
}; | ||
receiveAndPush().then(() => { | ||
pushWithLoop(1000 * 60 * 60 * 3); | ||
}); | ||
}); | ||
pusher.beforeUnmount(() => { | ||
if (timer) clearTimeout(timer); | ||
}); |