You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: first-bot/your-first-bot.md
-7Lines changed: 0 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
-
8
1
# Your First Bot
9
2
10
3
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.
Copy file name to clipboardExpand all lines: frequently-asked-questions.md
-6Lines changed: 0 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
-
7
1
# Frequently Asked Questions
8
2
9
3
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.
Copy file name to clipboardExpand all lines: getting-started/getting-started-long-version.md
-7Lines changed: 0 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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
-
8
1
# Getting Started - Long Version
9
2
10
3
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!
Copy file name to clipboardExpand all lines: other-guides/env-files.md
+45-22Lines changed: 45 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,26 @@
1
-
# `.env` Files and Environment Variables
1
+
# Using Environment Variables
2
2
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.
4
4
5
5
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.
6
6
7
7
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:
8
8
9
9
`npm install dotenv`
10
10
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\).
12
12
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
15
16
CLIENT_TOKEN=[YOUR_BOT_TOKEN]
16
17
OWNER=[YOUR_OWNER_ID]
17
18
PREFIX=[DEFAULT_BOT_PREFIX]
18
19
```
20
+
19
21
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
21
24
constDiscord=require('discord.js');
22
25
// Importing this allows you to access the environment variables of the running node process
// 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")
49
52
client.login();
50
53
```
54
+
51
55
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.
52
56
53
-
## Using Git (ex. GitHub)
57
+
## Using Git \(ex. GitHub\)
58
+
54
59
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.
55
60
56
61
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
58
64
.env
59
65
```
66
+
60
67
Following this, all users will see in your bot code is `process.env.ENV_VARIABLE` which exposes nothing!
61
68
62
69
## Using Heroku
70
+
63
71
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.
64
72
65
73
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.
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
72
81
73
82
Both a and b can be controlled. It's just you need to be smart.
74
83
75
84
## Set environment variables in the start script
85
+
76
86
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.
77
87
78
88
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`.
79
89
80
90
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
82
93
"scripts": {
83
94
"test":"echo \"Error: no test specified\" && exit 1",
84
95
},
85
96
```
86
97
87
98
Here, you can add multiple scripts. Where going to add a few scripts. `production`, `development`, and `start`.
88
-
```json
99
+
100
+
```javascript
89
101
"scripts": {
90
102
"test":"echo \"Error: no test specified\" && exit 1",
- 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`
98
111
99
112
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
101
115
require('dotenv').config();
102
116
103
117
// process.env.NODE_ENV allows you to get the environment the node process is in
@@ -112,11 +126,13 @@ client.on('ready', () => {
112
126
}
113
127
});
114
128
```
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.
116
131
117
132
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.
118
133
119
134
If you don't want to use start scripts, you can always set the node environment directly in the command line. Here's how:
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
134
151
# Heroku will run the bot in production mode
135
152
worker npm run production
136
153
```
154
+
137
155
If you're using Glitch, you can add this in your `start` script in your `package.json`:
138
-
```json
156
+
157
+
```javascript
139
158
"scripts": {
140
159
"test":"echo \"Error: no test specified\" && exit 1",
141
160
"start":"NODE_ENV=production&&node app.js"
142
161
},
143
162
```
163
+
144
164
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.
145
165
146
166
## More information
167
+
147
168
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)
-[`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)
0 commit comments