Skip to content

Commit

Permalink
feat(koishi): support captureQuote, fix #1432
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 30, 2024
1 parent 3bb19f4 commit 4dd30f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ function toStringType(type: Argv.Type) {

export namespace Command {
export interface Config extends Argv.CommandBase.Config, Permissions.Config {
captureQuote?: boolean
/** disallow unknown options */
checkUnknown?: boolean
/** check argument count */
Expand All @@ -376,6 +377,7 @@ export namespace Command {
permissions: Schema.array(String).role('perms').default(['authority:1']).description('权限继承。'),
dependencies: Schema.array(String).role('perms').description('权限依赖。'),
slash: Schema.boolean().description('启用斜线指令功能。').default(true),
captureQuote: Schema.boolean().description('是否捕获引用文本。').default(true).hidden(),
checkUnknown: Schema.boolean().description('是否检查未知选项。').default(false).hidden(),
checkArgCount: Schema.boolean().description('是否检查参数数量。').default(false).hidden(),
showWarning: Schema.boolean().description('是否显示警告。').default(true).hidden(),
Expand Down
31 changes: 15 additions & 16 deletions packages/core/src/command/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,9 @@ export class Commander {

ctx.before('parse', (content, session) => {
// we need to make sure that the user truly has the intension to call a command
const { quote, isDirect, stripped: { prefix, appel } } = session
const { isDirect, stripped: { prefix, appel } } = session
if (!isDirect && typeof prefix !== 'string' && !appel) return
const argv = Argv.parse(content)
if (quote?.content) {
argv.tokens.push({
content: quote.content,
quoted: true,
inters: [],
terminator: '',
})
}
return argv
return Argv.parse(content)
})

ctx.on('interaction/command', (session) => {
Expand Down Expand Up @@ -173,26 +164,26 @@ export class Commander {
}, { numeric: true })

this.domain('integer', (source, session) => {
const value = +source
const value = +source.replace(/[,_]/g, '')
if (value * 0 === 0 && Math.floor(value) === value) return value
throw new Error('internal.invalid-integer')
}, { numeric: true })

this.domain('posint', (source, session) => {
const value = +source
const value = +source.replace(/[,_]/g, '')
if (value * 0 === 0 && Math.floor(value) === value && value > 0) return value
throw new Error('internal.invalid-posint')
}, { numeric: true })

this.domain('natural', (source, session) => {
const value = +source
const value = +source.replace(/[,_]/g, '')
if (value * 0 === 0 && Math.floor(value) === value && value >= 0) return value
throw new Error('internal.invalid-natural')
}, { numeric: true })

this.domain('bigint', (source, session) => {
try {
return BigInt(source)
return BigInt(source.replace(/[,_]/g, ''))
} catch {
throw new Error('internal.invalid-integer')
}
Expand Down Expand Up @@ -291,7 +282,7 @@ export class Commander {
if (argv.command) return argv.command
if (argv.name) return argv.command = this.resolve(argv.name)

const { stripped, isDirect } = argv.session
const { stripped, isDirect, quote } = argv.session
// guild message should have prefix or appel to be interpreted as a command call
const isStrict = this.config.prefixMode === 'strict' || !isDirect && !stripped.appel
if (argv.root && stripped.prefix === null && isStrict) return
Expand All @@ -307,6 +298,14 @@ export class Commander {
argv.options = command._aliases[name].options
if (command._arguments.length) break
}
if (argv.command?.config.captureQuote !== false && quote?.content) {
argv.tokens.push({
content: quote.content,
quoted: true,
inters: [],
terminator: '',
})
}
return argv.command
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/common/inspect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const Config: Schema<Config> = Schema.object({})
export function apply(ctx: Context) {
ctx.i18n.define('zh-CN', zhCN)

ctx.command('inspect')
ctx.command('inspect', { captureQuote: false })
.action(({ session }, target) => {
if (session.quote) {
return session.text('.message', {
Expand Down

0 comments on commit 4dd30f2

Please sign in to comment.