Created
November 30, 2016 17:54
-
-
Save joe-schorn/bee06175deb42bab95191cd68e758687 to your computer and use it in GitHub Desktop.
Discord webhook with queue
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
const request = require("request"); | |
const EventEmitter = require("events").EventEmitter; | |
const util = require("util"); | |
const endpoint = "https://discordapp.com/api/"; | |
function DiscordWebhook(url) { | |
this.url = url; | |
this.onReady = false; | |
var queue = []; | |
this.execute = function(payload) { | |
if (!this.onReady) { | |
queue.push(payload); | |
return; | |
} | |
request({ | |
url:endpoint+"webhooks/"+this.id+"/"+this.token, | |
method:"POST", | |
body:payload, | |
json:true | |
}, (e,r,b) => { | |
if (e) throw e; | |
}); | |
} | |
request(url, (e,r,b) => { | |
if (e) { | |
this.emit("error", e); | |
return; | |
} | |
try { | |
data = JSON.parse(b); | |
this.name = data.name; | |
this.channel_id = data.channel_id; | |
this.token = data.token; | |
this.avatar = data.avatar; | |
this.guild_id = data.guild_id; | |
this.id = data.id; | |
this.onReady = true; | |
for(var i = this.queue.lengh - 1; i >= 0; i--){ | |
var payload = this.queue.pop(); | |
this.execute(payload); | |
} | |
} catch (e) { | |
this.emit("error", e); | |
} | |
}); | |
EventEmitter.call(this); | |
} | |
util.inherits(DiscordWebhook, EventEmitter); | |
module.exports = DiscordWebhook; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment