Something everyone is dying to do… Code-driven Discord server management.
Requirements
- Discord API Token
- Some knowledge with Terraform (to understand what is going on)
- Terraform 0.13 and up
Source Code
All of my code and related files can be found in my Overture Repo.
Obtain Discord API Token
- Head over to the Discord Developer Portal
- Get logged in
- Create a new application
- Name this whatever you want
- Ex: “Terraform”
- Once in your new application, head over to the “Bot” section
- Click “Add Bot”
- Give the bot a name (or leave the default name)
- Ex: “Mr. Terraform”
- Click the “Reset Token” button
- This will give you your token, and is the only time it will be displayed
- Make sure you keep this in a safe place, and treat it like a password
- Use this token in your credentials.auto.tfvars file
Terraform Provider Information
The provider used here is discord by Chaotic Logic (Source).
Get started
Note: Make sure you have your Discord API token ready
Prepare Terraform
To get Terraform ready, create a file in the same directory called provider.tf
, and paste the following information into it:
|
|
Now, get the required provider downloaded (cd
into your Terraform directory first):
|
|
Create credentials file
Create a new directory, and cd
into it. Then create a file in there called credentials.auto.tfvars
.
Paste the following information in:
|
|
You’ll want to edit the values (in quotations) as needed, filling in discord_token
with your API token received from here, it would be best to choose a region closest to you.
Create variables file
While the credentials.auto.tfvars
assigns values to variables, the variables still need to be defined within Terraform. Create a file called variables.tf
to store these, paste the following contents (one per assigned variable from above):
|
|
Create a server
In the same directory as your provider.tf
file, create a file called createServer.tf
.
Paste the following information:
|
|
What does this do?
- The block
discord_server.my_server
creates a Discord server with a given name and region assigned from thecredentials.auto.tfvars
file. - Then saves the
id
of the server indata.discord_server.createdServerInfo
.
Create a text channel
Now, we need to add a general channel for the server!
I found the provider requires a category channel needs created, and channels be placed under that category… so we can do this in one shot:
|
|
What does this do?
- The block
discord_category_channel.general
creates a “general” category, which also depends ondata.discords_server.createdServerInfo
to contain information (ensuring the server is created before the category is)- This places the category in the server that we created by supplying its
id
- This places the category in the server that we created by supplying its
- The block
discord_text_channel.general
creates a “general” text channel, which falls under the “general” category. Just like the category depends on the server to exist, this text channel depends on the category to exist.
Create an invite
Now, we need a way to actually see all of the work Terraform will perform. Since a Bot is what created the server and everything in it, we need an invite so our own user can join the server and interact with it.
So, create yet another file, call this one createServerInvite.tf
:
|
|
What does this do?
- The
discord_invite.inviteMe
block will create a non-expiring invite code that we can use to join the server, placing the invite under the channel we created earlier (byid
) - The
output.invite_info
block will print the invite code as a string to the terminal in a cleaner, more easily-found fashion.
Let Terraform do the work, and join your server!
Now, open a terminal session, cd
into your Terraform files directory and run terraform plan
.
- This will show you what Terraform will do
- You may have to correct some errors before you can proceed
Then, just run terraform apply -auto-approve
to create the server.
In the terminal, something like this will show:
|
|
This invite_info
is what you need to join the server. To do this:
- Open your Discord client
- Click the “+” button under the list of joined servers to “Add a Server”
- Under “Have an invite already?”, click the “Join Server” button
- Enter the code given by the terminal (you don’t need a URL, just the code), and click “Join Server”!
You should now be a member of the new server, and you should also see a shiny new text channel titled general
.