@pfearr/discordwebhook
Version:
Send Discord Webhooks Easily!
109 lines (106 loc) • 4.31 kB
JavaScript
let webhooks = {}
const color = require('terminalcolors.js')
var XMLHttpRequest = require('xhr2');
webhooks.Start = class {
/**
* Starts up the webhook process
* @class Webhook
* @param {URL} url Webhook URL
*/
constructor(url){
this.url = url;
}
/**
* Send Regular Webhook.
*
* For any optional arguments you can put null to disable that option
* @param {String} username Username of the webhook
* @param {String} text Text of the webhook
* @param {URL} AvatarURL Avatar for the webhook. This is optional.
* @param {Integer} Time Time it takes in seconds to send webhook. This is optional
* @param {Boolean} TTS Text To Speech. This is optional.
* @return {Boolean} Returns true if operation was successful, Halts and returns false if operation failed
*/
Send(username,text,AvatarURL,Time,TTS){
AvatarURL = AvatarURL || ""
Time = Time
TTS = TTS || false
if(!typeof(TTS) == Boolean){TTS = false}
if (parseInt(Time)){Time = Time * 1000}else{Time = null}
if(!username){return console.log(color.red("Please provide a username for your webhook"))}
if(!text){return console.log(color.red("Please provide text for your webhook"))}
if(!this.url){return console.log(color.red("No webhook URL specified!"))}
const request = new XMLHttpRequest();
request.open("POST", this.url);
request.setRequestHeader('Content-type', 'application/json');
const params = {
username: username,
avatar_url: AvatarURL,
content: text,
tts: TTS
}
try{
if (Time){
setTimeout(function(){
request.send(JSON.stringify(params));
return true
},Time)
} else {
request.send(JSON.stringify(params));
return true
}
} catch(e) {
console.log(color.red("Operation failed. Error: "+e))
return false
}
}
/**
* Send Embed Webhook.
*
* For any optional arguments you can put null to disable that option
* @param {Array} Embed Embed Array. Find more on docs. https://discord.com/developers/docs/resources/channel#embed-object. Make an embed here: https://discohook.org. You only need "embeds":[] part. Example: let embed = [ "embeds": [
{
"title": "Embed 1",
"color": null
}
]]
* @param {String} username Username of the webhook
* @param {String} text Text of the webhook. This is optional
* @param {URL} AvatarURL Avatar for the webhook. This is optional.
* @param {Integer} Time Time it takes in seconds to send webhook. This is optional
* @return {Boolen} Returns true if operation was successful, Halts and returns false if operation failed
*/
SendEmbed(Embed,username,text,AvatarURL,Time){
AvatarURL = AvatarURL || ""
Time = Time
text = text || null
if (parseInt(Time)){Time = Time * 1000}else{Time = null}
if(!username){return console.log(color.red("Please provide a username for your webhook"))}
if(!this.url){return console.log(color.red("No webhook URL specified!"))}
if (!Embed || Array.isArray(Embed)){return console.log(color.red("Embed must be JSON!"))}
const request = new XMLHttpRequest();
request.open("POST", this.url);
request.setRequestHeader('Content-type', 'application/json');
const params = {
username: username,
avatar_url: AvatarURL,
content: text,
embeds: Embed
}
try{
if (Time){
setTimeout(function(){
request.send(JSON.stringify(params));
return true
},Time)
} else {
request.send(JSON.stringify(params));
return true
}
} catch(e) {
console.log(color.red("Operation failed. Error: "+e))
return false
}
}
}
module.exports = webhooks;