mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 19:43:29 +01:00
small fixes and additions to examples, added deepFilter to list
This commit is contained in:
@@ -3,11 +3,17 @@
|
|||||||
* access user avatars.
|
* access user avatars.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Discord = require( "discord.js" );
|
var Discord = require( "../" );
|
||||||
var myBot = new Discord.Client();
|
var myBot = new Discord.Client();
|
||||||
|
|
||||||
myBot.login( "hello@example.com", "password1" );
|
myBot.login( "hello@example.com", "password1" );
|
||||||
|
|
||||||
|
// The "ready" event is triggered after the bot successfully connected to
|
||||||
|
// Discord and is ready to send messages.
|
||||||
|
myBot.on( "ready", function() {
|
||||||
|
console.log( "Bot connected successfully." );
|
||||||
|
} );
|
||||||
|
|
||||||
myBot.on( "message", function( message ) {
|
myBot.on( "message", function( message ) {
|
||||||
// React to all messages with the content "$avatar"
|
// React to all messages with the content "$avatar"
|
||||||
if ( message.content === "$avatar" ) {
|
if ( message.content === "$avatar" ) {
|
||||||
@@ -22,10 +28,10 @@ myBot.on( "message", function( message ) {
|
|||||||
// A user can be mentioned in a message by inserting the string obtained
|
// A user can be mentioned in a message by inserting the string obtained
|
||||||
// by user.mention() into the message.
|
// by user.mention() into the message.
|
||||||
// Note that simply writing "@user" will NOT work.
|
// Note that simply writing "@user" will NOT work.
|
||||||
bot.sendMessage( message.channel, message.author.mention() + ", here's your avatar: " + url );
|
this.sendMessage( message.channel, message.author.mention() + ", here's your avatar: " + url );
|
||||||
} else {
|
} else {
|
||||||
// Nothing should be done if the user has not set an avatar.
|
// Nothing should be done if the user has not set an avatar.
|
||||||
bot.sendMessage( message.channel, message.author.mention() + ", you don't have an avatar!" );
|
this.sendMessage( message.channel, message.author.mention() + ", you don't have an avatar!" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|||||||
@@ -3,16 +3,24 @@
|
|||||||
* joining or leaving.
|
* joining or leaving.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Discord = require( "discord.js" );
|
var Discord = require( "../" );
|
||||||
var myBot = new Discord.Client();
|
var myBot = new Discord.Client();
|
||||||
|
|
||||||
myBot.login( "hello@example.com", "password1" );
|
myBot.login( "hello@example.com", "password1" );
|
||||||
|
|
||||||
|
// The "ready" event is triggered after the bot successfully connected to
|
||||||
|
// Discord and is ready to send messages.
|
||||||
|
myBot.on( "ready", function() {
|
||||||
|
console.log( "Bot connected successfully." );
|
||||||
|
} );
|
||||||
|
|
||||||
// The "presence" event is triggered when a user joins a server, leaves it or
|
// The "presence" event is triggered when a user joins a server, leaves it or
|
||||||
// goes away.
|
// goes away.
|
||||||
// The status parameter can be "online", "offline" or "idle", respectively.
|
// The status parameter can be "online", "offline" or "idle", respectively.
|
||||||
myBot.on( "presence", function( user, status, server ) {
|
myBot.on( "presence", function( user, status, server ) {
|
||||||
// Send a message on the default channel of the server, as presence updates
|
// Send a message on the default channel of the server, as presence updates
|
||||||
// are not restricted to one channel.
|
// are not restricted to one channel.
|
||||||
bot.sendMessage( server.getDefaultChannel(), user.mention() + " is " + status + "!" );
|
var message = user.mention() + " is " + status + " in " + server.name + "!";
|
||||||
|
console.log(message);
|
||||||
|
this.sendMessage( server.getDefaultChannel(), message );
|
||||||
} );
|
} );
|
||||||
|
|||||||
@@ -4,47 +4,52 @@
|
|||||||
* 100 messages.
|
* 100 messages.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Discord = require( "discord.js" );
|
var Discord = require( "../" );
|
||||||
var myBot = new Discord.Client();
|
var myBot = new Discord.Client();
|
||||||
|
|
||||||
myBot.login( "hello@example.com", "password1" );
|
myBot.login( "hello@example.com", "password1" );
|
||||||
|
|
||||||
|
// The "ready" event is triggered after the bot successfully connected to
|
||||||
|
// Discord and is ready to send messages.
|
||||||
|
myBot.on( "ready", function() {
|
||||||
|
console.log( "Bot connected successfully." );
|
||||||
|
} );
|
||||||
|
|
||||||
myBot.on( "message", function( message ) {
|
myBot.on( "message", function( message ) {
|
||||||
// React to all messages starting with "$query".
|
// React to all messages starting with "$query".
|
||||||
if ( message.content.startsWith( "$query" ) ) {
|
if ( message.content.split(" ")[0] === "$query") {
|
||||||
// Obtain the channel for which logs should be accessed.
|
// Obtain the channel for which logs should be accessed.
|
||||||
var channel = message.channel;
|
var channel = message.channel;
|
||||||
|
|
||||||
// Find all the arguments to the command.
|
// Find all the arguments to the command.
|
||||||
var arguments = message.content.split( " " );
|
var arguments = message.content.split( " " );
|
||||||
|
|
||||||
// Get the first argument specifically, as it contains the username
|
// Get the arguments, as they contains the username
|
||||||
// to be queried for.
|
// to be queried for.
|
||||||
var username = arguments.slice( 1 ).join( " " );
|
var username = arguments.slice( 1 ).join( " " );
|
||||||
|
|
||||||
// Exit the event handler unless the user exists.
|
// Exit the event handler unless the user exists.
|
||||||
if( !username ) {
|
if ( !username ) {
|
||||||
myBot.sendMessage( channel, "That user doesn't exist!" );
|
myBot.sendMessage( channel, "That user doesn't exist!" );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The getChannelLogs() function takes the channel that should be accessed,
|
// The getChannelLogs() function takes the channel that should be accessed,
|
||||||
// the amount of messages to query and a callback as its arguments.
|
// the amount of messages to query and a callback as its arguments.
|
||||||
myBot.getChannelLogs( channel, 100, function( messageList ) {
|
myBot.getChannelLogs( channel, 1000, function( messageList ) {
|
||||||
// filter() takes three arguments, the key to be filtered for (in this
|
// filter() takes three arguments, the key to be filtered for (in this
|
||||||
// case the username, so "username"), the value to look for, and whether
|
// case the username, so "username"), the value to look for, and whether
|
||||||
// only the first finding should be returned (true) or a list of all
|
// only the first finding should be returned (true) or a list of all
|
||||||
// findings (false).
|
// findings (false).
|
||||||
var message = messageList.filter( "username", username, true );
|
var message = messageList.deepFilter( ["author", "username"], username, true );
|
||||||
|
|
||||||
// Only continue if the message has been found
|
// Only continue if the message has been found
|
||||||
if( message ) {
|
if ( message ) {
|
||||||
myBot.sendMessage( channel, "The last message from user " + username +
|
myBot.sendMessage( channel, "The last message from user " + username + " is:\n_" + message.content + "_" );
|
||||||
" is: \"" + message.content + "\"." ).
|
} else {
|
||||||
} else {
|
myBot.sendMessage( channel, "That user has not sent a message for the last 1,000 messages!" );
|
||||||
myBot.sendMessage( "That user has not sent a message " +
|
}
|
||||||
"for the last 100 messages!" )
|
} );
|
||||||
}
|
|
||||||
} );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} );
|
} );
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
* starts up or shuts down, respectively.
|
* starts up or shuts down, respectively.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Discord = require( "discord.js" );
|
var Discord = require( "../" );
|
||||||
var myBot = new Discord.Client();
|
var myBot = new Discord.Client();
|
||||||
|
|
||||||
myBot.login( "hello@example.com", "password1" );
|
myBot.login( "hello@example.com", "password1" );
|
||||||
@@ -19,6 +19,6 @@ myBot.on( "ready", function() {
|
|||||||
// ended.
|
// ended.
|
||||||
// It is also triggered when the connection attempt fails, for example due
|
// It is also triggered when the connection attempt fails, for example due
|
||||||
// to a wrong password.
|
// to a wrong password.
|
||||||
myBot.on( "disconnected", function() {
|
myBot.on( "disconnected", function(e) {
|
||||||
console.log( "Bot disconnected from Discord." );
|
console.log( "Bot disconnected from Discord -", e.reason );
|
||||||
} );
|
} );
|
||||||
|
|||||||
14
index.js
14
index.js
@@ -85,10 +85,10 @@ exports.Client.prototype.login = function( email, password ) {
|
|||||||
.send( details )
|
.send( details )
|
||||||
.end( function( err, res ) {
|
.end( function( err, res ) {
|
||||||
if ( !res.ok ) {
|
if ( !res.ok ) {
|
||||||
client.triggerEvent( "disconnected", {
|
client.triggerEvent( "disconnected", [{
|
||||||
reason: "failed to log in",
|
reason: "failed to log in",
|
||||||
error: err
|
error: err
|
||||||
} );
|
}] );
|
||||||
} else {
|
} else {
|
||||||
client.token = res.body.token;
|
client.token = res.body.token;
|
||||||
client.loggedIn = true;
|
client.loggedIn = true;
|
||||||
@@ -104,10 +104,10 @@ exports.Client.prototype.connectWebsocket = function( cb ) {
|
|||||||
|
|
||||||
this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB );
|
this.websocket = new WebSocket( Endpoints.WEBSOCKET_HUB );
|
||||||
this.websocket.onclose = function( e ) {
|
this.websocket.onclose = function( e ) {
|
||||||
client.triggerEvent( "disconnected", {
|
client.triggerEvent( "disconnected", [{
|
||||||
reason: "websocket disconnected",
|
reason: "websocket disconnected",
|
||||||
error: e
|
error: e
|
||||||
} );
|
}] );
|
||||||
};
|
};
|
||||||
this.websocket.onmessage = function( e ) {
|
this.websocket.onmessage = function( e ) {
|
||||||
|
|
||||||
@@ -265,6 +265,12 @@ exports.Client.prototype.sendMessage = function( channel, message, cb, _mentions
|
|||||||
.set( "authorization", client.token )
|
.set( "authorization", client.token )
|
||||||
.send( details )
|
.send( details )
|
||||||
.end( function( err, res ) {
|
.end( function( err, res ) {
|
||||||
|
|
||||||
|
if(err){
|
||||||
|
cb(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var msg = new Message( res.body, client.channelFromId( res.body.channel_id ) );
|
var msg = new Message( res.body, client.channelFromId( res.body.channel_id ) );
|
||||||
if ( options.selfDestruct ) {
|
if ( options.selfDestruct ) {
|
||||||
setTimeout( function() {
|
setTimeout( function() {
|
||||||
|
|||||||
28
lib/list.js
28
lib/list.js
@@ -72,3 +72,31 @@ exports.List.prototype.filter = function( key, value, onlyOne ) {
|
|||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.List.prototype.deepFilter = function( keys, value, onlyOne ) {
|
||||||
|
|
||||||
|
var results = [];
|
||||||
|
|
||||||
|
for ( index in this.contents ) {
|
||||||
|
var child = this.contents[ index ];
|
||||||
|
var buffer = child;
|
||||||
|
|
||||||
|
for(key of keys){
|
||||||
|
buffer = buffer[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( buffer == value ) {
|
||||||
|
if ( onlyOne ) {
|
||||||
|
return child;
|
||||||
|
} else {
|
||||||
|
results.push( child );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( onlyOne ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user