Skip to content

Commit

Permalink
feat(core): support functional appOptions.prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 5, 2021
1 parent a2c73dc commit 5780080
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
23 changes: 13 additions & 10 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface DelayOptions {
export interface AppOptions extends BotOptions {
port?: number
bots?: BotOptions[]
prefix?: string | string[]
prefix?: string | string[] | ((session: Session.Message) => void | string | string[])
nickname?: string | string[]
maxListeners?: number
prettyErrors?: boolean
Expand Down Expand Up @@ -65,7 +65,6 @@ export class App extends Context {
_sessions: Record<string, Session> = {}

private _nameRE: RegExp
private _prefixRE: RegExp

static defaultConfig: AppOptions = {
maxListeners: 64,
Expand Down Expand Up @@ -161,11 +160,9 @@ export class App extends Context {
}

prepare() {
const { nickname, prefix } = this.options
const { nickname } = this.options
this.options.nickname = makeArray(nickname)
this.options.prefix = Array.isArray(prefix) ? prefix : [prefix || '']
this._nameRE = createLeadingRE(this.options.nickname, '@?', '([,,]\\s*|\\s+)')
this._prefixRE = createLeadingRE(this.options.prefix)
}

async start() {
Expand Down Expand Up @@ -205,7 +202,13 @@ export class App extends Context {
this._httpServer?.close()
}

private async _process(session: Session, next: NextFunction) {
private _resolvePrefixes(session: Session.Message) {
const { prefix } = this.options
const temp = typeof prefix === 'function' ? prefix(session) : prefix
return Array.isArray(temp) ? temp : [temp || '']
}

private async _process(session: Session.Message, next: NextFunction) {
let capture: RegExpMatchArray
let atSelf = false, appel = false, prefix: string = null
const pattern = /^\[CQ:(\w+)((,\w+=[^,\]]*)*)\]/
Expand All @@ -221,10 +224,10 @@ export class App extends Context {
content = content.slice(capture[0].length)
}

// eslint-disable-next-line no-cond-assign
if (capture = content.match(this._prefixRE)) {
prefix = capture[0]
content = content.slice(capture[0].length)
for (const _prefix of this._resolvePrefixes(session)) {
if (!content.startsWith(_prefix)) continue
prefix = _prefix
content = content.slice(_prefix.length)
}

// store parsed message
Expand Down
7 changes: 2 additions & 5 deletions packages/koishi-core/tests/runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ after(() => app.stop())
describe('Runtime', () => {
describe('Command Prefix', () => {
it('single prefix', async () => {
app.options.prefix = '!'
app.prepare()
// also support functions
app.options.prefix = () => '!'

await session1.shouldReply('cmd2', 'cmd2:123')
await session4.shouldNotReply('cmd2')
Expand All @@ -64,7 +64,6 @@ describe('Runtime', () => {

it('multiple prefixes', async () => {
app.options.prefix = ['!', '.']
app.prepare()

await session1.shouldReply('cmd2', 'cmd2:123')
await session4.shouldNotReply('cmd2')
Expand All @@ -76,7 +75,6 @@ describe('Runtime', () => {

it('optional prefix', async () => {
app.options.prefix = ['.', '']
app.prepare()

await session1.shouldReply('cmd2', 'cmd2:123')
await session4.shouldReply('cmd2', 'cmd2:123')
Expand All @@ -88,7 +86,6 @@ describe('Runtime', () => {

it('no prefix', async () => {
app.options.prefix = null
app.prepare()

await session1.shouldReply('cmd2', 'cmd2:123')
await session4.shouldReply('cmd2', 'cmd2:123')
Expand Down

0 comments on commit 5780080

Please sign in to comment.