mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-19 21:13:30 +01:00
Added examples section to docs (#488)
* Started adding usage examples * Receiving messages May have to expand on this later * Changed wording * Add on to this Conflicts: docs/index.rst
This commit is contained in:
committed by
abalabahaha
parent
b05e18871a
commit
4f713a3f6d
72
docs/examples.rst
Normal file
72
docs/examples.rst
Normal file
@@ -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}');
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -21,6 +21,7 @@ Feel free to make any contributions you want, whether it be through creating an
|
|||||||
installing
|
installing
|
||||||
migrating
|
migrating
|
||||||
troubleshooting
|
troubleshooting
|
||||||
|
examples
|
||||||
|
|
||||||
.. toctree::
|
.. toctree::
|
||||||
:maxdepth: 1
|
:maxdepth: 1
|
||||||
|
|||||||
@@ -30,3 +30,5 @@
|
|||||||
.. _VoiceChannel Resolvable : http://discordjs.readthedocs.org/en/indev/docs_resolvables.html#voice-channel-resolvable
|
.. _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
|
.. _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
|
.. _Role Resolvable : http://discordjs.readthedocs.org/en/indev/docs_resolvables.html#role-resolvable
|
||||||
|
|
||||||
|
.. _My Applications : https://discordapp.com/developers/applications/me
|
||||||
Reference in New Issue
Block a user