Skip to content

Commit

Permalink
plugin-schedule: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 11, 2020
1 parent 2487a75 commit 273d003
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-schedule/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-schedule",
"description": "Schedule plugin for Koishi",
"version": "0.1.1",
"version": "1.0.0-alpha.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"author": "Shigma <1700011071@pku.edu.cn>",
Expand Down
34 changes: 21 additions & 13 deletions packages/plugin-schedule/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Context, onStart, appMap } from 'koishi-core'
import { Context, appMap, Database, CommandConfig } from 'koishi-core'
import { Schedule } from './database'
import './database'

function inspectSchedule ({ id, assignee, meta, interval, command, time }: Schedule, now = Date.now()) {
function inspectSchedule ({ id, assignee, meta, interval, command, time }: Schedule) {
if (!appMap[assignee]) return
const now = Date.now()
const app = appMap[assignee]

if (!interval) {
Expand Down Expand Up @@ -30,27 +32,33 @@ function inspectSchedule ({ id, assignee, meta, interval, command, time }: Sched
}
}

export function apply (ctx: Context) {
onStart(async ({ database }) => {
const now = Date.now()
const schedules = await database.getAllSchedules()
schedules.forEach(schedule => inspectSchedule(schedule, now))
const databases = new Set<Database>()

export const name = 'schedule'

export function apply (ctx: Context, config: CommandConfig = {}) {
const { database } = ctx.app

ctx.app.receiver.on('connect', async () => {
if (!database || databases.has(database)) return
databases.add(database)
const schedules = await database?.getAllSchedules()
schedules?.forEach(schedule => inspectSchedule(schedule))
})

ctx.command('advanced')
.subcommand('schedule <time> -- <command>', '设置定时命令', { authority: 3, maxUsage: 5 })
ctx.command('schedule <time> -- <command>', '设置定时命令', { authority: 3, maxUsage: 5, ...config })
.option('-i, --interval <interval>', '设置触发的间隔秒数', { default: 0, authority: 4 })
// .option('-l, --list', '查看已经设置的日程')
.option('-d, --delete <id>', '删除已经设置的日程', { notUsage: true })
.action(async ({ meta, options, rest }, date) => {
if (options.delete) {
await ctx.app.database.removeSchedule(options.delete)
await database.removeSchedule(options.delete)
return meta.$send('日程已删除。')
}

const time = date ? new Date(date).valueOf() : Date.now()
const schedule = await ctx.app.database.createSchedule(time, ctx.app.options.selfId, options.interval * 1000, rest, meta)
inspectSchedule(schedule)
return meta.$send(`日程已创建,编号为 ${schedule.id}。`)
const schedule = await database.createSchedule(time, ctx.app.selfId, options.interval * 1000, rest, meta)
await meta.$send(`日程已创建,编号为 ${schedule.id}。`)
return inspectSchedule(schedule)
})
}
2 changes: 2 additions & 0 deletions packages/plugin-teach/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import update from './update'
export * from './database'
export { TeachConfig }

export const name = 'teach'

export function apply (ctx: Context, config: TeachConfig = {}) {
ctx.plugin(receiver, config)

Expand Down

0 comments on commit 273d003

Please sign in to comment.