From 594241227423148284e4718f59df99eefe7decdc Mon Sep 17 00:00:00 2001 From: _LittleC_ Date: Mon, 23 Aug 2021 01:27:59 +0800 Subject: [PATCH] feat(discord): support sending audio (#353) --- packages/adapter-discord/src/sender.ts | 22 ++++++++++++++++------ packages/adapter-discord/src/utils.ts | 8 ++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/packages/adapter-discord/src/sender.ts b/packages/adapter-discord/src/sender.ts index 9885c29826..e8948aff4b 100644 --- a/packages/adapter-discord/src/sender.ts +++ b/packages/adapter-discord/src/sender.ts @@ -1,4 +1,5 @@ import { readFileSync } from 'fs' +import { basename } from 'path' import FormData from 'form-data' import FileType from 'file-type' import AggregateError from 'es-aggregate-error' @@ -27,10 +28,11 @@ export class Sender { } } - async sendEmbed(fileBuffer: Buffer, payload_json: Record = {}) { + async sendEmbed(fileBuffer: Buffer, payload_json: Record = {}, filename: string) { const fd = new FormData() const type = await FileType.fromBuffer(fileBuffer) - fd.append('file', fileBuffer, 'file.' + type.ext) + filename ||= 'file.' + type.ext + fd.append('file', fileBuffer, filename) fd.append('payload_json', JSON.stringify(payload_json)) return this.post(fd, fd.getHeaders()) } @@ -48,10 +50,11 @@ export class Sender { } if (data.url.startsWith('file://')) { - return this.sendEmbed(readFileSync(data.url.slice(8)), addition) + const filename = basename(data.url.slice(7)) + return this.sendEmbed(readFileSync(data.url.slice(7)), addition, data.file || filename) } else if (data.url.startsWith('base64://')) { const a = Buffer.from(data.url.slice(9), 'base64') - return await this.sendEmbed(a, addition) + return await this.sendEmbed(a, addition, data.file) } const sendDirect = async () => { @@ -62,6 +65,7 @@ export class Sender { } const sendDownload = async () => { + const filename = basename(data.url) const a = await axios.get(data.url, { ...axiosConfig, ...discord.axiosConfig, @@ -70,11 +74,11 @@ export class Sender { accept: type + '/*', }, }) - return this.sendEmbed(a.data, addition) + return this.sendEmbed(a.data, addition, data.file || filename) } const mode = data.mode as HandleExternalAsset || discord.handleExternalAsset - if (mode === 'download' || discord.handleMixedContent === 'attach' && addition.content) { + if (mode === 'download' || discord.handleMixedContent === 'attach' && addition.content || type === 'file') { return sendDownload() } else if (mode === 'direct') { return sendDirect() @@ -135,6 +139,12 @@ export class Sender { ...addition, embeds: [{ ...data }], }) + } else if (type === 'record'){ + await this.sendAsset('file', data, { + ...addition, + content: textBuffer.trim(), + }) + textBuffer = '' } } diff --git a/packages/adapter-discord/src/utils.ts b/packages/adapter-discord/src/utils.ts index a90abda689..c9cace45e7 100644 --- a/packages/adapter-discord/src/utils.ts +++ b/packages/adapter-discord/src/utils.ts @@ -82,9 +82,16 @@ export function adaptMessage(bot: DiscordBot, meta: DC.Message, session: Partial proxy_url: v.proxy_url, file: v.filename, }) + } else if (v.content_type?.startsWith('audio/')) { + return segment('record', { + url: v.url, + proxy_url: v.proxy_url, + file: v.filename, + }) } else { return segment('file', { url: v.url, + proxy_url: v.proxy_url, file: v.filename, }) } @@ -158,6 +165,7 @@ export async function adaptSession(bot: DiscordBot, input: DC.Payload) { if (session.userId === bot.selfId) return } else if (input.t === 'MESSAGE_DELETE') { session.type = 'message-deleted' + session.messageId = input.d.id prepareMessageSession(session, input.d) } else if (input.t === 'MESSAGE_REACTION_ADD') { session.type = 'reaction-added'