Last active
February 3, 2024 16:24
-
-
Save flazepe/b8deffbc728ce917deff5549b5be7a1d to your computer and use it in GitHub Desktop.
2 ways to send a Discord voice message using a bot token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFileSync, statSync } from "fs"; | |
import { getAudioDurationInSeconds } from "get-audio-duration"; | |
const botToken = "Njc1NTM5NTEyOTEwMTUxNjkx.GCAq4j.VaRQAFI6hBrgvcVPi_wY4zCzT4G-2D60h5mw7g", | |
channelID = "732171142344736818", | |
audioPath = "path/to/audio/file"; | |
const body = new FormData(); | |
body.append("files[0]", new Blob([readFileSync(audioPath)]), "voice-message.ogg"); | |
body.append( | |
"payload_json", | |
JSON.stringify({ | |
attachments: [ | |
{ | |
id: "0", | |
filename: "voice-message.ogg", | |
duration_secs: await getAudioDurationInSeconds(audioPath), | |
waveform: "" | |
} | |
], | |
flags: 1 << 13 // Voice message flag | |
}) | |
); | |
// Send the voice message | |
await fetch(`https://discord.com/api/v10/channels/${channelID}/messages`, { | |
method: "POST", | |
headers: { authorization: `Bot ${botToken}` }, | |
body | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFileSync, statSync } from "fs"; | |
import { getAudioDurationInSeconds } from "get-audio-duration"; | |
const botToken = "Njc1NTM5NTEyOTEwMTUxNjkx.GCAq4j.VaRQAFI6hBrgvcVPi_wY4zCzT4G-2D60h5mw7g", | |
channelID = "732171142344736818", | |
audioPath = "path/to/audio/file"; | |
// Create an attachment for uploading | |
const { | |
attachments: [{ upload_url, upload_filename }] | |
} = await ( | |
await fetch(`https://discord.com/api/v10/channels/${channelID}/attachments`, { | |
method: "POST", | |
headers: { | |
"content-type": "application/json", | |
authorization: `Bot ${botToken}` | |
}, | |
body: JSON.stringify({ | |
files: [ | |
{ | |
id: "42069", | |
filename: "voice-message.ogg", | |
file_size: statSync(audioPath).size | |
} | |
] | |
}) | |
}) | |
).json(); | |
// Upload our file | |
await fetch(upload_url, { method: "PUT", body: readFileSync(audioPath) }); | |
// Send the voice message | |
await fetch(`https://discord.com/api/v10/channels/${channelID}/messages`, { | |
method: "POST", | |
headers: { | |
"content-type": "application/json", | |
authorization: `Bot ${botToken}` | |
}, | |
body: JSON.stringify({ | |
attachments: [ | |
{ | |
id: "72786", | |
uploaded_filename: upload_filename, | |
filename: "voice-message.ogg", | |
duration_secs: await getAudioDurationInSeconds(audioPath), | |
waveform: "" | |
} | |
], | |
flags: 1 << 13 // Voice message flag | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment