Skip to content

Commit 4c8b59f

Browse files
DiamondLionLVDiamondLionLV
DiamondLionLV
authored and
DiamondLionLV
committed
upload
1 parent 4cf1fee commit 4c8b59f

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

first-bot/adding-a-config-file.md

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Adding a Config File
2+
3+
Now that you have a bot up and running, we can start splitting it into some more useful parts. And the first part of this is separating some of the variables we have defined into a configuration file, `config.json`. We'll be loading this file on boot.
4+
5+
Putting your token in a config file is fine, but **DO NOT COMMIT IT TO GITHUB** or any other public location. Usually, adding a `.gitignore` file to your project should be enough. [Here's an example](https://github.com/github/gitignore/blob/master/Node.gitignore). Simply add a line that says `config.json` to that file, save it in your project root and you should be good. [More details in this page](../other-guides/using-git-to-share-and-update-code.md).
6+
7+
## Why a config file?
8+
9+
One of the advantages of having a configuration file is that you can safely copy your bot's code into, say, hastebin.com to show people, and your token won't be in there. A second advantage is that you can upload the code to a repository like github and, as long as you ignore the config file, your bot can be shared but remain secure. We'll see that in action in a future walk through.
10+
11+
## Step 1: The config file
12+
13+
The 2 things that we can add to the config file are:
14+
15+
* The bot's token
16+
* The prefix
17+
18+
Simply take the following example, and create a new file in the same folder as your bot's file, calling it `config.json`:
19+
20+
```js
21+
{
22+
"token": "insert-bot-token-here",
23+
"prefix": "!"
24+
}
25+
```
26+
27+
## Step 2: Require the config file
28+
29+
At the top of your bot file, you need to add a line that will load this configuration, and put it in a variable. This is what it looks like:
30+
31+
```js
32+
const { Client, Intents } = require("discord.js");
33+
const client = new Client({
34+
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
35+
});
36+
const config = require("./config.json");
37+
```
38+
39+
This means that now, `config` is your configuration object. `config.token` is your token, `config.prefix` is your prefix! Simple enough.
40+
41+
## Step 3: Using `config` in your code
42+
43+
So let's use what we did earlier, and use the token from the config file, instead of putting it directly in the file. The last line of our bot looks like this:
44+
45+
```js
46+
client.login("yourToken");
47+
```
48+
49+
And we simply need to change it to this:
50+
51+
```js
52+
client.login(config.token);
53+
```
54+
55+
The other thing we have, is of course the prefix. Again from before, we have this line in our message handler:
56+
57+
```js
58+
const prefix = "!";
59+
client.on("messageCreate", (message) => {
60+
if (!message.content.startsWith(prefix) || message.author.bot) return;
61+
62+
if (message.content.startsWith(`${prefix}ping`)) {
63+
message.channel.send("pong!");
64+
} else
65+
66+
if (message.content.startsWith(`${prefix}foo`)) {
67+
message.channel.send("bar!");
68+
}
69+
});
70+
```
71+
72+
We're using `prefix` in a few places, so we need to change them all. Here's how it looks like after the changes:
73+
74+
```js
75+
client.on("messageCreate", (message) => {
76+
if (!message.content.startsWith(config.prefix) || message.author.bot) return;
77+
78+
if (message.content.startsWith(`${config.prefix}ping`)) {
79+
message.channel.send("pong!");
80+
} else
81+
82+
if (message.content.startsWith(`${config.prefix}foo`)) {
83+
message.channel.send("bar!");
84+
}
85+
});
86+
```
87+
88+
We remove the line that sets the prefix. We don't need it anymore!
89+
90+
## Changing the config
91+
92+
If you're asking yourself "but how do I change the prefix, now?" fear not, we have some help for you. We suggest you start by reading the rest of this section of the guide \("First Bot"\) and then hop on to the [Per-Server Configuration Guide on the Enmap Documentation](https://enmap.evie.dev/examples/per-server-settings)!
93+
94+
## Extending the idea
95+
96+
So is there anything else you could put in that config file? Absolutely. One thing I use it for is to store my personal user ID, so that my bot can use it to recognize me and give me exclusive access to some commands.
97+
98+
```js
99+
{
100+
"token": "insert-bot-token-here",
101+
"prefix": "!",
102+
"ownerID": "your-user-ID"
103+
}
104+
```
105+
106+
Then, in a protected command ([eval](../examples/making-an-eval-command.md) for example), I could use the following line to prevent access to all the users that think they can use it!:
107+
108+
```js
109+
if (message.author.id !== config.ownerID) return;
110+
```
111+
112+
## What's next _now_?
113+
114+
So now that we have a functional bot with a configuration file, let's add more stuff to it! Follow me to the [Command with arguments](command-with-arguments.md) for the next part!

first-bot/your-first-bot.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,4 @@ client.on("messageCreate", (message) => {
141141
client.login("yourToken");
142142
```
143143

144-
It's not good practice to have tokens and auth stuff in your code, it really should be in a separate file! <!--Head on over to Adding a [Config File]() and let's get this done.-->
144+
It's not good practice to have tokens and auth stuff in your code, it really should be in a separate file! Head on over to [Adding a Config File](adding-a-config-file.md) and let's get this done.

0 commit comments

Comments
 (0)