Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 28, 2021
2 parents 33f04ff + e1ac882 commit 0c06208
Show file tree
Hide file tree
Showing 67 changed files with 699 additions and 460 deletions.
29 changes: 26 additions & 3 deletions build/dtsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async function bundle(path: string) {
const moduleRE = `"(${files.join('|')})"`
const internalImport = new RegExp('import\\(' + moduleRE + '\\)\\.', 'g')
const internalExport = new RegExp('^ {4}export .+ from ' + moduleRE + ';$')
const internalInject = new RegExp('^declare module ' + moduleRE + ' {$')
const importMap: Record<string, Record<string, string>> = {}
const namespaceMap: Record<string, string> = {}

Expand Down Expand Up @@ -76,7 +77,7 @@ async function bundle(path: string) {
}
}).map((line) => {
if (cap = /^declare module ["'](.+)["'] \{( \})?$/.exec(line)) {
if (cap[2]) return
if (cap[2]) return ''
identifier = namespaceMap[cap[1]]
return identifier ? `declare namespace ${identifier} {` : ''
} else if (line === '}') {
Expand All @@ -87,6 +88,18 @@ async function bundle(path: string) {
.replace(internalImport, '')
.replace(/import\("index"\)/g, 'import(".")')
.replace(/^((module|class|namespace) .+ \{)$/, (_) => `declare ${_}`)
} else {
return ''
}
}).map((line) => {
if (cap = internalInject.exec(line)) {
identifier = '@internal'
return ''
} else if (line === '}') {
return identifier ? identifier = '' : '}'
} else {
if (identifier) line = line.slice(4)
return line.replace(/^((class|namespace) .+ \{)$/, (_) => `export ${_}`)
}
}).filter(line => line).join(EOL)

Expand All @@ -112,8 +125,18 @@ async function bundleAll(names: readonly string[]) {
}
}

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

const corePlugins = ['eval', 'puppeteer']

function precedence(name: string) {
if (name.startsWith('adapter')) return 1
Expand Down
9 changes: 8 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ module.exports = {
'/plugins/eval/sandbox.md',
'/plugins/eval/config.md',
],
}, {
}, ...process.env.NODE_ENV === 'production' ? [] : [{
text: '冒险系统 (Adventure)',
isGroup: true,
children: [
'/plugins/adventure/index.md',
'/plugins/adventure/events.md',
],
}], {
text: '其他官方插件',
isGroup: true,
children: [
Expand Down
19 changes: 19 additions & 0 deletions docs/plugins/adventure/events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
sidebarDepth: 2
---

# 事件

## Event API

## 扩展事件

### adventure/lose(itemMap, session, hints)

在剧情中失去物品时触发。因售卖、物品数量溢出所导致的物品失去不计。

### adventure/gain(itemMap, session, hints)

在剧情中获得物品时触发。因抽卡、购买所导致的物品获得不计。


14 changes: 14 additions & 0 deletions docs/plugins/adventure/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
sidebarDepth: 2
---

# 基本用法

::: tip 提示
本章介绍的功能都由 koishi-plugin-adventure 插件提供。
:::

::: danger 注意
koishi-plugin-adventure 现处于早期开发中,API 随时可能发生破坏性变更,同时也不保证此文档的即时性。
:::

6 changes: 3 additions & 3 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.2.0",
"version": "1.3.0",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand All @@ -28,11 +28,11 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.12.0"
"koishi-core": "^3.12.1"
},
"devDependencies": {
"@types/ws": "^7.4.2",
"koishi-test-utils": "^6.0.0"
"koishi-test-utils": "^6.0.1"
},
"dependencies": {
"axios": "^0.21.1",
Expand Down
103 changes: 80 additions & 23 deletions packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { segment } from 'koishi-utils'
import FormData from 'form-data'
import FileType from 'file-type'

export type HandleExternalAssets = 'auto' | 'download' | 'direct'

export class SenderError extends Error {
constructor(url: string, data: any, selfId: string) {
super(`Error when trying to request ${url}, data: ${JSON.stringify(data)}`)
Expand Down Expand Up @@ -126,16 +128,63 @@ export class DiscordBot extends Bot<'discord'> {
})
sentMessageId = r.id
} else {
try {
const { axiosConfig, discord = {} } = this.app.options
const sendMode =
data.mode as HandleExternalAssets || // define in segment
discord.handleExternalAssets || // define in app options
'auto' // default

// Utils
async function sendDownload() {
const a = await axios.get(data.url, {
...axiosConfig,
...discord.axiosConfig,
responseType: 'arraybuffer',
headers: {
accept: 'image/*',
},
})
const r = await that.sendEmbedMessage(requestUrl, a.data, {
...addition,
})
const r = await this.sendEmbedMessage(requestUrl, a.data, {
sentMessageId = r.id
}
async function sendDirect() {
const r = await that.request('POST', requestUrl, {
content: data.url,
...addition,
})
sentMessageId = r.id
} catch (e) {
throw new SenderError(data.url, data, this.selfId)
}

if (sendMode === 'direct') {
// send url directly
await sendDirect()
} else if (sendMode === 'download') {
// download send
await sendDownload()
} else {
// auto mode
await axios
.head(data.url, {
...axiosConfig,
...discord.axiosConfig,
headers: {
accept: 'image/*',
},
})
.then(async ({ headers }) => {
if (headers['content-type'].includes('image')) {
await sendDirect()
} else {
await sendDownload()
}
}, async () => {
await sendDownload()
})
.catch(() => {
throw new SenderError(data.url, data, this.selfId)
})
}
}
}
Expand All @@ -161,7 +210,7 @@ export class DiscordBot extends Bot<'discord'> {
return session.messageId
}

async deleteMessage(channelId: string, messageId: string) {
deleteMessage(channelId: string, messageId: string) {
return this.request('DELETE', `/channels/${channelId}/messages/${messageId}`)
}

Expand All @@ -176,7 +225,7 @@ export class DiscordBot extends Bot<'discord'> {
})
}

async $getMessage(channelId: string, messageId: string) {
$getMessage(channelId: string, messageId: string) {
return this.request<DC.Message>('GET', `/channels/${channelId}/messages/${messageId}`)
}

Expand Down Expand Up @@ -226,7 +275,7 @@ export class DiscordBot extends Bot<'discord'> {
return await this.sendFullMessage(`/webhooks/${id}/${token}?wait=${wait}`, data.content, data)
}

async $getGuildMember(guildId: string, userId: string) {
$getGuildMember(guildId: string, userId: string) {
return this.request<DC.GuildMember>('GET', `/guilds/${guildId}/members/${userId}`)
}

Expand All @@ -238,15 +287,15 @@ export class DiscordBot extends Bot<'discord'> {
}
}

async $getGuildRoles(guildId: string) {
$getGuildRoles(guildId: string) {
return this.request<DC.Role[]>('GET', `/guilds/${guildId}/roles`)
}

async $getChannel(channelId: string) {
$getChannel(channelId: string) {
return this.request<DC.Channel>('GET', `/channels/${channelId}`)
}

async $listGuildMembers(guildId: string, limit?: number, after?: string) {
$listGuildMembers(guildId: string, limit?: number, after?: string) {
return this.request<DC.GuildMember[]>('GET', `/guilds/${guildId}/members?limit=${limit || 1000}&after=${after || '0'}`)
}

Expand All @@ -265,58 +314,66 @@ export class DiscordBot extends Bot<'discord'> {
return members.filter(v => v.roles.includes(roleId))
}

async $modifyGuildMember(guildId: string, userId: string, data: Partial<DC.ModifyGuildMember>) {
$modifyGuildMember(guildId: string, userId: string, data: Partial<DC.ModifyGuildMember>) {
return this.request('PATCH', `/guilds/${guildId}/members/${userId}`, data)
}

async $addGuildMemberRole(guildId: string, userId: string, roleId: string) {
$setGroupCard(guildId: string, userId: string, nick: string) {
return this.$modifyGuildMember(guildId, userId, { nick })
}

$addGuildMemberRole(guildId: string, userId: string, roleId: string) {
return this.request('PUT', `/guilds/${guildId}/members/${userId}/roles/${roleId}`)
}

async $removeGuildMemberRole(guildId: string, userId: string, roleId: string) {
$removeGuildMemberRole(guildId: string, userId: string, roleId: string) {
return this.request('DELETE', `/guilds/${guildId}/members/${userId}/roles/${roleId}`)
}

async $createGuildRole(guildId: string, data: DC.GuildRoleBody) {
$createGuildRole(guildId: string, data: DC.GuildRoleBody) {
return this.request('POST', `/guilds/${guildId}/roles`, data)
}

async $modifyGuildRole(guildId: string, roleId: string, data: Partial<DC.GuildRoleBody>) {
$modifyGuildRole(guildId: string, roleId: string, data: Partial<DC.GuildRoleBody>) {
return this.request('PATCH', `/guilds/${guildId}/roles/${roleId}`, data)
}

async $modifyGuild(guildId: string, data: DC.GuildBody) {
$modifyGuild(guildId: string, data: DC.GuildModify) {
return this.request('PATCH', `/guilds/${guildId}`, data)
}

async $createWebhook(channelId: string, data: {
$setGroupName(guildId: string, name: string) {
return this.$modifyGuild(guildId, { name })
}

$createWebhook(channelId: string, data: {
name: string;
avatar?: string
}) {
return this.request('POST', `/channels/${channelId}/webhooks`, data)
}

async $modifyWebhook(webhookId: string, data: {
$modifyWebhook(webhookId: string, data: {
name?: string;
avatar?: string
channel_id?: string
}) {
return this.request('PATCH', `/webhooks/${webhookId}`, data)
}

async $getChannelWebhooks(channelId: string) {
$getChannelWebhooks(channelId: string) {
return this.request<DC.Webhook[]>('GET', `/channels/${channelId}/webhooks`)
}

async $getGuildWebhooks(guildId: string) {
$getGuildWebhooks(guildId: string) {
return this.request<DC.Webhook[]>('GET', `/guilds/${guildId}/webhooks`)
}

async $modifyChannel(channelId, data: Partial<DC.ModifyGuild>) {
$modifyChannel(channelId: string, data: DC.ModifyChannel) {
return this.request('PATCH', `/channels/${channelId}`, data)
}

async $getGuild(guildId: string) {
$getGuild(guildId: string) {
return this.request<DC.Guild>('GET', `/guilds/${guildId}`)
}

Expand All @@ -334,7 +391,7 @@ export class DiscordBot extends Bot<'discord'> {
return data.map(v => adaptGroup(v))
}

async $getGuildChannels(guildId: string) {
$getGuildChannels(guildId: string) {
return this.request<DC.Channel[]>('GET', `/guilds/${guildId}/channels`)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/adapter-discord/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Adapter } from 'koishi-core'
import { AxiosRequestConfig } from 'axios'
import { DiscordBot } from './bot'
import { DiscordBot, HandleExternalAssets } from './bot'
import WsClient from './ws'
import * as DC from './types'
export * from './bot'

interface DiscordOptions extends Adapter.WsClientOptions {
endpoint?: string
axiosConfig?: AxiosRequestConfig
handleExternalAssets?: HandleExternalAssets
}

declare module 'koishi-core' {
Expand Down
Loading

0 comments on commit 0c06208

Please sign in to comment.