Created
November 2, 2021 16:56
-
-
Save supernovaplus/acb2d3be312b94b9ef6692b01a936f44 to your computer and use it in GitHub Desktop.
discord api bot post files js node
This file contains hidden or 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
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