Skip to content

Instantly share code, notes, and snippets.

@supernovaplus
Created November 2, 2021 16:56
Show Gist options
  • Save supernovaplus/acb2d3be312b94b9ef6692b01a936f44 to your computer and use it in GitHub Desktop.
Save supernovaplus/acb2d3be312b94b9ef6692b01a936f44 to your computer and use it in GitHub Desktop.
discord api bot post files js node
const axios = require("axios");
const fs = require("fs");
const FormData = require("form-data");
const wait = (ms = 1000) => new Promise(resolve => setTimeout(resolve, ms));
const channelId = "---";
const botToken = "---";
const filesInDownloads = fs.readdirSync("./downloaded");
console.log(`Files: ${filesInDownloads.length}`);
const discordPostFiles = (filepath, filename) => {
const formData = new FormData();
formData.append("file", fs.readFileSync(filepath), { filename });
return axios({
method: 'post',
url: `https://canary.discord.com/api/v9/channels/${channelId}/messages`,
headers: {
...formData.getHeaders(),
'Authorization': `Bot ${botToken}`
},
data: formData
}).then(({data}) => {
console.log(data)
return data;
}).catch(({response:{data}}) => {
console.log(data)
return null;
})
}
;(async () => {
for (let i = 0; i < filesInDownloads.length; i++) {
const data = await discordPostFiles("./downloaded/" + filesInDownloads[i], filesInDownloads[i]);
if(data?.id){
fs.unlinkSync("./downloaded/" + filesInDownloads[i])
console.log(`${filesInDownloads[i]} => successs`)
}else{
console.log(data)
console.log(`${filesInDownloads[i]} => error`)
}
await wait(2000);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment