Skip to content

Commit

Permalink
teach/eval: command.config.noInterp/noEval
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 25, 2020
1 parent 94673a6 commit 1bf9d0a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
16 changes: 10 additions & 6 deletions packages/plugin-eval/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Context, User, Session } from 'koishi-core'
import { App, Context, Session } from 'koishi-core'
import { CQCode, Logger, defineProperty, omit, Random } from 'koishi-utils'
import { Worker, ResourceLimits } from 'worker_threads'
import { WorkerAPI, WorkerConfig, WorkerData, Response } from './worker'
Expand All @@ -14,6 +14,12 @@ declare module 'koishi-core/dist/app' {
}
}

declare module 'koishi-core/dist/command' {
interface CommandConfig {
noEval?: boolean
}
}

declare module 'koishi-core/dist/context' {
interface EventMap {
'worker/start' (): void | Promise<void>
Expand All @@ -34,7 +40,6 @@ interface MainConfig {
prefix?: string
timeout?: number
maxLogs?: number
prohibitedCommands?: string[]
resourceLimits?: ResourceLimits
}

Expand All @@ -47,7 +52,6 @@ const defaultConfig: Config = {
timeout: 1000,
setupFiles: {},
maxLogs: Infinity,
prohibitedCommands: ['evaluate', 'echo', 'broadcast', 'teach', 'contextify'],
}

const logger = new Logger('eval')
Expand Down Expand Up @@ -103,7 +107,7 @@ export function apply(ctx: Context, config: Config = {}) {
workerData: {
entry: process.env.KOISHI_WORKER_ENTRY,
logLevels: Logger.levels,
...omit(config, ['maxLogs', 'resourceLimits', 'timeout', 'prohibitedCommands']),
...omit(config, ['maxLogs', 'resourceLimits', 'timeout']),
},
resourceLimits: config.resourceLimits,
})
Expand Down Expand Up @@ -141,12 +145,12 @@ export function apply(ctx: Context, config: Config = {}) {
})

ctx.on('before-command', ({ command, session }) => {
if (config.prohibitedCommands.includes(command.name) && session._isEval) {
if (command.config.noEval && session._isEval) {
return `不能在 evaluate 指令中调用 ${command.name} 指令。`
}
})

const evaluate = ctx.command('evaluate [expr...]', '执行 JavaScript 脚本')
const evaluate = ctx.command('evaluate [expr...]', '执行 JavaScript 脚本', { noEval: true })
.alias('eval')
.userFields(['authority'])
.option('slient', '-s 不输出最后的结果')
Expand Down
9 changes: 7 additions & 2 deletions packages/plugin-teach/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { formatQuestionAnswers } from './search'
import { format } from 'util'
import leven from 'leven'

declare module 'koishi-core/dist/command' {
interface CommandConfig {
noInterp?: boolean
}
}

declare module 'koishi-core/dist/plugins/message' {
namespace Message {
export namespace Teach {
Expand Down Expand Up @@ -169,9 +175,8 @@ export default function apply(ctx: Context, config: Dialogue.Config) {
})
})

const { prohibitedCommands = [] } = config
ctx.on('before-command', ({ command, session }) => {
if (prohibitedCommands.includes(command.name) && session._redirected) {
if (command.config.noInterp && session._redirected) {
return format(Message.Teach.ProhibitedCommand, command.name)
}
})
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-teach/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export namespace Dialogue {
prefix?: string
historyAge?: number
validateRegExp?: RegExpValidator.Options
prohibitedCommands?: string[]
}

export enum Flag {
Expand Down

0 comments on commit 1bf9d0a

Please sign in to comment.