mirror of
https://github.com/discordjs/discord.js.git
synced 2024-08-20 23:14:44 +12:00
Added joining servers and better message handling
This commit is contained in:
parent
945fe8f65e
commit
93361925ab
7 changed files with 110 additions and 44 deletions
|
@ -19,7 +19,7 @@ Commands[ "info" ] = {
|
|||
"User ID is *" + user.id + "*",
|
||||
"Authority/OP Level to me is **" + Authority.getLevel( user ) + "**"
|
||||
], function( err ) {
|
||||
if(err)
|
||||
if ( err )
|
||||
console.log( err );
|
||||
} );
|
||||
|
||||
|
@ -27,77 +27,77 @@ Commands[ "info" ] = {
|
|||
}
|
||||
|
||||
Commands[ "loading" ] = {
|
||||
oplevel:0,
|
||||
fn: function(bot, params, message){
|
||||
oplevel: 0,
|
||||
fn: function( bot, params, message ) {
|
||||
|
||||
var progress = 0;
|
||||
var currentMessage;
|
||||
var bars = 20;
|
||||
|
||||
function getM(){
|
||||
function getM() {
|
||||
var before = progress;
|
||||
var after = bars - progress;
|
||||
var ret = "";
|
||||
for(x=0; x < before; x++){
|
||||
for ( x = 0; x < before; x++ ) {
|
||||
ret += "-";
|
||||
}
|
||||
ret += "**#**";
|
||||
for(y=0; y < after; y++){
|
||||
for ( y = 0; y < after; y++ ) {
|
||||
ret += "-";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
function doProg(){
|
||||
if(progress === (bars + 1)){
|
||||
function doProg() {
|
||||
if ( progress === ( bars + 1 ) ) {
|
||||
progress = 0;
|
||||
}
|
||||
|
||||
if(currentMessage){
|
||||
bot.updateMessage(currentMessage, getM(), function(err, msg){
|
||||
if(!err)
|
||||
if ( currentMessage ) {
|
||||
bot.updateMessage( currentMessage, getM(), function( err, msg ) {
|
||||
if ( !err )
|
||||
currentMessage = msg;
|
||||
});
|
||||
} );
|
||||
progress++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bot.sendMessage(message.channel, getM(), function(err, message){
|
||||
bot.sendMessage( message.channel, getM(), function( err, message ) {
|
||||
currentMessage = message;
|
||||
setInterval(doProg, 200);
|
||||
});
|
||||
setInterval( doProg, 200 );
|
||||
} );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Commands[ "flashy" ] = {
|
||||
oplevel:0,
|
||||
fn: function(bot, params, message){
|
||||
oplevel: 0,
|
||||
fn: function( bot, params, message ) {
|
||||
|
||||
var phase = 0;
|
||||
var msg;
|
||||
|
||||
var textToSay = getKey(params, "m", "FLASH");
|
||||
var speed = parseInt( getKey(params, "s", "500") );
|
||||
var textToSay = getKey( params, "m", "FLASH" );
|
||||
var speed = parseInt( getKey( params, "s", "500" ) );
|
||||
|
||||
function change(){
|
||||
if(msg){
|
||||
function change() {
|
||||
if ( msg ) {
|
||||
|
||||
var highlighting = ((phase % 2) === 0 ? "**" : "");
|
||||
var highlighting = ( ( phase % 2 ) === 0 ? "**" : "" );
|
||||
phase++;
|
||||
bot.updateMessage(msg, highlighting + textToSay + highlighting, function(err, message){
|
||||
if(!err){
|
||||
bot.updateMessage( msg, highlighting + textToSay + highlighting, function( err, message ) {
|
||||
if ( !err ) {
|
||||
msg = message;
|
||||
}
|
||||
});
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
bot.sendMessage(message.channel, textToSay, function(err, message){
|
||||
bot.sendMessage( message.channel, textToSay, function( err, message ) {
|
||||
msg = message;
|
||||
setInterval(change, speed);
|
||||
});
|
||||
setInterval( change, speed );
|
||||
} );
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -278,7 +278,7 @@ Commands[ "feedback" ] = {
|
|||
|
||||
bot.getChannelLogs( message.channel, amount, function( err, logs ) {
|
||||
|
||||
console.log(logs);
|
||||
console.log( logs );
|
||||
|
||||
if ( err ) {
|
||||
bot.reply( message, "an error occurred when grabbing the logs.", false, {
|
||||
|
@ -289,8 +289,8 @@ Commands[ "feedback" ] = {
|
|||
var found = [];
|
||||
for ( msg of logs.contents ) {
|
||||
|
||||
if ( ~msg.content.indexOf( "[request" ) || ~msg.content.indexOf( "[feature" || ~msg.content.indexOf( "[suggestion") ) ) {
|
||||
if(msg.content.length > 10){
|
||||
if ( ~msg.content.indexOf( "[request" ) || ~msg.content.indexOf( "[feature" || ~msg.content.indexOf( "[suggestion" ) ) ) {
|
||||
if ( msg.content.length > 10 ) {
|
||||
found.push( msg );
|
||||
}
|
||||
}
|
||||
|
@ -299,13 +299,13 @@ Commands[ "feedback" ] = {
|
|||
|
||||
bot.sendMessage( message.author, "Ok, here's a rundown of all feature requests so far:", function( err, ms ) {
|
||||
|
||||
if (!err)
|
||||
if ( !err )
|
||||
gothroughit();
|
||||
|
||||
} );
|
||||
|
||||
bot.reply( message, "I found " + found.length + " result(s) that matched this. I'll send it to you in a PM.", false, {
|
||||
selfDestruct : 3000
|
||||
selfDestruct: 3000
|
||||
} );
|
||||
|
||||
function gothroughit() {
|
||||
|
@ -322,6 +322,23 @@ Commands[ "feedback" ] = {
|
|||
|
||||
}
|
||||
|
||||
Commands[ "acceptinvite" ] = {
|
||||
oplevel: 0,
|
||||
fn: function( bot, params, message ) {
|
||||
|
||||
var inv = getKey(params, "i");
|
||||
|
||||
bot.joinServer(inv, function(err, server){
|
||||
if(err){
|
||||
bot.reply(message, "I couldn't join that server :(");
|
||||
}else{
|
||||
bot.reply(message, "I joined **"+ server.name +"**, a server with "+server.channels.length()+" channels and "+server.members.length()+" members.");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Commands[ "remind" ] = {
|
||||
oplevel: 0,
|
||||
fn: function( bot, params, message ) {
|
||||
|
@ -380,12 +397,12 @@ Commands[ "activity" ] = {
|
|||
var report = "here's a list of activity over the last " + count + " messages :\n\n";
|
||||
|
||||
var usernames = {};
|
||||
for( id in activity ){
|
||||
usernames[id] = bot.getUser(id).username;
|
||||
for ( id in activity ) {
|
||||
usernames[ id ] = bot.getUser( id ).username;
|
||||
}
|
||||
|
||||
for ( id in activity ) {
|
||||
report += usernames[id] + " | " + activity[ id ] + " | **" + Math.round( ( activity[ id ] / count ) * 100 ) + "%**.\n";
|
||||
report += usernames[ id ] + " | " + activity[ id ] + " | **" + Math.round( ( activity[ id ] / count ) * 100 ) + "%**.\n";
|
||||
}
|
||||
|
||||
bot.reply( message, report, false, false );
|
||||
|
|
|
@ -24,6 +24,7 @@ var hydrabot = new Discord.Client();
|
|||
var commandPrefixes = [ "$", "£", "`" ];
|
||||
|
||||
// Log the client in using the auth details in config.json
|
||||
console.time("hydrabotbenchmark");
|
||||
hydrabot.login( BotConfig.email, BotConfig.password );
|
||||
|
||||
console.log( "Starting up..." );
|
||||
|
@ -32,7 +33,7 @@ var time = Date.now();
|
|||
|
||||
// When the bot is ready to go, output to the console
|
||||
hydrabot.on( "ready", function() {
|
||||
console.log( "Ready in "+ (Date.now() - time) +"ms!" );
|
||||
console.timeEnd("hydrabotbenchmark");
|
||||
} );
|
||||
|
||||
// When the bot gets disconnected, exit.
|
||||
|
@ -48,12 +49,12 @@ hydrabot.on("messageDelete", function(message){
|
|||
|
||||
hydrabot.on("messageUpdate", function(former, edit){
|
||||
|
||||
if(former.author.equals(this.user)){
|
||||
return;
|
||||
}
|
||||
|
||||
if(former){
|
||||
|
||||
if(former.author.equals(this.user) || former.content === edit.content){
|
||||
return;
|
||||
}
|
||||
|
||||
var seconds = Math.round((Date.now() - former.time) / 1000);
|
||||
|
||||
var channel = former.channel;
|
||||
|
|
29
index.js
29
index.js
|
@ -10,6 +10,8 @@ var PMChannel = require( "./lib/PMChannel.js" ).PMChannel;
|
|||
var WebSocket = require( 'ws' );
|
||||
var Internal = require( "./lib/internal.js" ).Internal;
|
||||
|
||||
var serverCreateRequests = [];
|
||||
|
||||
/**
|
||||
* The wrapper module for the Discord Client, also provides some helpful objects.
|
||||
*
|
||||
|
@ -34,6 +36,7 @@ exports.PMChannel = PMChannel;
|
|||
* @param {Object} [options] An object containing configurable options.
|
||||
* @param {Number} [options.maxmessage=5000] The maximum amount of messages to be stored per channel.
|
||||
*/
|
||||
|
||||
exports.Client = function( options ) {
|
||||
|
||||
/**
|
||||
|
@ -405,6 +408,7 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
|
|||
data.id = data.id || formerMessage.id;
|
||||
data.mentions = data.mentions || formerMessage.mentions;
|
||||
data.mention_everyone = data.mention_everyone || formerMessage.everyoneMentioned;
|
||||
data.embeds = data.embeds || formerMessage.embeds;
|
||||
|
||||
newMessage = new Message( data, channel );
|
||||
|
||||
|
@ -447,7 +451,12 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
|
|||
|
||||
if ( !self.serverList.filter( "id", dat.d.id, true ) ) {
|
||||
self.cacheServer( dat.d, function( server ) {
|
||||
self.triggerEvent( "serverJoin", [ server ] );
|
||||
if ( serverCreateRequests[ server.id ] ) {
|
||||
serverCreateRequests[ server.id ]( null, server );
|
||||
serverCreateRequests[ server.id ] = null;
|
||||
} else {
|
||||
self.triggerEvent( "serverJoin", [ server ] );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
|
@ -878,6 +887,24 @@ exports.Client.prototype.deleteServer = function( server, callback ) {
|
|||
|
||||
}
|
||||
|
||||
exports.Client.prototype.joinServer = function( invite, callback ) {
|
||||
|
||||
var self = this;
|
||||
|
||||
var code = ( invite instanceof Invite ? invite.code : invite );
|
||||
|
||||
Internal.XHR.acceptInvite( self.token, code, function( err, inviteData ) {
|
||||
|
||||
if ( err ) {
|
||||
callback( err );
|
||||
} else {
|
||||
serverCreateRequests[inviteData.guild.id] = callback;
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
exports.Client.prototype.getServers = function() {
|
||||
return this.serverList;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ exports.Channel = function(name, server, type, id, isPrivate){
|
|||
this.type = type;
|
||||
this.id = id;
|
||||
this.isPrivate = isPrivate;
|
||||
this.messages = new List("id", "5000");
|
||||
this.messages = new List("id", 5000);
|
||||
}
|
||||
|
||||
exports.Channel.equals = function(otherChannel){
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
var base = "https://discordapp.com/";
|
||||
var apibase = base + "api";
|
||||
|
||||
exports.API = apibase;
|
||||
|
||||
exports.WEBSOCKET_HUB = "wss://discordapp.com/hub"
|
||||
|
||||
exports.USERS = apibase + "/users";
|
||||
|
|
|
@ -225,4 +225,21 @@ Internal.XHR.getServer = function( token, serverID, callback ) {
|
|||
|
||||
}
|
||||
|
||||
Internal.XHR.acceptInvite = function(token, inviteID, callback){
|
||||
|
||||
console.log(Endpoints.API + "/invite/" + inviteID);
|
||||
|
||||
request
|
||||
.post( Endpoints.API + "/invite/" + inviteID )
|
||||
.set( "authorization", token )
|
||||
.end(function(err, res){
|
||||
if(err){
|
||||
callback(err);
|
||||
}else{
|
||||
callback(null, res.body)
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
exports.Internal = Internal;
|
||||
|
|
|
@ -2,7 +2,7 @@ var User = require( "./user.js" ).User;
|
|||
var List = require( "./list.js" ).List;
|
||||
var PMChannel = require( "./PMChannel.js" ).PMChannel;
|
||||
|
||||
exports.Message = function( time, author, content, channel, id, mentions, everyoneMentioned ) {
|
||||
exports.Message = function( time, author, content, channel, id, mentions, everyoneMentioned, embeds ) {
|
||||
|
||||
if ( !content ) {
|
||||
message = time;
|
||||
|
@ -13,6 +13,7 @@ exports.Message = function( time, author, content, channel, id, mentions, everyo
|
|||
id = message.id;
|
||||
mentions = message.mentions;
|
||||
everyoneMentioned = message.mention_everyone;
|
||||
embeds = message.embeds;
|
||||
}
|
||||
|
||||
this.time = Date.parse( time );
|
||||
|
@ -22,6 +23,7 @@ exports.Message = function( time, author, content, channel, id, mentions, everyo
|
|||
this.id = id;
|
||||
this.mentions = new List( "id" );
|
||||
this.everyoneMentioned = everyoneMentioned;
|
||||
this.embeds = embeds;
|
||||
for ( x in mentions ) {
|
||||
var _mention = mentions[ x ];
|
||||
this.mentions.add( new User( _mention ) );
|
||||
|
|
Loading…
Reference in a new issue