From 9f049e5e18a01288d269c31567b0f553699768b7 Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 12 Aug 2015 13:17:36 +0200 Subject: [PATCH 1/8] Add comments to pingpong.js --- examples/pingpong.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/examples/pingpong.js b/examples/pingpong.js index a832a8f62..57d45dc42 100644 --- a/examples/pingpong.js +++ b/examples/pingpong.js @@ -1,10 +1,26 @@ +/* + * A basic bot that shows how to connect to a Discord account, + * how to listen to messages and how to send messages. + * + * This bot responds to every "ping" message with a "pong". + */ + var Discord = require( "discord.js" ); + +// Create the bot var myBot = new Discord.Client(); +// Login with an example email and password myBot.login( "hello@example.com", "password1" ); +// Add a listener to the "message" event, which triggers upon receiving +// any message myBot.on( "message", function( message ) { + // message.content accesses the content of the message as a string. + // If it is equal to "ping", then the bot should respond with "pong". if ( message.content === "ping" ) { + // Send a message ("pong") to the channel the message was sent in, + // which is accessed by message.channel. this.sendMessage( message.channel, "pong" ); } } ); From c0c8b7322d3c124e8a17d8c118f093bf2fd2ae99 Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 12 Aug 2015 13:21:43 +0200 Subject: [PATCH 2/8] Add comments to avatar.js --- examples/avatar.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/examples/avatar.js b/examples/avatar.js index 9beec588f..c6fa4d69d 100644 --- a/examples/avatar.js +++ b/examples/avatar.js @@ -1,15 +1,30 @@ +/* + * A bot that shows how to mention users in messages and how to + * access user avatars. + */ + var Discord = require( "discord.js" ); var myBot = new Discord.Client(); myBot.login( "hello@example.com", "password1" ); myBot.on( "message", function( message ) { + // React to all messages with the content "$avatar" if ( message.content === "$avatar" ) { - var user = message.author; //the user who wants an avatar is the author + // Obtain the user who requested the avatar. + var user = message.author; + + // Check whether the user actually has an avatar. if ( user.avatar ) { + // Construct the avatar URL from the user ID and the avatar ID. var url = "https://discordapp.com/api/users/" + user.id + "/avatars/" + user.avatar + ".jpg"; + + // A user can be mentioned in a message by inserting the string obtained + // by user.mention() into the message. + // Note that simply writing "@user" will NOT work. bot.sendMessage( message.channel, message.author.mention() + ", here's your avatar: " + url ); } else { + // 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!" ); } } From 2a1e3a7e69e24cdf2e54cbe2589247bf56ea57aa Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 12 Aug 2015 13:24:42 +0200 Subject: [PATCH 3/8] Add comments to presence.js --- examples/presence.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/examples/presence.js b/examples/presence.js index ec8d9bc39..4a426e601 100644 --- a/examples/presence.js +++ b/examples/presence.js @@ -1,8 +1,18 @@ +/* + * A bot that shows how to listen to presence update events, such as a user + * joining or leaving. + */ + var Discord = require( "discord.js" ); var myBot = new Discord.Client(); myBot.login( "hello@example.com", "password1" ); +// The "presence" event is triggered when a user joins a server, leaves it or +// goes away. +// The status parameter can be "online", "offline" or "idle", respectively. myBot.on( "presence", function( user, status, server ) { + // Send a message on the default channel of the server, as presence updates + // are not restricted to one channel. bot.sendMessage( server.getDefaultChannel(), user.mention() + " is " + status + "!" ); } ); From d4cd1ec6f82118b0162105d7f743e520c36af38b Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 12 Aug 2015 13:30:46 +0200 Subject: [PATCH 4/8] Add an example that shows the bot status events --- examples/status.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 examples/status.js diff --git a/examples/status.js b/examples/status.js new file mode 100644 index 000000000..a54814b01 --- /dev/null +++ b/examples/status.js @@ -0,0 +1,24 @@ +/* + * A bot that doesn't interact with Discord, but instead shows how to listen + * to the "ready" and "disconnected" events, that are triggered when the bot + * starts up or shuts down, respectively. + */ + +var Discord = require( "discord.js" ); +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." ); +} ); + +// The "disconnected" event is triggered after the connection to Discord +// ended. +// It is also triggered when the connection attempt fails, for example due +// to a wrong password. +myBot.on( "disconnected", function() { + console.log( "Bot disconnected from Discord." ); +} ); From 8d403a0274f073ad0d427e3122b58f8d9889e01b Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 12 Aug 2015 13:54:27 +0200 Subject: [PATCH 5/8] Add an example to show channel logs --- examples/query.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 examples/query.js diff --git a/examples/query.js b/examples/query.js new file mode 100644 index 000000000..4cf7e7df1 --- /dev/null +++ b/examples/query.js @@ -0,0 +1,50 @@ +/* + * A bot that shows how to access and search the logs of a specific channel. + * Specifically, it returns the last message from a given user in the last + * 100 messages. + */ + +var Discord = require( "discord.js" ); +var myBot = new Discord.Client(); + +myBot.login( "hello@example.com", "password1" ); + +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; + + // 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[1]; + + // 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 ); + + // 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!") + } + } ); + } +} ); From cd2b79393ad4486297d1dc1cdebcbf1ff61554bb Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 12 Aug 2015 13:54:39 +0200 Subject: [PATCH 6/8] Add an example to show message formatting --- examples/formatting.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 examples/formatting.js diff --git a/examples/formatting.js b/examples/formatting.js new file mode 100644 index 000000000..c12d0af5a --- /dev/null +++ b/examples/formatting.js @@ -0,0 +1,17 @@ +/* + * Discord uses a subset of Markdown for formatting, so adding formatting to + * messages is as simple as inserting the formatting codes into the message. + */ + +var Discord = require( "discord.js" ); +var myBot = new Discord.Client(); + +myBot.login( "hello@example.com", "password1" ); + +myBot.on( "message", function( message ) { + // React to all messages with the content "$formatting". + if ( message.content === "$formatting" ) { + myBot.sendMessage( message.channel, "**bold** ****semibold**** *italic* " + + "_**bold and italic**_ __underline__ ~~strikethrough~~") + } +} ); From 365746f2888752a47eb3ebaba47ca291ff3437d9 Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 12 Aug 2015 13:56:25 +0200 Subject: [PATCH 7/8] Fix formatting.js code formatting and add a comment --- examples/formatting.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/formatting.js b/examples/formatting.js index c12d0af5a..9131e9c37 100644 --- a/examples/formatting.js +++ b/examples/formatting.js @@ -11,7 +11,8 @@ myBot.login( "hello@example.com", "password1" ); myBot.on( "message", function( message ) { // React to all messages with the content "$formatting". if ( message.content === "$formatting" ) { + // Show off formatting by sending a simple message with formatting codes. myBot.sendMessage( message.channel, "**bold** ****semibold**** *italic* " + - "_**bold and italic**_ __underline__ ~~strikethrough~~") + "_**bold and italic**_ __underline__ ~~strikethrough~~" ); } } ); From 7fd5f8f5a66289dcd680d407e0caa954c4e968ee Mon Sep 17 00:00:00 2001 From: meew0 Date: Wed, 12 Aug 2015 14:03:21 +0200 Subject: [PATCH 8/8] Fix query.js to properly deal with users with spaces in their name. --- examples/query.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/query.js b/examples/query.js index 4cf7e7df1..00680102f 100644 --- a/examples/query.js +++ b/examples/query.js @@ -16,15 +16,15 @@ myBot.on( "message", function( message ) { var channel = message.channel; // 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 // to be queried for. - var username = arguments[1]; + var username = arguments.slice( 1 ).join( " " ); // Exit the event handler unless the user exists. if( !username ) { - myBot.sendMessage(channel, "That user doesn't exist!"); + myBot.sendMessage( channel, "That user doesn't exist!" ); return; } @@ -39,11 +39,11 @@ myBot.on( "message", function( message ) { // Only continue if the message has been found if( message ) { - myBot.sendMessage(channel, "The last message from user " + username + - " is: \"" + message.content + "\"."). + 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!") + myBot.sendMessage( "That user has not sent a message " + + "for the last 100 messages!" ) } } ); }