Skip to content

Commit f1a9c92

Browse files
eslachancegitbook-bot
authored andcommitted
GitBook: [master] 6 pages modified
1 parent d45bb6d commit f1a9c92

File tree

6 files changed

+90
-93
lines changed

6 files changed

+90
-93
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
---
2-
description: >-
3-
Starting up with discord.js botmaking? Need a written guide because docs are
4-
hard and videos are annoying? Boy have we got the solution for you! All for
5-
the one-time price of $0.00!
6-
---
7-
81
# Welcome
92

103
## Introduction

first-bot/your-first-bot.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
---
2-
description: >-
3-
In this chapter I'll guide you through the development of a simple bot with
4-
some useful commands. We'll start with the example we created in the first
5-
chapter and expand upon it.
6-
---
7-
81
# Your First Bot
92

103
This chapter assumes you've followed the Getting Started chapter and your bot code compiles. Also, I have to repeat: if you don't understand the code you're about to see, coding a bot might not be for you. Go to [CodeAcademy](https://www.codecademy.com/learn/javascript) and learn Javascript.

frequently-asked-questions.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
---
2-
description: >-
3-
In this page, some very basic, frequently-asked questions are answered. If I
4-
send you here, it means this page has your answer ;)
5-
---
6-
71
# Frequently Asked Questions
82

93
In this page, some very basic, frequently-asked questions are answered. It's important to understand that **these examples are generic** and will most likely not work if you just copy/paste them in your code. You need to **understand** these lines, not just blindly shove them in your code.

getting-started/getting-started-long-version.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
---
2-
description: >-
3-
So, you want to write a bot and you know some JavaScript, or maybe even
4-
node.js. You want to do cool things like a music bot, tag commands, the whole
5-
shebang. Well you're at the right place!
6-
---
7-
81
# Getting Started - Long Version
92

103
So, you want to write a bot and you know some JavaScript, or maybe even node.js. You want to do cool things like a music bot, tag commands, random image searches, the whole shebang. Well you're at the right place!

other-guides/env-files.md

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
# `.env` Files and Environment Variables
1+
# Using Environment Variables
22

3-
A `.env` file is a type of file that holds environment variables of an application. Environment variables allow you to easily integrate your bot with various online platforms (ex. Heroku), easily split your production and development environment as well as keep important information like your bot token, API tokens and database details secure.
3+
A `.env` file is a type of file that holds environment variables of an application. Environment variables allow you to easily integrate your bot with various online platforms \(ex. Heroku\), easily split your production and development environment as well as keep important information like your bot token, API tokens and database details secure.
44

55
You do not want this information to get out to anyone. And using environment variables in a `.env` file is one of the best ways to secure your information, being preferred over a `config.json` file.
66

77
To start out, it's suggested you install the `dotenv` package from NPM. This allows you to easily integrate environment variables into your bot. You may do that via the console. Make sure you are in your bot's root directory. Run this command:
88

99
`npm install dotenv`
1010

11-
Once installed, you will need to create a `.env` file. I'll create an example one here (I will be using the `CLIENT_TOKEN` variable from the master branch of discord.js).
11+
Once installed, you will need to create a `.env` file. I'll create an example one here \(I will be using the `CLIENT_TOKEN` variable from the master branch of discord.js\).
1212

13-
*If using v12 branch of discord.js, use `DISCORD_TOKEN=`.*
14-
```
13+
_If using v12 branch of discord.js, use_ `DISCORD_TOKEN=`_._
14+
15+
```text
1516
CLIENT_TOKEN=[YOUR_BOT_TOKEN]
1617
OWNER=[YOUR_OWNER_ID]
1718
PREFIX=[DEFAULT_BOT_PREFIX]
1819
```
20+
1921
After you create and update this file, save it and head over to your main file. I will build off of the default Discord.js example code.
20-
```js
22+
23+
```javascript
2124
const Discord = require('discord.js');
2225
// Importing this allows you to access the environment variables of the running node process
2326
require('dotenv').config();
@@ -48,56 +51,67 @@ client.on('message', message => {
4851
// Here you can login the bot. It automatically attempts to login the bot with the environment variable you set for your bot token (either "CLIENT_TOKEN" or "DISCORD_TOKEN")
4952
client.login();
5053
```
54+
5155
This is all you will need to get off the ground. Now I will explain why this is important and why you should use environment variables with a `.env` file over a `config.json` file.
5256

53-
## Using Git (ex. GitHub)
57+
## Using Git \(ex. GitHub\)
58+
5459
If you're going to publish your code with Git to a site like GitHub, then it's imperative you secure information by making sure your `.env` isn't committed to a repository. You may do that using a `.gitignore` file. Simply add the `.env` file to the `.gitignore` file in your local repository.
5560

