From c2163a29c0c73863948460bc86d91e173bb27699 Mon Sep 17 00:00:00 2001 From: Hayden Andreyka Date: Thu, 4 Aug 2016 14:49:29 -0700 Subject: [PATCH] Docs update (#495) * Fix pagebreaks and formatting * Start touching up sections * Grab default channel Usually the general channel anyways :yum: * Add more information * move this up here * derp * cleanup * touchups Too many commits? noooo * Add template literals everywhere, fix backticks and some other stuff * quote standardization is that even spelled right * final touches there have been a lot of final touches today --- docs/examples.rst | 86 +++++++++++++++++++++++++++++++---------------- docs/vars.rst | 1 + 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/docs/examples.rst b/docs/examples.rst index 3a7cbde65..f959fe76f 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -5,9 +5,11 @@ Usage Examples Not all of these are standalone examples, many of them are usage examples. If you're a beginner to Discord.js, we encourage you to look through these examples to get a hang of the way things work using the library. -.. warning:: Please do not copy/paste code directly from these examples. Try to learn from and adapt these pieces of code to your specific situation. +.. warning :: Please do not copy/paste code directly from these examples. Try to learn from and adapt these pieces of code to your specific situation. --------- +.. note :: We use `Template Literals`_ in these examples. These are an ES6 feature and may not be fully supported in your environment. In this case, it is safe to use other methods of concatenating strings. + +----- Logging In ---------- @@ -28,10 +30,10 @@ Logging in with a username and password function output(error, token) { if (error) { - console.log('There was an error logging in: ' + error); + console.log(`There was an error logging in: ${error}`); return; } else - console.log('Logged in. Token: ' + token); + console.log(`Logged in. Token: ${token}`); } Logging in with a token @@ -48,10 +50,10 @@ You can get your bot's token using the `My Applications`_ page on the Discord De function output(error, token) { if (error) { - console.log('There was an error logging in: ' + error); + console.log(`There was an error logging in: ${error}`); return; } else - console.log('Logged in. Token: ' + token); + console.log(`Logged in. Token: ${token}`); } ----- @@ -65,45 +67,71 @@ Here we will demonstrate receiving messages and logging them to the console. client.on('message', function(message) { if (message.channel.isPrivate) { - console.log('(Private) ${message.author.name}: ${message.content}'); + console.log(`(Private) ${message.author.name}: ${message.content}`); } else { - console.log('(${message.server.name} / ${message.channel.name}) ${message.author.name}: ${message.content}'); + console.log(`(${message.server.name} / ${message.channel.name}) ${message.author.name}: ${message.content}`); } }); +----- + Sending messages ----------------- -Sends Hello to "general" in "my_server". +In the same channel +~~~~~~~~~~~~~~~~~~~ + +This is by far the most common way people will send a message in the Discord API. Here we will send a message to the same Channel_ we received a message from in the above example. + +.. code-block:: javascript + + client.on('message', function(message) { + // Don't forget to log the message! + client.sendMessage(message.channel, "Hello!"); + }); + +You can also use a `Message`_ resolvable as an parameter. This example does the same thing as above. .. code-block:: javascript - var channel = client.servers.get("name", "my_server").channels.get("name", "general"); + client.on('message', function(message) { + client.sendMessage(message, "Hello!"); + }); + +You can also directly reply to messages. This does the same as adding an @mention in front of your text. + +Sends "@author Hello!" + +.. code-block:: javascript + + client.on('message', function(message) { + client.reply(message, "Hello!"); + }); + +To a specific server and channel +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Sends "Hello" to the default Channel_ in the Server_ "My Server". Note that this does not require any sort of received message to be activated, however if there are multiple servers with the name "My Server", a random one will be chosen. + +See Cache_ for more details on getting specific objects and resolvables. + +.. code-block:: javascript + + var channel = client.servers.get("name", "My Server").defaultChannel; 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. +Private Messages +~~~~~~~~~~~~~~~~ + +You can also send private messages to a user with a User_ object. This will send "Hello!" as a private message to the original author of the received message. + +Do note however, that a PMChannel_ is not the same as a ServerChannel_ and therefore does not have the same properties such as ``server`` and ``name``. .. code-block:: javascript client.on('message', function(message) { - client.sendMessage(message, "Hello"); + client.sendMessage(message.author, "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!'); - }); +----- \ No newline at end of file diff --git a/docs/vars.rst b/docs/vars.rst index d6a1763ba..6ccb4ef64 100644 --- a/docs/vars.rst +++ b/docs/vars.rst @@ -18,6 +18,7 @@ .. _Resolvables : ./docs_resolvables.html .. _VoiceConnection : ./docs_voiceconnection.html .. _Promises : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise +.. _Template Literals : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals .. _EventEmitter : https://nodejs.org/api/events.html#events_class_events_eventemitter .. _Channel Resolvable : http://discordjs.readthedocs.org/en/indev/docs_resolvables.html#channel-resolvable .. _String Resolvable : http://discordjs.readthedocs.org/en/indev/docs_resolvables.html#string-resolvable