Skip to content

Commit

Permalink
chore: deprecate MessageMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 21, 2020
1 parent 1527159 commit c677df4
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 29 deletions.
10 changes: 5 additions & 5 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Command, ShortcutConfig, ParsedCommandLine } from './command'
import { Context, Middleware, NextFunction, ContextScope, Events, EventMap } from './context'
import { GroupFlag, UserFlag, UserField, createDatabase, DatabaseConfig, GroupField } from './database'
import { showSuggestions } from './utils'
import { Meta, MessageMeta } from './meta'
import { Meta } from './meta'
import { simplify, noop } from 'koishi-utils'
import { errors, messages } from './messages'
import { ParsedLine } from './parser'
Expand Down Expand Up @@ -272,7 +272,7 @@ export class App extends Context {
}
}

private _preprocess = async (meta: MessageMeta, next: NextFunction) => {
private _preprocess = async (meta: Meta<'message'>, next: NextFunction) => {
// strip prefix
let capture: RegExpMatchArray
let atMe = false, nickname = '', prefix: string = null
Expand Down Expand Up @@ -379,7 +379,7 @@ export class App extends Context {
})
}

parseCommandLine (message: string, meta: MessageMeta): ParsedCommandLine {
parseCommandLine (message: string, meta: Meta<'message'>): ParsedCommandLine {
const name = message.split(/\s/, 1)[0].toLowerCase()
const command = this._commandMap[name]
if (command?.context.match(meta)) {
Expand All @@ -388,14 +388,14 @@ export class App extends Context {
}
}

executeCommandLine (message: string, meta: MessageMeta, next: NextFunction = noop) {
executeCommandLine (message: string, meta: Meta<'message'>, next: NextFunction = noop) {
if (!('$ctxType' in meta)) this.server.parseMeta(meta)
const argv = this.parseCommandLine(message, meta)
if (argv) return argv.command.execute(argv, next)
return next()
}

private _applyMiddlewares = async (meta: MessageMeta) => {
private _applyMiddlewares = async (meta: Meta<'message'>) => {
// preparation
const counter = this._middlewareCounter++
this._middlewareSet.add(counter)
Expand Down
8 changes: 4 additions & 4 deletions packages/koishi-core/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Context, NextFunction } from './context'
import { UserData, UserField, GroupField } from './database'
import { messages, errors } from './messages'
import { noop } from 'koishi-utils'
import { MessageMeta } from './meta'
import { Meta } from './meta'
import { format } from 'util'
import { updateUsage } from './utils'

Expand All @@ -17,7 +17,7 @@ import {
} from './parser'

export interface ParsedCommandLine extends Partial<ParsedLine> {
meta: MessageMeta
meta: Meta<'message'>
command?: Command
next?: NextFunction
}
Expand Down Expand Up @@ -225,7 +225,7 @@ export class Command {
return this
}

getConfig <K extends keyof CommandConfig> (key: K, meta: MessageMeta): Exclude<CommandConfig[K], (user: UserData) => any> {
getConfig <K extends keyof CommandConfig> (key: K, meta: Meta<'message'>): Exclude<CommandConfig[K], (user: UserData) => any> {
const value = this.config[key] as any
return typeof value === 'function' ? value(meta.$user) : value
}
Expand Down Expand Up @@ -293,7 +293,7 @@ export class Command {
}

/** check authority and usage */
private async _checkUser (meta: MessageMeta, options: Record<string, any>) {
private async _checkUser (meta: Meta<'message'>, options: Record<string, any>) {
const user = meta.$user
if (!user) return true
let isUsage = true
Expand Down
8 changes: 4 additions & 4 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { contain, union, intersection, difference } from 'koishi-utils'
import { Command, CommandConfig, ParsedCommandLine } from './command'
import { MessageMeta, Meta, contextTypes } from './meta'
import { Meta, contextTypes } from './meta'
import { EventEmitter } from 'events'
import { Sender } from './sender'
import { App } from './app'
Expand All @@ -9,7 +9,7 @@ import { messages, errors } from './messages'
import { format } from 'util'

export type NextFunction = (next?: NextFunction) => any
export type Middleware = (meta: MessageMeta, next: NextFunction) => any
export type Middleware = (meta: Meta<'message'>, next: NextFunction) => any

type PluginFunction <T extends Context, U> = (ctx: T, options: U) => void
type PluginObject <T extends Context, U> = { name?: string, apply: PluginFunction<T, U> }
Expand Down Expand Up @@ -240,12 +240,12 @@ export class Context {
return this.app._commandMap[name.slice(index + 1).toLowerCase()]
}

getCommand (name: string, meta: MessageMeta) {
getCommand (name: string, meta: Meta<'message'>) {
const command = this._getCommandByRawName(name)
if (command?.context.match(meta) && !command.getConfig('disable', meta)) return command
}

runCommand (name: string, meta: MessageMeta, args: string[] = [], options: Record<string, any> = {}, rest = '') {
runCommand (name: string, meta: Meta<'message'>, args: string[] = [], options: Record<string, any> = {}, rest = '') {
const command = this._getCommandByRawName(name)
if (!command || !command.context.match(meta) || command.getConfig('disable', meta)) {
return meta.$send(messages.COMMAND_NOT_FOUND)
Expand Down
1 change: 0 additions & 1 deletion packages/koishi-core/src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export enum contextTypes {
discuss = 2,
}

export type MessageMeta = Meta<'message'>
export type ContextType = keyof typeof contextTypes

export interface ResponsePayload {
Expand Down
6 changes: 3 additions & 3 deletions packages/koishi-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isInteger } from 'koishi-utils'
import { UserField, GroupField, UserData } from './database'
import { NextFunction } from './context'
import { Command } from './command'
import { MessageMeta } from './meta'
import { Meta } from './meta'
import { messages } from './messages'
import { format } from 'util'
import leven from 'leven'
Expand Down Expand Up @@ -53,13 +53,13 @@ export function updateUsage (name: string, user: UserData, maxUsage: number, min
interface SuggestOptions {
target: string
items: string[]
meta: MessageMeta
meta: Meta<'message'>
next: NextFunction
prefix: string
suffix: string
coefficient?: number
command: Command | ((suggestion: string) => Command)
execute: (suggestion: string, meta: MessageMeta, next: NextFunction) => any
execute: (suggestion: string, meta: Meta<'message'>, next: NextFunction) => any
}

function findSimilar (target: string, coefficient: number) {
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/tests/runtime.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MockedApp } from 'koishi-test-utils'
import { messages, MessageMeta } from 'koishi-core'
import { messages, Meta } from 'koishi-core'
import { format } from 'util'

const app = new MockedApp()
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('nickname prefix', () => {
})

describe('Command Execution', () => {
const meta: MessageMeta = {
const meta: Meta<'message'> = {
userId: 789,
selfId: app.selfId,
postType: 'message',
Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-common/src/help.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Command, UserData, MessageMeta, getUsage } from 'koishi-core'
import { Context, Command, UserData, Meta, getUsage } from 'koishi-core'

export default function apply (ctx: Context) {
ctx.command('help [command]', '显示帮助信息', { authority: 0 })
Expand Down Expand Up @@ -27,7 +27,7 @@ function getShortcuts (command: Command, user: UserData) {
})
}

function getCommands (context: Context, meta: MessageMeta, parent?: Command) {
function getCommands (context: Context, meta: Meta<'message'>, parent?: Command) {
const commands = parent
? parent.children
: context.app._commands.filter(cmd => cmd.context.match(meta))
Expand All @@ -36,13 +36,13 @@ function getCommands (context: Context, meta: MessageMeta, parent?: Command) {
.sort((a, b) => a.name > b.name ? 1 : -1)
}

function showGlobalShortcut (context: Context, meta: MessageMeta) {
function showGlobalShortcut (context: Context, meta: Meta<'message'>) {
const commands = getCommands(context, meta)
const shortcuts = [].concat(...commands.map(command => getShortcuts(command, meta.$user)))
return meta.$send(`当前可用的全局指令有:${shortcuts.join(',')}。`)
}

function getCommandList (context: Context, meta: MessageMeta, parent: Command, expand: boolean) {
function getCommandList (context: Context, meta: Meta<'message'>, parent: Command, expand: boolean) {
let commands = getCommands(context, meta, parent)
if (!expand) {
commands = commands.filter(cmd => cmd.parent === parent)
Expand All @@ -69,15 +69,15 @@ export const GLOBAL_HELP_EPILOGUE = [
'输入“帮助+指令名”查看特定指令的语法和使用示例。',
].join('\n')

function showGlobalHelp (context: Context, meta: MessageMeta, options: any) {
function showGlobalHelp (context: Context, meta: Meta<'message'>, options: any) {
return meta.$send([
GLOBAL_HELP_PROLOGUE,
...getCommandList(context, meta, null, options.expand),
GLOBAL_HELP_EPILOGUE,
].join('\n'))
}

async function showCommandHelp (command: Command, meta: MessageMeta, options: any) {
async function showCommandHelp (command: Command, meta: Meta<'message'>, options: any) {
const output = [command.name + command.declaration, command.config.description]
if (command.context.database) {
meta.$user = await command.context.database.observeUser(meta.userId)
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-schedule/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageMeta, getSelfIds, injectMethods } from 'koishi-core'
import { Meta, getSelfIds, injectMethods } from 'koishi-core'
import { noop } from 'koishi-utils'
import {} from 'koishi-database-mysql'
import {} from 'koishi-database-level'
Expand All @@ -15,7 +15,7 @@ declare module 'koishi-core/dist/database' {
}

interface ScheduleMethods {
createSchedule (time: number, assignee: number, interval: number, command: string, meta: MessageMeta): Promise<Schedule>
createSchedule (time: number, assignee: number, interval: number, command: string, meta: Meta<'message'>): Promise<Schedule>
removeSchedule (id: number): Promise<any>
getSchedule (id: number): Promise<Schedule>
getAllSchedules (assignees?: number[]): Promise<Schedule[]>
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-schedule/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, appMap, Database, CommandConfig, MessageMeta } from 'koishi-core'
import { Context, appMap, Database, CommandConfig, Meta } from 'koishi-core'
import ms from 'ms'
import './database'

Expand All @@ -8,7 +8,7 @@ export interface Schedule {
time: number
interval: number
command: string
meta: MessageMeta
meta: Meta<'message'>
}

function inspectSchedule ({ id, assignee, meta, interval, command, time }: Schedule) {
Expand Down

0 comments on commit c677df4

Please sign in to comment.