Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(discord): support sending voice messages #264

Merged
merged 5 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions adapters/discord/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE
const { filename, data, mime } = await this.bot.ctx.http.file(attrs.src || attrs.url, attrs)
const form = new FormData()
const value = new Blob([data], { type: mime })
form.append('file', value, attrs.file || filename)
// https://discord.com/developers/docs/reference#uploading-files
form.append('files[0]', value, attrs.file || filename)
form.append('payload_json', JSON.stringify(payload))
return this.post(form)
}
Expand Down Expand Up @@ -300,7 +301,15 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE
} else if (type === 'audio') {
await this.sendAsset('file', attrs, {
...this.addition,
content: this.buffer.trim(),
content: '',
attachments: [
{
waveform: '', // base64 encoded bytearray representing a sampled waveform
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the waveform be required here to be calculated from the wave ?

From Discord's doc:

The waveform is intended to be a preview of the entire voice message, with 1 byte per datapoint encoded in base64. Clients sample the recording at most once per 100 milliseconds, but will downsample so that no more than 256 datapoints are in the waveform.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also possible to send without calculation.

id: 0,
duration_secs: attrs.duration ?? 0,
},
],
flags: Message.Flag.IS_VOICE_MESSAGE,
})
this.buffer = ''
} else if (type === 'author') {
Expand Down
4 changes: 4 additions & 0 deletions adapters/discord/src/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ export namespace Message {
LOADING = 1 << 7,
/** this message failed to mention some roles and add their members to the thread */
FAILED_TO_MENTION_SOME_ROLES_IN_THREAD = 1 << 8,
/** this message will not trigger push and desktop notifications */
SUPPRESS_NOTIFICATIONS = 1 << 12,
/** this message is a voice message */
IS_VOICE_MESSAGE = 1 << 13,
}

/** https://discord.com/developers/docs/resources/channel#message-reference-object-message-reference-structure */
Expand Down
Loading