diff --git a/docs/examples.rst b/docs/examples.rst new file mode 100644 index 000000000..b4d82ab92 --- /dev/null +++ b/docs/examples.rst @@ -0,0 +1,72 @@ +.. include:: ./vars.rst + +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. + +-------- + +Logging In +---------- + +Logs the Client_ in, allowing you to begin working with the Discord API. + +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); + return; + } else + console.log('Logged in. Token: ' + token); + } + +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); + return; + } else + console.log('Logged in. Token: ' + token); + } + +----- + +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}'); + } else { + console.log('(${message.server.name} / ${message.channel.name}) ${message.author.name}: ${message.content}'); + } + }); diff --git a/docs/index.rst b/docs/index.rst index 99d1c5324..2c73d39c6 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -21,6 +21,7 @@ Feel free to make any contributions you want, whether it be through creating an installing migrating troubleshooting + examples .. toctree:: :maxdepth: 1 diff --git a/docs/vars.rst b/docs/vars.rst index d63f3e063..d6a1763ba 100644 --- a/docs/vars.rst +++ b/docs/vars.rst @@ -30,3 +30,5 @@ .. _VoiceChannel Resolvable : http://discordjs.readthedocs.org/en/indev/docs_resolvables.html#voice-channel-resolvable .. _File Resolvable : http://discordjs.readthedocs.org/en/indev/docs_resolvables.html#file-resolvable .. _Role Resolvable : http://discordjs.readthedocs.org/en/indev/docs_resolvables.html#role-resolvable + +.. _My Applications : https://discordapp.com/developers/applications/me \ No newline at end of file