From 461e5b337ec1b5b35220cf1edf6192f1a5793d5c Mon Sep 17 00:00:00 2001 From: Hugo Holmqvist Date: Tue, 2 Aug 2016 01:11:36 +0300 Subject: [PATCH] Moar doc examples (#490) * Add more examples to docs * docs * Switch to objects from resolvables * Small changes in wording --- docs/examples.rst | 51 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/docs/examples.rst b/docs/examples.rst index b4d82ab92..3a7cbde65 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -20,12 +20,12 @@ Logging in with a username and password **Do not use a normal user account for large or public bots. This is considered abuse of the API and can land you in trouble.** .. code-block:: javascript - + const Discord = require('discord.js'); var client = new Discord.Client(); - + client.login('mybot@example.com', 'password', output); - + function output(error, token) { if (error) { console.log('There was an error logging in: ' + error); @@ -40,12 +40,12 @@ Logging in with a token You can get your bot's token using the `My Applications`_ page on the Discord Developers site. .. code-block:: javascript - + const Discord = require('discord.js'); var client = new Discord.Client(); - + client.loginWithToken('token', output); - + function output(error, token) { if (error) { console.log('There was an error logging in: ' + error); @@ -62,7 +62,7 @@ Receiving Messages Here we will demonstrate receiving messages and logging them to the console. .. code-block:: javascript - + client.on('message', function(message) { if (message.channel.isPrivate) { console.log('(Private) ${message.author.name}: ${message.content}'); @@ -70,3 +70,40 @@ Here we will demonstrate receiving messages and logging them to the console. console.log('(${message.server.name} / ${message.channel.name}) ${message.author.name}: ${message.content}'); } }); + +Sending messages +----------------- + +Sends Hello to "general" in "my_server". + +.. code-block:: javascript + + var channel = client.servers.get("name", "my_server").channels.get("name", "general"); + client.sendMessage(channel, "Hello"); + +You can also use a `Message`_ object as an parameter. This example sends "Hello" to the channel the message was sent from. + +.. code-block:: javascript + + client.on('message', function(message) { + client.sendMessage(message, "Hello"); + }); + +You can send DMs to a user with a `User`_ object. This will send "Hello" as an DM to the author of the received message. + +.. code-block:: javascript + + client.on('message', function(message) { + client.sendMessage(message.author, "Hello"); + }); + +Replying to messages +------------------ + +Sends "@author Hello!". + +.. code-block:: javascript + + client.on('message', function(message) { + client.reply(message, 'Hello!'); + });