Discord bot
Introduction
By the end of this tutorial you will have a Discord bot hosted 24/7, for free,
forever, on Val Town, that responds to a basic /ping
Slash Command.
Val Town is a social website to write, and deploy JavaScript from the browser.
No prior coding experience required. The vast majority of this tutorial is creating the bot on Discord and pasting your keys into Val Town. The actual coding parts are just clicking Run two times to fork over two pieces of code to your Val Town account.
Video Tutorial
If you prefer videos, here’s this same tutorial in a 3-minute YouTube video.
Step 1: Create a Discord app
- Go to the Discord Developer Portal and select “New Application” in the top right
- Enter a name for your Bot, accept the terms and press Create.
Step 2: Add the bot to your server
To invite your new bot to your server, you need to generate a link to click on.
- Head to the Oauth2 > URL Generator page.
- Select the
bot
scope, then theSend Messages
permission.
- Copy the URL at the bottom of the page and paste it into a new browser tab.
- Click through the steps until you’re prompted to choose your server and press Continue.
- Press Authorize.
🎉 Your bot is now a member of your server! Now let’s get it to respond to Slash Commands.
Step 3: Login to Val Town
Val Town is a social website to write, run, and host JavaScript. You can create APIs, scheduled functions, email yourself, and persist small pieces of data — all from the browser and instantly deployed. There is a generous free plan, and no credit card required.
Step 4: Save bot secrets
- Go to the “General Information” page
- Copy your Application ID
- Go to val.town/settings/environment-variables
- Create a new environment variable called
discordAppId
and paste in the value - Copy your Public Key, saving it as
discordPublicKey
- Go to the Bot page
- Click Reset Token and accept the warning
- Copy and paste it in
val.town/settings/environment-variables as
discordBotToken
.
Step 5: Register a new Slash Command
- The embedded code below should have your name in the top-left corner. If you see anonymous, refresh this page. If you still see anonymous, ensure you’re logged into Val Town.
- Press Run to add the
/ping
command to your bot.
Step 6: Listen for Slash Commands
- Press Run to listen for Slash Commands and reply.
- Next to your val’s name, click 🔒 > Unlisted.
- In the val above, in the bottom-left corner of the val, click Script and change it to HTTP.
- Click on the URL at the bottom of that val that looks like https://username-handleDiscordInteraction.web.val.run
- Visit the General Information page for your bot and paste your copied endpoint into INTERACTIONS ENDPOINT URL and click Save Changes.
đź’¬ Try it out
Refresh Discord and try out your bot using /ping
Further Directions
Now that you have a basic Discord Bot setup, you’ll want to customize it to do exactly what you want. You’ll probably want to:
- Register a new Slash Command
- Connect your bot to APIs like OpenAI’s GPT or Dall-E
Come join us in the Val Town Discord if you get stuck, have questions, or want to show off your cool new bot!