Skip to content

Commit

Permalink
fix observer
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 19, 2020
1 parent 92636bd commit 4b64c08
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/koishi-core/src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User, Group, UserField, GroupField } from './database'
import { User, Group } from './database'
import { ParsedCommandLine } from './command'
import { isInteger } from 'koishi-utils'

Expand Down
9 changes: 4 additions & 5 deletions packages/koishi-utils/src/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ const builtinClasses = ['Date', 'RegExp', 'Set', 'Map', 'WeakSet', 'WeakMap', 'A
const refs: Record<string | number, any> = {}

function observeObject <T extends object> (target: T, label: string, update?: () => void): T {
Object.defineProperty(target, '__proxyGetters__', { value: {} })
if (!target['__proxyGetters__']) {
Object.defineProperty(target, '__proxyGetters__', { value: {} })
}

if (!update) {
Object.defineProperty(target, '_diff', {
value: {},
writable: true,
})
Object.defineProperty(target, '_diff', { value: {}, writable: true })
}

return new Proxy(target as Observed<T, any>, {
Expand Down
32 changes: 31 additions & 1 deletion packages/plugin-teach/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
import { Context } from 'koishi-core'
import { DialogueFlag } from './database'
import { simplifyQuestion, simplifyAnswer } from './utils'
import { simplify } from 'koishi-utils'

const prefixPunctuation = /^([()\]]|\[(?!cq:))*/
const suffixPunctuation = /([.,?!()[~]|(?<!\[cq:[^\]]+)\])*$/

export function stripPunctuation (source: string) {
source = source.toLowerCase()
.replace(/\s+/g, '')
.replace(//g, ',')
.replace(//g, ',')
.replace(//g, '.')
.replace(//g, '?')
.replace(//g, '!')
.replace(//g, '(')
.replace(//g, ')')
.replace(//g, '[')
.replace(//g, ']')
.replace(//g, '~')
return source
.replace(prefixPunctuation, '')
.replace(suffixPunctuation, '') || source
}

export function simplifyQuestion (source: string) {
return simplify(stripPunctuation(String(source || '')))
}

export function simplifyAnswer (source: string) {
return (String(source || '')).trim()
}

export default function apply (ctx: Context) {
ctx.command('teach')
Expand Down Expand Up @@ -75,5 +104,6 @@ export default function apply (ctx: Context) {
ctx.on('dialogue/receive', (meta, test) => {
if (meta.message.includes('[CQ:image,')) return true
test.question = simplifyQuestion(meta.message)
return !test.question
})
}
2 changes: 1 addition & 1 deletion packages/plugin-teach/src/receiver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, UserField, getSenderName, Meta } from 'koishi-core'
import { CQCode, sleep } from 'koishi-utils'
import { simplifyQuestion, getDialogues } from './utils'
import { getDialogues } from './utils'
import { Dialogue, DialogueTest } from './database'

declare module 'koishi-core/dist/context' {
Expand Down
37 changes: 2 additions & 35 deletions packages/plugin-teach/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, Meta, User } from 'koishi-core'
import { simplify, difference } from 'koishi-utils'
import { difference } from 'koishi-utils'
import { Dialogue, DialogueTest } from './database'
import { SessionState } from './receiver'

Expand All @@ -17,40 +17,7 @@ declare module 'koishi-core/dist/context' {
}
}

export interface TeachConfig {
key?: string
imageServer?: string
uploadServer?: string
}

const prefixPunctuation = /^([()\]]|\[(?!cq:))*/
const suffixPunctuation = /([.,?!()[~]|(?<!\[cq:[^\]]+)\])*$/

export function stripPunctuation (source: string) {
source = source.toLowerCase()
.replace(/\s+/g, '')
.replace(//g, ',')
.replace(//g, ',')
.replace(//g, '.')
.replace(//g, '?')
.replace(//g, '!')
.replace(//g, '(')
.replace(//g, ')')
.replace(//g, '[')
.replace(//g, ']')
.replace(//g, '~')
return source
.replace(prefixPunctuation, '')
.replace(suffixPunctuation, '') || source
}

export function simplifyQuestion (source: string) {
return simplify(stripPunctuation(String(source || '')))
}

export function simplifyAnswer (source: string) {
return (String(source || '')).trim()
}
export interface TeachConfig {}

export function deleteDuplicate <T> (array: T[]) {
return [...new Set(array)]
Expand Down

0 comments on commit 4b64c08

Please sign in to comment.