Hello, and welcome back to another Discord bot tutorial.
Now, since the last time I recorded those first three
videos, I have been very busy.
I have deployed the bot that I made in the previous video
via Glitch.
So you could see here, and you can
follow the link in the video's description
to the code on Glitch.
And the bot is running.
If I head on over--
I should change one thing.
If I just type in !choochoo--
And I will talk about why I am using exclamation point
in the next video when I talk about handling
multiple commands.
But for now, let's just make sure it's working.
There we go.
I get my response back.
And if I come back to here, let's look at the code.
Something rather significant has changed.
I know it's a really tiny font, so I'm going to zoom on in.
client.login-- look the token is no longer there.
Here's the code that I have in VS Code, in Visual Studio Code,
with the token right there, that I should not be showing you,
because that's a private key that I must keep private.
Returning back again to Glitch, it's there--
process.env.TOKEN.
What's going on?
So environment variables are variables
that can be configured on a particular server
separate from your code.
So your code can be released in public.
For example, you can go to the link and see this code.
But your environment variables are stored securely in a file,
or it could be other ways around it.
But a sort of convention is to store them in a file called
.env.
So if you follow this link, you won't see the contents of .env.
You will however see the contents of .env-sample,
which you can look at to see these are the environment
variables that I happen to be storing.
And the really important one here, of course, is TOKEN.
The way I'm using SERVERID and CHANNELID,
just to limit the bot to a particular server and channel,
is kind of an optional thing at this point.
But the TOKEN is a really, really critical.
And just to show you what it looks like,
I'll regenerate the token again to switch it out.
If I go here .env on Glitch, it's actually giving me
an interface to show here's the token, here's its value.
But if I click up here on Plaintext,
I can see this is what it actually looks like.
TOKEN equals that very long string.
So returning back to the code that I have
locally in Visual Studio Code.
And I will cover in a separate video how to deploy to Glitch.
I definitely would encourage you.
There's lots of different web servers
and hosting platforms that you could use to deploy your bot.
And I bet you could even run your own Raspberry Pi
or your own computer.
But that's not the topic of this video.
I just want to look at getting an environment variable set up.
So the first thing that I need to do is create a file.
I'm going to make a new file.
And I'm going to call it .env.
And in that file, I want to say TOKEN equals.
And maybe I want to be a little bit more specific.
Let's call it BOTTOKEN.
And then I want to paste the value of the token
there setting that environment variable to that value.
I can go back to the browser, go to my developer page.
I might as well regenerate.
Well, I'll regenerate it later.
I'm just going to hit Copy.
Paste it in here.
You can see it's that same token.
It's there.
And then the idea is that I could say right here, instead
of this, process.env.BOTTOKEN.
Now, this won't actually work.
Let's check and see why.
So one thing I actually want to do
is let me disable the bot on Glitch
to make sure that it's not the one running so that I can
be sure when I'm testing that the one that's running
is the one that's running here locally on my computer.
It's kind of silly, but I'm just going to comment out
all the code on Glitch.
I'm sure there's another way to disable the web application
through the interface.
And now let me say node bot.js to run this.
OK.
It's telling me an invalid token was provided.
If you're just working with Node like I am,
just a Mac laptop with a .env file,
this process.env.BOTTOKEN is not going to just work magically.
Servers like Glitch or Heroku or other kind of platforms are set
up to use a .env file.
But I've got to include special code to use that .env file.
And the code that I want to use is called dotenv.
So I want to require the Node package dotenv.
And then I can actually-- it's just one line of code--
require the dotenv package and call the config function.
That is the function you call to tell it to load the values from
the .env file.
But if I run this right now, it's going to say cannot find
the module dotenv.
That's because I have to say npm install dotenv.
So I've got to do that as well.
We could check package.json to see that now it
is a new dependency.
In addition to discord.js, I also
have dotenv as a dependency.
And now it's running.
Let's see what happens if I go back over to Discord and type
in !choochoo.
I'm getting some-- Why am I always getting choo choo back?
There we go.
I got some emojis.
Great!
So that's actually all I wanted to show you in this video.
I could think about are there other things that
are like custom settings that I don't want to package
as part of my source code.
And, for example, it could be that the channel
ID or the server ID are those.
And so I would say practice.
Try to follow along and get .env working with your code.
And in the next video, I want to look at multiple commands.
But actually-- I mean, certainly I could just
have an if statement.
That's what I'm going to start with.
So in the next video, I'm just going to show you
how I handle two commands.
If the content in the message is choochoo,
reply with one of these replies.
If the content of the message is exclamation point--
I don't know-- GIF something, then
I want to reply with a GIF.
So I'm going to show you how to post GIFs.
And that way we have two commands with an if statement.
And then the follow-up video to that--
An if statement isn't going to be very sustainable
if you want to have a bot that can
handle 10, 20, 100 commands.
How do I create a system, a structure,
for handling multiple commands in different JavaScript files
in a way that's scalable and manageable?
So stay tuned.
In the next video, we're going to make our bot post--
I don't know-- kitten GIFs or Coding Train GIFs
or who knows what.
[MUSIC PLAYING]