Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 18, 2021
2 parents 806bbae + 89b5996 commit 46ea05c
Show file tree
Hide file tree
Showing 78 changed files with 1,880 additions and 571 deletions.
11 changes: 6 additions & 5 deletions build/dtsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function bundle(path: string) {
const importMap: Record<string, Record<string, string>> = {}
const namespaceMap: Record<string, string> = {}

let prolog = '', cap: RegExpExecArray
let prolog = '', epilog = '', cap: RegExpExecArray
let content = await fs.readFile(entry, 'utf8')
content = content.split(EOL).filter((line) => {
if (cap = /^ {4}import \* as (.+) from ["'](.+)["'];$/.exec(line)) {
Expand All @@ -59,6 +59,8 @@ async function bundle(path: string) {
}
} else if (line.startsWith('///')) {
prolog += line + EOL
} else if (line.startsWith(' export default ')) {
epilog = line.trimStart() + EOL
} else {
return true
}
Expand All @@ -85,8 +87,9 @@ async function bundle(path: string) {
if (identifier) return `declare namespace ${identifier} {`
return ''
})
.replace(/^( {4})((module|class|namespace) .+ \{)$/gm, (_, $1, $2) => `${$1}declare ${$2}`)
.replace(/\r?\n}/g, '')
.replace(/^ {4}/gm, ''))
.replace(/^ {4}/gm, '') + epilog)
}

async function bundleAll(names: readonly string[]) {
Expand All @@ -95,15 +98,13 @@ async function bundleAll(names: readonly string[]) {
}
}

const targets = ['koishi-utils', 'koishi-core']
const databases = ['mongo', 'mysql']
const targets = ['koishi-utils', 'koishi-core', 'plugin-mysql', 'plugin-mongo']
const corePlugins = ['common', 'eval', 'puppeteer', 'teach']

function precedence(name: string) {
if (name.startsWith('adapter')) return 1
if (name.startsWith('koishi')) return 5
const plugin = name.slice(7)
if (databases.includes(plugin)) return 2
if (corePlugins.includes(plugin)) return 3
return 4
}
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-discord/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-adapter-discord",
"description": "Discord adapter for Koishi",
"version": "1.0.3",
"version": "1.0.4",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand All @@ -28,7 +28,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.2.0"
"koishi-core": "^3.2.1"
},
"devDependencies": {
"@types/ws": "^7.4.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class DiscordBot extends Bot<'discord'> {
})
sentMessageId = r.id
}
if (type === 'image' || type === 'video') {
if (type === 'image' || type === 'video' && data.url) {
if (data.url.startsWith('http://') || data.url.startsWith('https://')) {
const a = await axios({
url: data.url,
Expand Down
7 changes: 7 additions & 0 deletions packages/adapter-discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Adapter } from 'koishi-core'
import { AxiosRequestConfig } from 'axios'
import { DiscordBot } from './bot'
import WsClient from './ws'
import { Embed } from './types'

export * from './bot'
export * as dc from './types'
Expand All @@ -15,6 +16,12 @@ declare module 'koishi-core' {
discord?: DiscordOptions
}

interface Session {
discord?: {
embeds: Embed[]
}
}

namespace Bot {
interface Platforms {
discord: DiscordBot
Expand Down
6 changes: 4 additions & 2 deletions packages/adapter-discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const adaptAuthor = (author: DC.Author): AuthorInfo => ({
nickname: author.username,
})

export async function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, session: MessageInfo = {}) {
export async function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, session: Partial<Session.Payload<Session.MessageAction>> = {}) {
if (meta.author) {
session.author = adaptAuthor(meta.author)
session.userId = meta.author.id
Expand Down Expand Up @@ -90,7 +90,9 @@ export async function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, ses
session.content += segment('video', { url: embed.video.url, proxy_url: embed.video.proxy_url })
}
}
session.content = meta.embeds.map(v => segment('embed', { data: JSON.stringify(v) })).join('') + session.content
session.discord = {
embeds: meta.embeds
}
return session
}

Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-kaiheila/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.2.0"
"koishi-core": "^3.2.1"
},
"devDependencies": {
"koishi-test-utils": "^6.0.0-beta.10"
},
"dependencies": {
"axios": "^0.21.1",
"koishi-utils": "^4.0.1",
"koishi-utils": "^4.0.2",
"ws": "^7.4.4"
}
}
4 changes: 2 additions & 2 deletions packages/adapter-onebot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.2.0"
"koishi-core": "^3.2.1"
},
"devDependencies": {
"@types/ws": "^7.4.0",
Expand All @@ -36,7 +36,7 @@
},
"dependencies": {
"axios": "^0.21.1",
"koishi-utils": "^4.0.1",
"koishi-utils": "^4.0.2",
"ws": "^7.4.4"
}
}
4 changes: 2 additions & 2 deletions packages/adapter-telegram/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.2.0"
"koishi-core": "^3.2.1"
},
"devDependencies": {
"koishi-test-utils": "^6.0.0-beta.10"
},
"dependencies": {
"axios": "^0.21.1",
"form-data": "^4.0.0",
"koishi-utils": "^4.0.1"
"koishi-utils": "^4.0.2"
}
}
4 changes: 2 additions & 2 deletions packages/adapter-tomon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.2.0"
"koishi-core": "^3.2.1"
},
"devDependencies": {
"@types/pako": "^1.0.1",
"koishi-test-utils": "^6.0.0-beta.10"
},
"dependencies": {
"axios": "^0.21.1",
"koishi-utils": "^4.0.1",
"koishi-utils": "^4.0.2",
"pako": "^2.0.3",
"ws": "^7.4.4"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-core",
"description": "Core features for Koishi",
"version": "3.2.0",
"version": "3.2.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down Expand Up @@ -42,7 +42,7 @@
"fastest-levenshtein": "^1.0.12",
"koa": "^2.13.1",
"koa-bodyparser": "^4.3.0",
"koishi-utils": "^4.0.1",
"koishi-utils": "^4.0.2",
"lru-cache": "^6.0.0"
}
}
9 changes: 8 additions & 1 deletion packages/koishi-core/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger, paramCase, sleep, Time } from 'koishi-utils'
import { Logger, paramCase, remove, sleep, Time } from 'koishi-utils'
import { Session } from './session'
import { App } from './app'
import WebSocket from 'ws'
Expand Down Expand Up @@ -34,6 +34,7 @@ export abstract class Adapter<P extends Platform = Platform> {
const bot = new this.Bot(this, options)
this.bots.push(bot)
this.app.bots.push(bot)
return bot
}

