Added file sending

This commit is contained in:
hydrabolt
2015-08-21 14:00:39 +01:00
parent 68db1f6ac0
commit 9572236b69
3 changed files with 102 additions and 54 deletions

View File

@@ -264,6 +264,20 @@ Commands["setusername"] = {
} }
} }
Commands[ "cat" ] = {
oplevel: 0,
fn: function( bot, params, message ) {
var http = require( "http" );
var request = require( 'request' );
bot.sendFile( message, request("http://thewallpaperhost.com/wp-content/uploads/2014/12/cool-hd-wallpaper.jpg"), "cat.jpg", function( err ) {
bot.reply( message, err );
} );
}
}
Commands[ "icon" ] = { Commands[ "icon" ] = {
oplevel: 0, oplevel: 0,
fn: function( bot, params, message ) { fn: function( bot, params, message ) {

View File

@@ -745,7 +745,14 @@ exports.Client.prototype.startPM = function( user, callback ) {
* delete itself after being sent. * delete itself after being sent.
* @method sendMessage * @method sendMessage
*/ */
exports.Client.prototype.sendMessage = function( destination, toSend, callback, options ) {
exports.Client.prototype.sendFile = function( destination, toSend, fileName, callback, options ) {
this.sendMessage( destination, toSend, callback, options, fileName );
}
exports.Client.prototype.sendMessage = function( destination, toSend, callback, options, fileName ) {
options = options || {}; options = options || {};
callback = callback || function() {}; callback = callback || function() {};
@@ -753,8 +760,10 @@ exports.Client.prototype.sendMessage = function( destination, toSend, callback,
var channel_id, message, mentions, self = this; var channel_id, message, mentions, self = this;
channel_id = resolveChannel( destination, self ); channel_id = resolveChannel( destination, self );
if ( !fileName ) {
message = resolveMessage( toSend ); message = resolveMessage( toSend );
mentions = resolveMentions( message, options.mention ); mentions = resolveMentions( message, options.mention );
}
if ( channel_id ) { if ( channel_id ) {
send(); send();
@@ -764,6 +773,15 @@ exports.Client.prototype.sendMessage = function( destination, toSend, callback,
function send() { function send() {
if(fileName){
Internal.XHR.sendFile( self.token, channel_id, toSend, fileName, function(err){
callback(err);
} );
return;
}
Internal.XHR.sendMessage( self.token, channel_id, { Internal.XHR.sendMessage( self.token, channel_id, {
content: message, content: message,
mentions: mentions mentions: mentions

View File

@@ -124,6 +124,22 @@ Internal.XHR.sendMessage = function( token, channelID, messageParameters, callba
} }
Internal.XHR.sendFile = function( token, channelID, file, fileName, callback ) {
request
.post( Endpoints.CHANNELS + "/" + channelID + "/messages" )
.set( "authorization", token )
.attach("file", file, fileName)
.end( function( err, res ) {
if ( err ) {
callback( err );
} else {
callback( null, res.body );
}
} );
}
Internal.XHR.deleteMessage = function( token, channelID, messageID, callback ) { Internal.XHR.deleteMessage = function( token, channelID, messageID, callback ) {
request request
.del( Endpoints.CHANNELS + "/" + channelID + "/messages/" + messageID ) .del( Endpoints.CHANNELS + "/" + channelID + "/messages/" + messageID )