Skip to content

Commit

Permalink
feat(discord): support sending audio (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX authored Aug 22, 2021
1 parent 6d58002 commit 5942412
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/adapter-discord/src/sender.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -27,10 +28,11 @@ export class Sender {
}
}

async sendEmbed(fileBuffer: Buffer, payload_json: Record<string, any> = {}) {
async sendEmbed(fileBuffer: Buffer, payload_json: Record<string, any> = {}, 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())
}
Expand All @@ -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 () => {
Expand All @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -135,6 +139,12 @@ export class Sender {
...addition,
embeds: [{ ...data }],
})
} else if (type === 'record'){
await this.sendAsset('file', data, {
...addition,
content: textBuffer.trim(),
})
textBuffer = ''
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/adapter-discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 5942412

Please sign in to comment.