dispatch(session: Session) {
Expand Down Expand Up @@ -254,6 +255,12 @@ export class Bot<P extends Platform> {
}
return messageIds
}

dispose() {
const bot = this as Bot.Instance<P>
remove(this.adapter.bots, bot)
remove(this.app.bots, bot)
}
}

export namespace Bot {
Expand Down
7 changes: 4 additions & 3 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class App extends Context {
if (this.database) {
if (session.subtype === 'group') {
// attach group data
const channelFields = new Set<Channel.Field>(['flag', 'assignee', 'disable'])
const channelFields = new Set<Channel.Field>(['flag', 'assignee'])
this.emit('before-attach-channel', session, channelFields)
const channel = await session.observeChannel(channelFields)

Expand Down Expand Up @@ -346,11 +346,12 @@ export class App extends Context {
return argv
}

private _handleShortcut(content: string, { parsed, quote }: Session) {
private _handleShortcut(content: string, session: Session) {
const { parsed, quote } = session
if (parsed.prefix || quote) return
for (const shortcut of this._shortcuts) {
const { name, fuzzy, command, greedy, prefix, options = {}, args = [] } = shortcut
if (prefix && !parsed.appel) continue
if (prefix && !parsed.appel || !command.context.match(session)) continue
if (typeof name === 'string') {
if (!fuzzy && content !== name || !content.startsWith(name)) continue
const message = content.slice(name.length)
Expand Down
10 changes: 5 additions & 5 deletions packages/koishi-core/src/command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger, coerce, Time, template } from 'koishi-utils'
import { Logger, coerce, Time, template, remove } from 'koishi-utils'
import { Argv, Domain } from './parser'
import { Context, NextFunction } from './context'
import { User, Channel } from './database'
Expand Down Expand Up @@ -245,11 +245,9 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev
}
this.app._shortcuts = this.app._shortcuts.filter(s => s.command !== this)
this._aliases.forEach(name => delete this.app._commandMap[name])
const index = this.app._commands.indexOf(this)
this.app._commands.splice(index, 1)
remove(this.app._commands, this)
if (this.parent) {
const index = this.parent.children.indexOf(this)
this.parent.children.splice(index, 1)
remove(this.parent.children, this)
}
}
}
Expand All @@ -260,6 +258,8 @@ export function getUsageName(command: Command) {

export type ValidationField = 'authority' | 'usage' | 'timers'

Command.channelFields(['disable'])

Command.userFields(({ tokens, command, options = {} }, fields) => {
if (!command) return
const { maxUsage, minInterval, authority } = command.config
Expand Down
18 changes: 5 additions & 13 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger, defineProperty } from 'koishi-utils'
import { Logger, defineProperty, remove } from 'koishi-utils'
import { Command } from './command'
import { Session } from './session'
import { User, Channel, Database } from './database'
Expand Down Expand Up @@ -139,14 +139,6 @@ export class Context {
return this.app.registry.get(this._plugin)
}

private removeDisposable(listener: Disposable) {
const index = this.state.disposables.indexOf(listener)
if (index >= 0) {
this.state.disposables.splice(index, 1)
return true
}
}

addSideEffect(state = this.state) {
while (state && !state.sideEffect) {
state.sideEffect = true
Expand Down Expand Up @@ -208,6 +200,7 @@ export class Context {
...state.disposables.map(dispose => dispose()),
]).finally(() => {
this.app.registry.delete(plugin)
remove(state.parent.children, plugin)
const index = state.parent.children.indexOf(plugin)
if (index >= 0) state.parent.children.splice(index, 1)
this.emit('registry', this.app.registry)
Expand Down Expand Up @@ -292,7 +285,7 @@ export class Context {
return _listener(), () => false
} else if (name === 'before-disconnect') {
this.state.disposables[method](_listener)
return () => this.removeDisposable(_listener)
return () => remove(this.state.disposables, _listener)
} else if (name === 'before-connect') {
// before-connect is side effect
this.addSideEffect()
Expand Down Expand Up @@ -342,7 +335,7 @@ export class Context {
private createTimerDispose(timer: NodeJS.Timeout) {
const dispose = () => {
clearTimeout(timer)
return this.removeDisposable(dispose)
return remove(this.state.disposables, dispose)
}
this.state.disposables.push(dispose)
return dispose
Expand Down Expand Up @@ -502,8 +495,7 @@ Router.prototype.register = function (this: Router, ...args) {
const layer = register.apply(this, args)
const context: Context = this['_koishiContext']
context.state.disposables.push(() => {
const index = this.stack.indexOf(layer)
if (index) this.stack.splice(index, 1)
remove(this.stack, layer)
})
return layer
}
5 changes: 2 additions & 3 deletions packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import LruCache from 'lru-cache'
import { distance } from 'fastest-levenshtein'
import { User, Channel, TableType, Tables } from './database'
import { Command } from './command'
import { contain, observe, Logger, defineProperty, Random, template } from 'koishi-utils'
import { contain, observe, Logger, defineProperty, Random, template, remove } from 'koishi-utils'
import { Argv } from './parser'
import { Middleware, NextFunction } from './context'
import { App } from './app'
Expand Down Expand Up @@ -169,8 +169,7 @@ export class Session<
const hook = () => {
resolve()
clearTimeout(timer)
const index = this._hooks.indexOf(hook)
if (index >= 0) this._hooks.splice(index, 1)
remove(this._hooks, hook)
}
this._hooks.push(hook)
const timer = setTimeout(async () => {
Expand Down
1 change: 0 additions & 1 deletion packages/koishi-core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"compilerOptions": {
"rootDir": "src",
"outFile": "temp/index.d.ts",
"tsBuildInfoFile": "tsconfig.tsbuildinfo"
},
"include": [
"src"
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"dependencies": {
"chai": "^4.3.3",
"chai-as-promised": "^7.1.1",
"koishi-core": "^3.2.0",
"koishi-utils": "^4.0.1"
"koishi-core": "^3.2.1",
"koishi-utils": "^4.0.2"
},
"devDependencies": {
"@types/chai": "^4.2.15",
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-utils",
"description": "Utilities for Koishi",
"version": "4.0.1",
"version": "4.0.2",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down
8 changes: 8 additions & 0 deletions packages/koishi-utils/src/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ export function union<T>(array1: readonly T[], array2: readonly T[]) {
export function deduplicate<T>(array: readonly T[]) {
return [...new Set(array)]
}

export function remove<T>(list: T[], item: T) {
const index = list.indexOf(item)
if (index >= 0) {
list.splice(index, 1)
return true
}
}
Loading

0 comments on commit 46ea05c

Please sign in to comment.