Skip to content

Commit

Permalink
feat(core): support slash commands
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jul 2, 2023
1 parent 8b44675 commit 5b180ce
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@koishijs/i18n-utils": "^1.0.0",
"@koishijs/utils": "^7.0.2",
"@minatojs/core": "^2.3.2",
"@satorijs/core": "^2.4.0",
"@satorijs/core": "^2.5.0",
"cordis": "^2.8.0",
"cosmokit": "^1.4.2",
"fastest-levenshtein": "^1.0.16"
Expand Down
34 changes: 32 additions & 2 deletions packages/core/src/command/command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Awaitable, camelize, Dict, isNullable, remove } from 'cosmokit'
import { Awaitable, camelize, Dict, isNullable, remove, valueMap } from 'cosmokit'
import { coerce } from '@koishijs/utils'
import { Context, Fragment, Logger, Schema, Session } from '@satorijs/core'
import { Context, Fragment, Logger, Schema, Session, Universal } from '@satorijs/core'
import { Disposable } from 'cordis'
import { Argv } from './parser'
import { Next, SessionError } from '../middleware'
Expand Down Expand Up @@ -334,6 +334,33 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev
remove(this.parent.children, this)
}
}

toJSON(): Universal.Command {
return {
name: this.name,
aliases: this._aliases,
description: valueMap(this.ctx.i18n._data, (store) => store[`commands.${this.name}.description`] as string),
arguments: this._arguments.map(arg => ({
name: arg.name,
type: toStringType(arg.type),
description: { '': toStringType(arg.type) },
required: arg.required,
})),
options: Object.entries(this._options).map(([name, option]) => ({
name,
type: toStringType(option.type),
description: valueMap(this.ctx.i18n._data, (store) => store[`commands.${this.name}.options.${name}`] as string),
required: option.required,
})),
children: this.children
.filter(child => child.name.includes('.'))
.map(child => child.toJSON()),
}
}
}

function toStringType(type: Argv.Type) {
return typeof type === 'string' ? type : 'string'
}

export namespace Command {
Expand All @@ -350,10 +377,13 @@ export namespace Command {
handleError?: boolean | ((error: Error, argv: Argv) => Awaitable<void | Fragment>)
/** depend on existing commands */
patch?: boolean
/** enable slash integration */
slash?: boolean
}

export const Config: Schema<Config> = Schema.object({
authority: Schema.computed(Schema.natural()).description('指令的权限等级。').default(1),
slash: Schema.boolean().description('启用 slash 集成功能。'),
checkUnknown: Schema.boolean().description('是否检查未知选项。').default(false).hidden(),
checkArgCount: Schema.boolean().description('是否检查参数数量。').default(false).hidden(),
showWarning: Schema.boolean().description('是否显示警告。').default(true).hidden(),
Expand Down
18 changes: 17 additions & 1 deletion packages/core/src/command/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Awaitable, defineProperty } from 'cosmokit'
import { Context, h, Schema, Session } from '@satorijs/core'
import { Bot, Context, h, Schema, Session } from '@satorijs/core'
import { Command } from './command'
import { Argv } from './parser'
import validate from './validate'
Expand Down Expand Up @@ -105,6 +105,22 @@ export class Commander extends Map<string, Command> {
ctx.schema.extend('command-option', Schema.object({
authority: Schema.computed(Schema.natural()).description('选项的权限等级。').default(0),
}), 1000)

ctx.on('ready', () => {
const bots = ctx.bots.filter(v => v.status === 'online' && v.updateCommands)
bots.forEach(bot => this.updateCommands(bot))
})

ctx.on('bot-status-updated', async (bot) => {
if (bot.status !== 'online' || !bot.updateCommands) return
this.updateCommands(bot)
})
}

updateCommands(bot: Bot) {
return bot.updateCommands(this._commandList
.filter(cmd => !cmd.name.includes('.'))
.map(cmd => cmd.toJSON()))
}

private _resolvePrefixes(session: Session) {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export class Processor {
this.middleware(this._process.bind(this), true)
ctx.on('message', this._handleMessage.bind(this))

ctx.on('interaction/command', (session) => {
const { name, options, arguments: args } = session.data.command
session.execute({ name, args, options })
})

ctx.before('attach-user', (session, fields) => {
session.collect('user', session.argv, fields)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@koishijs/core": "4.13.5",
"@koishijs/loader": "4.0.0",
"@koishijs/utils": "^7.0.2",
"@satorijs/satori": "^2.4.0",
"@satorijs/satori": "^2.5.0",
"cac": "^6.7.14",
"kleur": "^4.1.5",
"ns-require": "^1.1.4"
Expand Down

0 comments on commit 5b180ce

Please sign in to comment.