small fixes and additions to examples, added deepFilter to list

This commit is contained in:
hydrabolt
2015-08-12 16:35:17 +01:00
parent 0013891764
commit 3e4f0fb74c
6 changed files with 96 additions and 43 deletions

View File

@@ -4,47 +4,52 @@
* 100 messages.
*/
var Discord = require( "discord.js" );
var Discord = require( "../" );
var myBot = new Discord.Client();
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 ) {
// React to all messages starting with "$query".
if ( message.content.startsWith( "$query" ) ) {
// Obtain the channel for which logs should be accessed.
var channel = message.channel;
if ( message.content.split(" ")[0] === "$query") {
// Obtain the channel for which logs should be accessed.
var channel = message.channel;
// Find all the arguments to the command.
var arguments = message.content.split( " " );
// Find all the arguments to the command.
var arguments = message.content.split( " " );
// Get the first argument specifically, as it contains the username
// to be queried for.
var username = arguments.slice( 1 ).join( " " );
// Get the arguments, as they contains the username
// to be queried for.
var username = arguments.slice( 1 ).join( " " );
// Exit the event handler unless the user exists.
if( !username ) {
myBot.sendMessage( channel, "That user doesn't exist!" );
return;
}
// Exit the event handler unless the user exists.
if ( !username ) {
myBot.sendMessage( channel, "That user doesn't exist!" );
return;
}
// The getChannelLogs() function takes the channel that should be accessed,
// the amount of messages to query and a callback as its arguments.
myBot.getChannelLogs( channel, 100, function( messageList ) {
// filter() takes three arguments, the key to be filtered for (in this
// 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
// findings (false).
var message = messageList.filter( "username", username, true );
// The getChannelLogs() function takes the channel that should be accessed,
// the amount of messages to query and a callback as its arguments.
myBot.getChannelLogs( channel, 1000, function( messageList ) {
// filter() takes three arguments, the key to be filtered for (in this
// 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
// findings (false).
var message = messageList.deepFilter( ["author", "username"], username, true );
// Only continue if the message has been found
if( message ) {
myBot.sendMessage( channel, "The last message from user " + username +
" is: \"" + message.content + "\"." ).
} else {
myBot.sendMessage( "That user has not sent a message " +
"for the last 100 messages!" )
}
} );
// Only continue if the message has been found
if ( message ) {
myBot.sendMessage( channel, "The last message from user " + username + " is:\n_" + message.content + "_" );
} else {
myBot.sendMessage( channel, "That user has not sent a message for the last 1,000 messages!" );
}
} );
}
} );