Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 4, 2021
2 parents 6c18929 + ebaf332 commit ec58c0a
Show file tree
Hide file tree
Showing 65 changed files with 811 additions and 382 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ Koishi 原生地支持了多账户与跨平台,同时为这些机器人之间

### 其他官方插件

- [koishi-plugin-chess](https://koishi.js.org/plugins/other/chess) / 下棋
- [koishi-plugin-assets](https://koishi.js.org/plugins/other/assets) / 资源转存
- [koishi-plugin-chess](https://koishi.js.org/plugins/other/chess) / 棋类游戏
- [koishi-plugin-github](https://koishi.js.org/plugins/other/github) / 接入 GitHub
- [koishi-plugin-image-search](https://koishi.js.org/plugins/other/image-search) / 搜图
- [koishi-plugin-image-search](https://koishi.js.org/plugins/other/image-search) / 图片搜索
- [koishi-plugin-puppeteer](https://koishi.js.org/plugins/other/puppeteer) / 网页截图
- [koishi-plugin-rss](https://koishi.js.org/plugins/other/rss) / 订阅 RSS
- [koishi-plugin-schedule](https://koishi.js.org/plugins/other/schedule) / 设置计划任务
- [koishi-plugin-webui](https://koishi.js.org/plugins/other/webui) / 状态监控
<!-- - [koishi-plugin-rss](https://koishi.js.org/plugins/other/rss) / 订阅 RSS -->
- [koishi-plugin-schedule](https://koishi.js.org/plugins/other/schedule) / 计划任务
- [koishi-plugin-tools](https://koishi.js.org/plugins/other/tools) / 实用工具
- [koishi-plugin-webui](https://koishi.js.org/plugins/other/webui) / 网页控制台

## 使用协议

Expand Down
1 change: 1 addition & 0 deletions build/dtsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async function bundle(path: string) {
if (!identifier) line = line.slice(4)
return line
.replace(internalImport, '')
.replace(/import\("index"\)/g, 'import(".")')
.replace(/^((module|class|namespace) .+ \{)$/, (_) => `declare ${_}`)
}
}).filter(line => line).join(EOL)
Expand Down
37 changes: 29 additions & 8 deletions build/publish.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PackageJson, getWorkspaces, spawnAsync, spawnSync } from './utils'
import { cwd, PackageJson, getWorkspaces, spawnAsync, spawnSync } from './utils'
import { gt, prerelease } from 'semver'
import { Octokit } from '@octokit/rest'
import { draft } from './release'
import { writeJson } from 'fs-extra'
import latest from 'latest-version'
import ora from 'ora'

Expand Down Expand Up @@ -40,15 +41,35 @@ if (CI && (GITHUB_REF !== 'refs/heads/master' || GITHUB_EVENT_NAME !== 'push'))
}))
spinner.succeed()

function publish(folder: string, name: string, version: string, tag: string) {
console.log(`publishing ${name}@${version} ...`)
return spawnAsync([
'yarn', 'publish', folder,
'--new-version', version,
'--tag', tag,
])
}

if (Object.keys(bumpMap).length) {
for (const folder in bumpMap) {
const { name, version } = bumpMap[folder]
console.log(`publishing ${name}@${version} ...`)
await spawnAsync([
'yarn', 'publish', folder,
'--new-version', version,
'--tag', prerelease(version) ? 'next' : 'latest',
])
const { name, version, dependencies, devDependencies } = bumpMap[folder]
await publish(folder, name, version, prerelease(version) ? 'next' : 'latest')
if (name === 'koishi-plugin-webui') {
const filename = cwd + '/packages/plugin-webui/package.json'
await writeJson(filename, {
...bumpMap[folder],
version: version + '-dev',
files: ['lib', 'dist', 'client'],
dependencies: Object.fromEntries([
...Object.entries(dependencies),
...Object.entries(devDependencies).filter(([key]) => {
return !key.startsWith('@types') && !key.startsWith('koishi')
}),
]),
}, { spaces: 2 })
await publish(folder, name, version + '-dev', 'dev')
await writeJson(filename, bumpMap[folder])
}
}
}

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.6",
"version": "1.0.7",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand All @@ -28,7 +28,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.6.1"
"koishi-core": "^3.7.0"
},
"devDependencies": {
"@types/ws": "^7.4.0",
Expand Down
154 changes: 132 additions & 22 deletions packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import axios, { Method } from 'axios'
import { Bot, MessageInfo } from 'koishi-core'
import * as DC from './types'
import { DiscordChannel, DiscordMessage, DiscordUser, ExecuteWebhookBody, GuildMember, PartialGuild } from './types'
import { adaptChannel, adaptGroup, adaptMessage, adaptUser } from './utils'
import { readFileSync, existsSync } from 'fs'
import { readFileSync } from 'fs'
import { segment } from 'koishi-utils'
import FormData from 'form-data'
import FileType from 'file-type'
Expand Down Expand Up @@ -38,7 +37,7 @@ export class DiscordBot extends Bot<'discord'> {
return adaptUser(data)
}

private async sendEmbedMessage(requestUrl: string, fileBuffer: Buffer, payload_json: Record<string, any> = {}, fileType?: string) {
private async sendEmbedMessage(requestUrl: string, fileBuffer: Buffer, payload_json: Record<string, any> = {}) {
const fd = new FormData()
const type = await FileType.fromBuffer(fileBuffer)
fd.append('file', fileBuffer, 'file.' + type.ext)
Expand Down Expand Up @@ -98,23 +97,22 @@ export class DiscordBot extends Bot<'discord'> {
sentMessageId = r.id
}
if (type === 'image' || type === 'video' && data.url) {
if (data.url.startsWith('http://') || data.url.startsWith('https://')) {
const a = await axios({
url: data.url,
responseType: 'arraybuffer',
})
const r = await this.sendEmbedMessage(requestUrl, a.data, {
if (data.url.startsWith('file://')) {
const r = await this.sendEmbedMessage(requestUrl, readFileSync(data.url.slice(7)), {
...addition,
})
sentMessageId = r.id
} else if (data.url.startsWith('base64://')) {
const a = Buffer.from(data.url.substring('base64://'.length), 'base64')
const a = Buffer.from(data.url.slice(9), 'base64')
const r = await this.sendEmbedMessage(requestUrl, a, {
...addition,
})
sentMessageId = r.id
} else if (existsSync(data.url)) {
const r = await this.sendEmbedMessage(requestUrl, readFileSync(data.url), {
} else {
const a = await axios.get(data.url, {
responseType: 'arraybuffer',
})
const r = await this.sendEmbedMessage(requestUrl, a.data, {
...addition,
})
sentMessageId = r.id
Expand Down Expand Up @@ -158,7 +156,7 @@ export class DiscordBot extends Bot<'discord'> {
}

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

async getMessage(channelId: string, messageId: string): Promise<MessageInfo> {
Expand All @@ -181,31 +179,143 @@ export class DiscordBot extends Bot<'discord'> {
}

async getUser(userId: string) {
const data = await this.request<DiscordUser>('GET', `/users/${userId}`)
const data = await this.request<DC.DiscordUser>('GET', `/users/${userId}`)
return adaptUser(data)
}

async getGroupList() {
const data = await this.request<PartialGuild[]>('GET', '/users/@me/guilds')
return data.map(adaptGroup)
}

async getGroupMemberList(guildId: string) {
const data = await this.request<GuildMember[]>('GET', `/guilds/${guildId}/members`)
const data = await this.$listGuildMembers(guildId)
return data.map(v => adaptUser(v.user))
}

async getChannel(channelId: string) {
const data = await this.request<DiscordChannel>('GET', `/channels/${channelId}`)
const data = await this.$getChannel(channelId)
return adaptChannel(data)
}

async executeWebhook(id: string, token: string, data: ExecuteWebhookBody, wait = false): Promise<string> {
async $executeWebhook(id: string, token: string, data: DC.ExecuteWebhookBody, wait = false): Promise<string> {
const chain = segment.parse(data.content)
if (chain.filter(v => v.type === 'image').length > 10) {
throw new Error('Up to 10 embed objects')
}

return await this.sendFullMessage(`/webhooks/${id}/${token}?wait=${wait}`, data.content, data)
}

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

async getGroupMember(groupId: string, userId: string) {
const member = await this.$getGuildMember(groupId, userId)
return {
...adaptUser(member.user),
nickname: member.nick,
}
}

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

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

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

async $getRoleMembers(guildId: string, roleId: string) {
let members: DC.GuildMember[] = []
let after = '0'
while (true) {
const data = await this.$listGuildMembers(guildId, 1000, after)
members = members.concat(data)
if (data.length) {
after = data[data.length - 1].user.id
} else {
break
}
}
return members.filter(v => v.roles.includes(roleId))
}

async $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) {
return this.request('PUT', `/guilds/${guildId}/members/${userId}/roles/${roleId}`)
}

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

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

async $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) {
return this.request('PATCH', `/guilds/${guildId}`, data)
}

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

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

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

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

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

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

async getGroup(groupId: string) {
const data = await this.$getGuild(groupId)
return adaptGroup(data)
}

async $getUserGuilds() {
return this.request<DC.PartialGuild[]>('GET', '/users/@me/guilds')
}

async getGroupList() {
const data = await this.$getUserGuilds()
return data.map(v => adaptGroup(v))
}

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

async getChannelList(groupId: string) {
const data = await this.$getGuildChannels(groupId)
return data.map(v => adaptChannel(v))
}
}
Loading

0 comments on commit ec58c0a

Please sign in to comment.