5661
From there on out, any future commits will ignore that file or any other files or directories in `.gitignore`. You can set up a `.env.example` file in its place, but that's not always necessary.
57-
```
62+
63+
```text
5864
.env
5965
```
66+
6067
Following this, all users will see in your bot code is `process.env.ENV_VARIABLE` which exposes nothing!
6168

6269
## Using Heroku
70+
6371
Heroku is a website that allows you to start out and host your applications for free. In this case, your Discord bot. However, Heroku requires that you use environment variables. If you setup your files with the `dotenv` package and required the specific environment variables in your code, then all you have to do is go the environment variables in your Heroku application and add the key and value.
6472

6573
In Heroku, these variables are called "Config Vars". I'm not going to go in-depth here about Heroku, but the same way you setup your `.env` file, you would add in new environment variables in Heroku.
6674

6775
![Heroku Config Vars](https://i.imgur.com/MSmEO5K.png)
6876

6977
The only way your bot token can be exposed along with other environment variables is if you do one or more of these things:
70-
- a) You accidentally expose your `.env` file because you didn't add it to `.gitignore`
71-
- b) You added a collaborator through Heroku. A collaborator has almost full permissions as the app owner
78+
79+
* a\) You accidentally expose your `.env` file because you didn't add it to `.gitignore`
80+
* b\) You added a collaborator through Heroku. A collaborator has almost full permissions as the app owner
7281

7382
Both a and b can be controlled. It's just you need to be smart.
7483

7584
## Set environment variables in the start script
85+
7686
When starting your application, either locally on your computer, in a npm start script in your `package.json` or even in a `Dockerfile` you can set what environment your bot should run in.
7787

7888
For example, you may want to run your bot in production on Heroku or Glitch and in development on your computer. You can simply do that via adding new scripts in your `package.json`.
7989

8090
Upon doing `npm init` when you first made your bot, you should have seen a `test` script created. The scripts portion of your `package.json` should look like this if you added nothing.
81-
```json
91+
92+
```javascript
8293
"scripts": {
8394
"test": "echo \"Error: no test specified\" && exit 1",
8495
},
8596
```
8697

8798
Here, you can add multiple scripts. Where going to add a few scripts. `production`, `development`, and `start`.
88-
```json
99+
100+
```javascript
89101
"scripts": {
90102
"test": "echo \"Error: no test specified\" && exit 1",
91103
"start": "node .",
92104
"production": "NODE_ENV=production&&npm start",
93105
"development": "set NODE_ENV=development&&npm start"
94106
},
95107
```
96-
- To start your bot in a production environment, you would do `npm run production`. This will set `process.env.NODE_ENV` to `production`
97-
- To start your bot in a development environment, you would do `npm run development`. This will set `process.env.NODE_ENV` to `development`
108+
109+
* To start your bot in a production environment, you would do `npm run production`. This will set `process.env.NODE_ENV` to `production`
110+
* To start your bot in a development environment, you would do `npm run development`. This will set `process.env.NODE_ENV` to `development`
98111

99112
In your code, you can define what should happen depending on the environment loaded. Here's an example where your bot should only show the stream status if the environment is in `production` when the `ready` event is fired:
100-
```js
113+
114+
```javascript
101115
require('dotenv').config();
102116

103117
// process.env.NODE_ENV allows you to get the environment the node process is in
@@ -112,11 +126,13 @@ client.on('ready', () => {
112126
}
113127
});
114128
```
115-
Here I make sure that the bot is set to a streaming status only if my node environment is in `production`. I don't want my bot shown as streaming while I'm working on it in a development environment. There is a lot more you can do with this though.
129+
130+
Here I make sure that the bot is set to a streaming status only if my node environment is in `production`. I don't want my bot shown as streaming while I'm working on it in a development environment. There is a lot more you can do with this though.
116131

117132
For example, you can change the `production` and `development` scripts to use entirely different main files if you wish. But I won't get into that here.
118133

119134
If you don't want to use start scripts, you can always set the node environment directly in the command line. Here's how:
135+
120136
```bash
121137
# Windows
122138
SET NODE_ENV=development&&npm start
@@ -130,22 +146,29 @@ NODE_ENV=development&&node app.js
130146
```
131147

132148
Either running these commands directly through the command line or in start scripts should work. It's entirely up to you. But for ease of use when deploying your bot, you should use start scripts. So for example, in Heroku, you can add this in your `Procfile`:
133-
```
149+
150+
```text
134151
# Heroku will run the bot in production mode
135152
worker npm run production
136153
```
154+
137155
If you're using Glitch, you can add this in your `start` script in your `package.json`:
138-
```json
156+
157+
```javascript
139158
"scripts": {
140159
"test": "echo \"Error: no test specified\" && exit 1",
141160
"start": "NODE_ENV=production&&node app.js"
142161
},
143162
```
163+
144164
You may need to change the name of the main file depending on what you called it/where it's located in your bot project.
145165

146166
## More information
167+
147168
Here are some links to more information you can read regarding environment variables and Git if you aren't that familiar with it:
148-
- [Using Git to share and update code (An Idiot's Guide)](https://anidiots.guide/other-guides/using-git-to-share-and-update-code#ignoring-files)
149-
- [Working with Environment Variables in Node.js (Twilio Blog)](https://www.twilio.com/blog/2017/08/working-with-environment-variables-in-node-js.html)
150-
- [`dotenv` NPM](https://www.npmjs.com/package/dotenv)
151-
- [`dotenv-flow` NPM (Used for multiple `.env` file)](https://www.npmjs.com/package/dotenv-flow)
169+
170+
* [Using Git to share and update code \(An Idiot's Guide\)](https://anidiots.guide/other-guides/using-git-to-share-and-update-code#ignoring-files)
171+
* [Working with Environment Variables in Node.js \(Twilio Blog\)](https://www.twilio.com/blog/2017/08/working-with-environment-variables-in-node-js.html)
172+
* [`dotenv` NPM](https://www.npmjs.com/package/dotenv)
173+
* [`dotenv-flow` NPM \(Used for multiple `.env` file\)](https://www.npmjs.com/package/dotenv-flow)
174+

0 commit comments

Comments
 (0)