From a1f116aad4f1e4030d037d0c0c8fa602cdbfdfc3 Mon Sep 17 00:00:00 2001 From: Dragon-Fish <824399619@qq.com> Date: Sun, 30 May 2021 01:21:28 +0800 Subject: [PATCH 1/9] close #280 --- packages/adapter-discord/src/bot.ts | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/adapter-discord/src/bot.ts b/packages/adapter-discord/src/bot.ts index 8ad80b7742..44e1478e93 100644 --- a/packages/adapter-discord/src/bot.ts +++ b/packages/adapter-discord/src/bot.ts @@ -126,17 +126,38 @@ export class DiscordBot extends Bot<'discord'> { }) sentMessageId = r.id } else { - try { + const { axiosConfig, discord = {} } = this.app.options + async function downloadSend() { const a = await axios.get(data.url, { + ...axiosConfig, + ...discord.axiosConfig, responseType: 'arraybuffer', }) - const r = await this.sendEmbedMessage(requestUrl, a.data, { + const r = await that.sendEmbedMessage(requestUrl, a.data, { ...addition, }) sentMessageId = r.id - } catch (e) { - throw new SenderError(data.url, data, this.selfId) } + axios.head(data.url, { + ...axiosConfig, + ...discord.axiosConfig, + }).then(async ({ headers }) => { + if (headers?.['content-type']?.includes('image')) { + const r = await this.request('POST', requestUrl, { + content: data.url, + ...addition, + }) + sentMessageId = r.id + } else { + await downloadSend() + } + }, async () => { + try { + await downloadSend() + } catch (e) { + throw new SenderError(data.url, data, this.selfId) + } + }) } } } From 75303eddb75bdf3108c5146e4ac95b75309baa3f Mon Sep 17 00:00:00 2001 From: Dragon-Fish <824399619@qq.com> Date: Tue, 22 Jun 2021 21:43:31 +0800 Subject: [PATCH 2/9] support define in app options or segment --- packages/adapter-discord/src/bot.ts | 59 ++++++++++++++++++--------- packages/adapter-discord/src/index.ts | 1 + 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/packages/adapter-discord/src/bot.ts b/packages/adapter-discord/src/bot.ts index 44e1478e93..4b92b7cd01 100644 --- a/packages/adapter-discord/src/bot.ts +++ b/packages/adapter-discord/src/bot.ts @@ -127,6 +127,12 @@ export class DiscordBot extends Bot<'discord'> { sentMessageId = r.id } else { const { axiosConfig, discord = {} } = this.app.options + const sendMode: 'auto' | 'download' | 'direct' = + data.mode || // define in segment + discord.urlImageSendMode || // define in app options + 'auto' // default + + // Utils async function downloadSend() { const a = await axios.get(data.url, { ...axiosConfig, @@ -138,26 +144,41 @@ export class DiscordBot extends Bot<'discord'> { }) sentMessageId = r.id } - axios.head(data.url, { - ...axiosConfig, - ...discord.axiosConfig, - }).then(async ({ headers }) => { - if (headers?.['content-type']?.includes('image')) { - const r = await this.request('POST', requestUrl, { - content: data.url, - ...addition, + async function directSend() { + const r = await this.request('POST', requestUrl, { + content: data.url, + ...addition, + }) + sentMessageId = r.id + } + + if (sendMode === 'direct') { + // send url directly + await directSend() + } else if (sendMode === 'download') { + // download send + await downloadSend() + } else { + // auto mode + axios.head(data.url, { + ...axiosConfig, + ...discord.axiosConfig, + }) + .then(async ({ headers }) => { + if (headers?.['content-type']?.includes('image')) { + directSend() + } else { + await downloadSend() + } }) - sentMessageId = r.id - } else { - await downloadSend() - } - }, async () => { - try { - await downloadSend() - } catch (e) { - throw new SenderError(data.url, data, this.selfId) - } - }) + .catch(async () => { + try { + await downloadSend() + } catch (e) { + throw new SenderError(data.url, data, this.selfId) + } + }) + } } } } diff --git a/packages/adapter-discord/src/index.ts b/packages/adapter-discord/src/index.ts index e13b6c746f..c22d67e5ed 100644 --- a/packages/adapter-discord/src/index.ts +++ b/packages/adapter-discord/src/index.ts @@ -8,6 +8,7 @@ export * from './bot' interface DiscordOptions extends Adapter.WsClientOptions { endpoint?: string axiosConfig?: AxiosRequestConfig + urlImageSendMode?: 'auto' | 'download' | 'direct' } declare module 'koishi-core' { From 0beea5869aee59169b27bec29d6b928eade4e38d Mon Sep 17 00:00:00 2001 From: Dragon-Fish <824399619@qq.com> Date: Tue, 22 Jun 2021 21:59:34 +0800 Subject: [PATCH 3/9] minor fix --- packages/adapter-discord/src/bot.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/adapter-discord/src/bot.ts b/packages/adapter-discord/src/bot.ts index 4b92b7cd01..35e61c790c 100644 --- a/packages/adapter-discord/src/bot.ts +++ b/packages/adapter-discord/src/bot.ts @@ -127,9 +127,10 @@ export class DiscordBot extends Bot<'discord'> { sentMessageId = r.id } else { const { axiosConfig, discord = {} } = this.app.options - const sendMode: 'auto' | 'download' | 'direct' = - data.mode || // define in segment - discord.urlImageSendMode || // define in app options + type SendMode = 'auto' | 'download' | 'direct' | '' + const sendMode: SendMode = + data.mode as SendMode || // define in segment + discord.urlImageSendMode as SendMode || // define in app options 'auto' // default // Utils @@ -145,7 +146,7 @@ export class DiscordBot extends Bot<'discord'> { sentMessageId = r.id } async function directSend() { - const r = await this.request('POST', requestUrl, { + const r = await that.request('POST', requestUrl, { content: data.url, ...addition, }) @@ -166,7 +167,7 @@ export class DiscordBot extends Bot<'discord'> { }) .then(async ({ headers }) => { if (headers?.['content-type']?.includes('image')) { - directSend() + await directSend() } else { await downloadSend() } From 82f3d6922b9ca2ebe38f02b3b941f51ca4dbcb48 Mon Sep 17 00:00:00 2001 From: Dragon-Fish <824399619@qq.com> Date: Tue, 22 Jun 2021 22:24:01 +0800 Subject: [PATCH 4/9] minor improvements --- packages/adapter-discord/src/bot.ts | 6 +++--- packages/adapter-discord/src/index.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/adapter-discord/src/bot.ts b/packages/adapter-discord/src/bot.ts index 35e61c790c..9a05090297 100644 --- a/packages/adapter-discord/src/bot.ts +++ b/packages/adapter-discord/src/bot.ts @@ -127,10 +127,10 @@ export class DiscordBot extends Bot<'discord'> { sentMessageId = r.id } else { const { axiosConfig, discord = {} } = this.app.options - type SendMode = 'auto' | 'download' | 'direct' | '' - const sendMode: SendMode = + type SendMode = 'auto' | 'download' | 'direct' + const sendMode = data.mode as SendMode || // define in segment - discord.urlImageSendMode as SendMode || // define in app options + discord.handleExternalAssets as SendMode || // define in app options 'auto' // default // Utils diff --git a/packages/adapter-discord/src/index.ts b/packages/adapter-discord/src/index.ts index c22d67e5ed..9af7f6725d 100644 --- a/packages/adapter-discord/src/index.ts +++ b/packages/adapter-discord/src/index.ts @@ -8,7 +8,7 @@ export * from './bot' interface DiscordOptions extends Adapter.WsClientOptions { endpoint?: string axiosConfig?: AxiosRequestConfig - urlImageSendMode?: 'auto' | 'download' | 'direct' + handleExternalAssets?: 'auto' | 'download' | 'direct' } declare module 'koishi-core' { From 562c8329bb1a2e4126420dad2a6b9d6258fc43ac Mon Sep 17 00:00:00 2001 From: Dragon-Fish <824399619@qq.com> Date: Tue, 22 Jun 2021 22:47:09 +0800 Subject: [PATCH 5/9] minor improvements --- packages/adapter-discord/src/bot.ts | 20 ++++++++++---------- packages/adapter-discord/src/index.ts | 4 +++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/adapter-discord/src/bot.ts b/packages/adapter-discord/src/bot.ts index 9a05090297..1c92855464 100644 --- a/packages/adapter-discord/src/bot.ts +++ b/packages/adapter-discord/src/bot.ts @@ -8,6 +8,7 @@ import { readFileSync } from 'fs' import { segment } from 'koishi-utils' import FormData from 'form-data' import FileType from 'file-type' +import { HandleExternalAssets } from '.' export class SenderError extends Error { constructor(url: string, data: any, selfId: string) { @@ -127,14 +128,13 @@ export class DiscordBot extends Bot<'discord'> { sentMessageId = r.id } else { const { axiosConfig, discord = {} } = this.app.options - type SendMode = 'auto' | 'download' | 'direct' const sendMode = - data.mode as SendMode || // define in segment - discord.handleExternalAssets as SendMode || // define in app options + data.mode as HandleExternalAssets || // define in segment + discord.handleExternalAssets || // define in app options 'auto' // default // Utils - async function downloadSend() { + async function sendDownload() { const a = await axios.get(data.url, { ...axiosConfig, ...discord.axiosConfig, @@ -145,7 +145,7 @@ export class DiscordBot extends Bot<'discord'> { }) sentMessageId = r.id } - async function directSend() { + async function sendDirect() { const r = await that.request('POST', requestUrl, { content: data.url, ...addition, @@ -155,10 +155,10 @@ export class DiscordBot extends Bot<'discord'> { if (sendMode === 'direct') { // send url directly - await directSend() + await sendDirect() } else if (sendMode === 'download') { // download send - await downloadSend() + await sendDownload() } else { // auto mode axios.head(data.url, { @@ -167,14 +167,14 @@ export class DiscordBot extends Bot<'discord'> { }) .then(async ({ headers }) => { if (headers?.['content-type']?.includes('image')) { - await directSend() + await sendDirect() } else { - await downloadSend() + await sendDownload() } }) .catch(async () => { try { - await downloadSend() + await sendDownload() } catch (e) { throw new SenderError(data.url, data, this.selfId) } diff --git a/packages/adapter-discord/src/index.ts b/packages/adapter-discord/src/index.ts index 9af7f6725d..d69d1f1eae 100644 --- a/packages/adapter-discord/src/index.ts +++ b/packages/adapter-discord/src/index.ts @@ -5,10 +5,12 @@ import WsClient from './ws' import * as DC from './types' export * from './bot' +export type HandleExternalAssets = 'auto' | 'download' | 'direct' + interface DiscordOptions extends Adapter.WsClientOptions { endpoint?: string axiosConfig?: AxiosRequestConfig - handleExternalAssets?: 'auto' | 'download' | 'direct' + handleExternalAssets?: HandleExternalAssets } declare module 'koishi-core' { From 4505e5050d53b2c3aa9011e07815fb9f475f0de4 Mon Sep 17 00:00:00 2001 From: Dragon-Fish <824399619@qq.com> Date: Tue, 22 Jun 2021 22:50:06 +0800 Subject: [PATCH 6/9] minor improvements --- packages/adapter-discord/src/bot.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/adapter-discord/src/bot.ts b/packages/adapter-discord/src/bot.ts index 1c92855464..c55b27e4b2 100644 --- a/packages/adapter-discord/src/bot.ts +++ b/packages/adapter-discord/src/bot.ts @@ -164,21 +164,19 @@ export class DiscordBot extends Bot<'discord'> { axios.head(data.url, { ...axiosConfig, ...discord.axiosConfig, + }).then(async ({ headers }) => { + if (headers['content-type'].includes('image')) { + await sendDirect() + } else { + await sendDownload() + } + }, async () => { + try { + await sendDownload() + } catch (e) { + throw new SenderError(data.url, data, this.selfId) + } }) - .then(async ({ headers }) => { - if (headers?.['content-type']?.includes('image')) { - await sendDirect() - } else { - await sendDownload() - } - }) - .catch(async () => { - try { - await sendDownload() - } catch (e) { - throw new SenderError(data.url, data, this.selfId) - } - }) } } } From 8adf25debedd176629af4e93e27ce6f20b8b1abe Mon Sep 17 00:00:00 2001 From: Dragon-Fish <824399619@qq.com> Date: Wed, 23 Jun 2021 18:43:38 +0800 Subject: [PATCH 7/9] minor improvements --- packages/adapter-discord/src/bot.ts | 32 ++++++++++++++------------- packages/adapter-discord/src/index.ts | 4 +--- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/adapter-discord/src/bot.ts b/packages/adapter-discord/src/bot.ts index c55b27e4b2..8b64a74d42 100644 --- a/packages/adapter-discord/src/bot.ts +++ b/packages/adapter-discord/src/bot.ts @@ -8,7 +8,8 @@ import { readFileSync } from 'fs' import { segment } from 'koishi-utils' import FormData from 'form-data' import FileType from 'file-type' -import { HandleExternalAssets } from '.' + +export type HandleExternalAssets = 'auto' | 'download' | 'direct' export class SenderError extends Error { constructor(url: string, data: any, selfId: string) { @@ -161,22 +162,23 @@ export class DiscordBot extends Bot<'discord'> { await sendDownload() } else { // auto mode - axios.head(data.url, { - ...axiosConfig, - ...discord.axiosConfig, - }).then(async ({ headers }) => { - if (headers['content-type'].includes('image')) { - await sendDirect() - } else { + await axios + .head(data.url, { + ...axiosConfig, + ...discord.axiosConfig, + }) + .then(async ({ headers }) => { + if (headers['content-type'].includes('image')) { + await sendDirect() + } else { + await sendDownload() + } + }, async () => { await sendDownload() - } - }, async () => { - try { - await sendDownload() - } catch (e) { + }) + .catch(() => { throw new SenderError(data.url, data, this.selfId) - } - }) + }) } } } diff --git a/packages/adapter-discord/src/index.ts b/packages/adapter-discord/src/index.ts index d69d1f1eae..0a4dde3684 100644 --- a/packages/adapter-discord/src/index.ts +++ b/packages/adapter-discord/src/index.ts @@ -1,12 +1,10 @@ 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' -export type HandleExternalAssets = 'auto' | 'download' | 'direct' - interface DiscordOptions extends Adapter.WsClientOptions { endpoint?: string axiosConfig?: AxiosRequestConfig From bd606541c30995eadd525adc200094c65bf60c2b Mon Sep 17 00:00:00 2001 From: Dragon-Fish <824399619@qq.com> Date: Wed, 23 Jun 2021 21:49:02 +0800 Subject: [PATCH 8/9] + headers.accept --- packages/adapter-discord/src/bot.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/adapter-discord/src/bot.ts b/packages/adapter-discord/src/bot.ts index 8b64a74d42..69ec8c523f 100644 --- a/packages/adapter-discord/src/bot.ts +++ b/packages/adapter-discord/src/bot.ts @@ -166,6 +166,9 @@ export class DiscordBot extends Bot<'discord'> { .head(data.url, { ...axiosConfig, ...discord.axiosConfig, + headers: { + accept: 'image/*', + }, }) .then(async ({ headers }) => { if (headers['content-type'].includes('image')) { From adc37425a18b479d284b2a4ced49f4c1e067482c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=BA=E6=99=BA=E7=9A=84=E5=B0=8F=E9=B1=BC=E5=90=9B?= <44761872+Dragon-Fish@users.noreply.github.com> Date: Thu, 24 Jun 2021 00:35:14 +0800 Subject: [PATCH 9/9] minor fix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 话说这里是不是掉了一个 header --- packages/adapter-discord/src/bot.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/adapter-discord/src/bot.ts b/packages/adapter-discord/src/bot.ts index 69ec8c523f..19407357f6 100644 --- a/packages/adapter-discord/src/bot.ts +++ b/packages/adapter-discord/src/bot.ts @@ -140,6 +140,9 @@ export class DiscordBot extends Bot<'discord'> { ...axiosConfig, ...discord.axiosConfig, responseType: 'arraybuffer', + headers: { + accept: 'image/*', + }, }) const r = await that.sendEmbedMessage(requestUrl, a.data, { ...addition,