Skip to content

Commit

Permalink
feat: ctx.filter()
Browse files Browse the repository at this point in the history
  • Loading branch information
AddonWiki committed Jul 23, 2024
1 parent c56e42b commit 6613031
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
7 changes: 2 additions & 5 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@
},
"linter": {
"rules": {
"recommended": false,
"complexity": {
"noForEach": "off"
},
"suspicious": { "noRedeclare": "off" }
"recommended": false
},
"ignore": ["**/*.js", "**/*.d.ts"]
},
"javascript": {
"formatter": {
"enabled": true,
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded",
"trailingCommas": "none",
Expand Down
78 changes: 48 additions & 30 deletions packages/core/src/components/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ import { stringRightSplit } from '@kotori-bot/tools'
import type { Context, EventsList } from 'fluoro'
import type { I18n } from '@kotori-bot/i18n'
import { CronJob } from 'cron'
import type { CommandConfig, MidwareCallback, RegexpCallback, SessionData, MessageQuick, TaskCallback } from '../types'
import type {
CommandConfig,
MidwareCallback,
RegexpCallback,
SessionData,
MessageQuick,
TaskCallback,
FilterOption
} from '../types'
import { cancelFactory, disposeFactory, quickFactory, sendMessageFactory } from '../utils/factory'
import { Command } from '../utils/command'
import CommandError from '../utils/commandError'
import { Symbols } from '../global'
import Filter from '../utils/filter'

interface MidwareData {
callback: MidwareCallback
Expand All @@ -26,6 +35,8 @@ export class Message {

public readonly [Symbols.regexp]: Set<RegexpData> = new Set()

public readonly [Symbols.filter]: Map<string, Filter> = new Map()

private handleMidware(session: SessionData) {
const { api } = session
let isPass = true
Expand Down Expand Up @@ -85,37 +96,35 @@ export class Message {
if (cancel.value) return

let matched: undefined | Command
this[Symbols.command].forEach(
async (cmd) => {
if (matched || !cmd.meta.action) return
const result = Command.run(params.raw, cmd.meta)
if (result instanceof CommandError && result.value.type === 'unknown') return

matched = cmd
this.ctx.emit('parse', { command: cmd, result, ...params, cancel: cancel.get() })
if (cancel.value || result instanceof CommandError) return

try {
const executed = await cmd.meta.action({ args: result.args, options: result.options }, session)
if (executed instanceof CommandError) {
this.ctx.emit('command', { command: cmd, result: executed, ...params })
return
}
if (executed !== undefined) session.quick(executed)
this.ctx.emit('command', {
command: cmd,
result: executed instanceof CommandError ? result : executed,
...params
})
} catch (error) {
this.ctx.emit('command', {
command: matched,
result: new CommandError({ type: 'error', error }),
...params
})
this[Symbols.command].forEach(async (cmd) => {
if (matched || !cmd.meta.action) return
const result = Command.run(params.raw, cmd.meta)
if (result instanceof CommandError && result.value.type === 'unknown') return

matched = cmd
this.ctx.emit('parse', { command: cmd, result, ...params, cancel: cancel.get() })
if (cancel.value || result instanceof CommandError) return

try {
const executed = await cmd.meta.action({ args: result.args, options: result.options }, session)
if (executed instanceof CommandError) {
this.ctx.emit('command', { command: cmd, result: executed, ...params })
return
}
if (executed !== undefined) session.quick(executed)
this.ctx.emit('command', {
command: cmd,
result: executed instanceof CommandError ? result : executed,
...params
})
} catch (error) {
this.ctx.emit('command', {
command: matched,
result: new CommandError({ type: 'error', error }),
...params
})
}
)
})

if (matched) return
this.ctx.emit('parse', {
Expand Down Expand Up @@ -193,6 +202,15 @@ export class Message {
const [cron, extraOptions] = typeof options === 'string' ? [options, {}] : [options.cron, options]
return new CronJob(cron, () => callback(this.ctx), null, extraOptions.start ?? true, extraOptions.timeZone)
}

public filter(option: FilterOption) {
if (this.ctx.identity) {
const filter = new Filter(option)
// this[Symbols.filter].set(this.ctx.identity, [...(this[Symbols.filter].get(this.ctx.identity) || []), filter])

// const dispose = () => this[Symbols.filter].delete(filter.name)
}
}
}

export default Message
1 change: 1 addition & 0 deletions packages/core/src/global/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export class Symbols {
public static readonly midware = Symbol.for('kotori.core.midware')
public static readonly command = Symbol.for('kotori.core.command')
public static readonly regexp = Symbol.for('kotori.core.regexp')
public static readonly filter = Symbol.for('kotori.core.filter')
public static readonly modules = Symbol.for('kotori.loader.module')
public static readonly job = Symbol.for('kotori.loader.job')
}
Expand Down
1 change: 0 additions & 1 deletion packages/kotori/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ program
Logger.info('ui', options)
})

program.command('module').action(() => Logger.info('module'))
program.command('module search <name>').action(() => Logger.info('module search'))
program.command('module download <name>').action(() => Logger.info('module download'))

Expand Down

0 comments on commit 6613031

Please sign in to comment.