Skip to content

Instantly share code, notes, and snippets.

@supernovaplus
Created November 2, 2021 16:27
Show Gist options
  • Save supernovaplus/dbc516e3e245fd2dd7be44621daef715 to your computer and use it in GitHub Desktop.
Save supernovaplus/dbc516e3e245fd2dd7be44621daef715 to your computer and use it in GitHub Desktop.
const axios = require("axios");
const FormData = require("form-data");
const webhook = "https://discord.com/api/webhooks/....";
//post message to discord channel via webhook
module.exports = post_discord_log = async(message = "") => {
if(!message || typeof message !== "string"){
return Promise.resolve(false);
}
if(message.length < 1900){ //if message is short send normal discord message
return axios
.post(webhook, { username: "Cool Webhook", content: `\`\`\`diff\n-${new Date()}\n+${message}\`\`\`` })
.catch(err => console.log(err.message || err));
}else{ //else if message is long send it as a txt file
const formData = new FormData();
formData.append("file", `[${new Date()}]\n${message}`, { filename : 'document.txt' });
// formData.append("content", "text");
// formData.append("file", new Buffer.from(message, "utf-8"), { filename : 'document.txt' });
return axios({
method: 'POST',
url: webhook,
params: { "wait": true },
headers: formData.getHeaders(),
data: formData
}).catch(err => console.log(err.message || err));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment