Skip to content

Commit

Permalink
directMessages now uses channel IDs as properties.
Browse files Browse the repository at this point in the history
Code refactored so sending messages to user IDs still works, and looks cleaner.
  • Loading branch information
izy521 committed Dec 29, 2015
1 parent 5b095e4 commit a90d205
Showing 1 changed file with 57 additions and 82 deletions.
139 changes: 57 additions & 82 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function DiscordClient(options) {
zlib = require('zlib'),
dns = require('dns'),
crypto = require('crypto'),
ws, KAi, cv, uv, vChannels = {};
ws, KAi, cv, uv, vChannels = {}, uIDToDM = {};
/*Version check*/
try {
request("https://registry.npmjs.org/discord.io", function(err, res, body) {
Expand Down Expand Up @@ -167,7 +167,9 @@ function DiscordClient(options) {
}
function getDirectMessages(DMArray) {
for (var DM=0; DM<DMArray.length; DM++) {
self.directMessages[DMArray[DM].recipient.id] = DMArray[DM];
self.directMessages[DMArray[DM].id] = DMArray[DM];
uIDToDM[DMArray[DM].recipient.id] = DMArray[DM].id;
//self.directMessages[DMArray[DM].recipient.id] = DMArray[DM];
}
}

Expand Down Expand Up @@ -339,8 +341,10 @@ function DiscordClient(options) {
if (message.d) {
if (message.d.is_private === true) {
self.directMessages[message.d.recipient.id] = {};
uIDToDM[message.d.recipient.id] = message.d.id;
for (var DMItem in message.d) {
self.directMessages[message.d.recipient.id][DMItem] = message.d[DMItem];
self.directMessages[message.d.id][DMItem] = message.d[DMItem];
//self.directMessages[message.d.recipient.id][DMItem] = message.d[DMItem];
}
} else {
self.servers[message.d.guild_id].channels[message.d.id] = {};
Expand All @@ -366,6 +370,10 @@ function DiscordClient(options) {
break;
case "CHANNEL_DELETE":
if (message.d) {
if (message.d.is_private === true) {
delete self.directMessages[message.d.id];
return delete uIDToDM[message.d.recipient.id];
}
delete self.servers[message.d.guild_id].channels[message.d.id];
}
break;
Expand Down Expand Up @@ -824,99 +832,66 @@ function DiscordClient(options) {
checkRS(function() {
var target = input.to;
var message = generateMessage(input.to, input.message);
var typingTime;
var time;
if (input.tts === true) message.tts = true;
if (input.nonce) message.nonce = input.nonce;
var messageJSON = JSON.stringify(message);

if (input.typing && input.typing === true) {
typingTime = (input.message.length * 0.12) * 1000;
(function emulateTyping(time) {
if (time <= 0) {
_sendMessage(message, target, messageJSON);
} else {
if (time > 5000) {
time = time - 5000;
} else {
time = time - time;
}
time = (input.message.length * 0.12) * 1000;
return emulateTyping(time);
}

function emulateTyping(time) {
if (time <= 0) return _sendMessage(messageJSON, target);
if (time > 5000) time = time - 5000; else time = time - time;

self.simulateTyping(target, function() {
setTimeout(function() {
emulateTyping(time);
}, time);
});
}

function _sendMessage(messageJSON, target) {

if (target in uIDToDM) return request.post({ //If they use a User ID and if there's a channel ID already matching the User ID, send it there.
url: "https://discordapp.com/api/channels/" + uIDToDM[target] + "/messages",
body: messageJSON
}, function(err, res, body) {
if (err || !checkStatus(res)) return console.log("Unable to send message: " + checkError(res, body));
if (typeof(callback) === 'function') return callback(JSON.parse(body));
});

for (var server in self.servers) { //If there's no User ID : Channel ID cache, see if the ID belongs to a User.
if (target in self.servers[server].members) return request.post({
url: "https://discordapp.com/api/users/" + self.id + "/channels",
body: JSON.stringify({recipient_id: target})
}, function(err, res, body) {
if (err || !checkStatus(res)) return;
body = JSON.parse(body);
uIDToDM[body.recipient.id] = body.id;
self.directMessages[body.id] = body;
request.post({
url: "https://discordapp.com/api/channels/" + target + "/typing",
url: "https://discordapp.com/api/channels/" + body.id + "/messages",
body: messageJSON
}, function(err, res, body) {
if (!err && checkStatus(res)) {
setTimeout(function() {
emulateTyping(time);
}, time);
}
if (err || !checkStatus(res)) return console.log("Unable to send message: " + checkError(res, body));
if (typof(callback) === 'function') return callback(JSON.parse(body));
});
}
})(typingTime);
return;
}
function _sendMessage(message, target, messageJSON) {
if (self.directMessages[target]) {
request.post({
url: "https://discordapp.com/api/channels/" + self.directMessages[target].id + "/messages",
body: messageJSON
}, function(err, res, mbody) {
if (!err && checkStatus(res)) {
if (typeof(callback) === 'function') {
callback(JSON.parse(mbody));
}
} else {
console.log("Unable to send message: " + checkError(res, mbody));
}
});
return;
} else {
for (var server in self.servers) {
if (self.servers[server].members[target]) {
var initMessage = {
recipient_id: target
};
request.post({
url: "https://discordapp.com/api/users/" + self.id + "/channels",
body: JSON.stringify(initMessage)
}, function(err, res, body) {
if (!err && checkStatus(res)) {
body = JSON.parse(body);
request.post({
url: "https://discordapp.com/api/channels/" + body.id + "/messages",
body: messageJSON
}, function(err, res, mbody) {
if (!err && checkStatus(res)) {
if (typeof(callback) === 'function') {
callback(JSON.parse(mbody));
}
} else {
console.log("Unable to send message. " + checkError(res, mbody));
}
});

self.directMessages[body.recipient.id] = body;
}
});

return;
}
}
}

request.post({
request.post({ //Finally, the ID must not belong to a User, so send the message directly to it, as it must be a channel.
url: "https://discordapp.com/api/channels/" + target + "/messages",
body: messageJSON
}, function(err, res, mbody) {
if (!err && checkStatus(res)) {
if (typeof(callback) === 'function') {
callback(JSON.parse(mbody));
}
} else {
console.log("Unable to send message. " + checkError(res, mbody));
}
}, function(err, res, body) {
if (err || !checkStatus(res)) return console.log("Unable to send message. " + checkError(res, body));
if (typeof(callback) === 'function') return callback(JSON.parse(body));
});
return;
}
_sendMessage(message, target, messageJSON);
_sendMessage(messageJSON, target);
});
};
self.getMessages = function(input, callback) {
Expand Down

0 comments on commit a90d205

Please sign in to comment.