• Townie
    AI
  • Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
boucher

boucher

discord-bot

Public
Like
discord-bot
Home
Code
19
vt-discord
4
.cursorrules
.env.example
.vtignore
DISCORD_BOT_SETUP.md
README.md
anthropic-service.js
api-server.js
database.js
deno.json
discord-client.js
discord-commands.js
index.js
interactions-endpoint.js
link-analyzer.js
package.json
query-service.js
C
val-town-cron.js
val-town-deploy.js
Branches
1
Pull requests
Remixes
History
Environment variables
7
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
DISCORD_BOT_SETUP.md
Code
/
DISCORD_BOT_SETUP.md
Search
5/2/2025
DISCORD_BOT_SETUP.md

Step-by-Step Discord Bot Setup Guide

This guide will walk you through creating a Discord bot and setting it up to work with the Discord DM Analyzer.

1. Create a Discord Application

  1. Go to the Discord Developer Portal
  2. Click the "New Application" button in the top-right corner
  3. Enter a name for your application (e.g., "DM Analyzer")
  4. Accept the terms and click "Create"

2. Set Up the Bot

  1. In the left sidebar, click on "Bot"
  2. Click the "Add Bot" button on the right
  3. Confirm by clicking "Yes, do it!"
  4. Under the bot's username, you'll see a "Reset Token" button
  5. Click "Reset Token" and confirm
  6. Copy the token that appears - this is your DISCORD_TOKEN
  7. Under "Privileged Gateway Intents", enable:
    • MESSAGE CONTENT INTENT
    • SERVER MEMBERS INTENT
  8. Scroll down and click "Save Changes"

3. Get Your Application ID

  1. In the left sidebar, click on "General Information"
  2. Under "Application ID", click the "Copy" button
  3. This is your DISCORD_CLIENT_ID

4. Get Your User ID

  1. Open Discord
  2. Go to User Settings (gear icon next to your name)
  3. Select "Advanced" in the left sidebar
  4. Enable "Developer Mode"
  5. Close User Settings
  6. Right-click on your username anywhere in Discord
  7. Select "Copy ID"
  8. This is your DISCORD_USER_ID

5. Get Your Spouse's User ID

  1. Right-click on your spouse's username in Discord
  2. Select "Copy ID"
  3. This is your SPOUSE_USER_ID

6. Update Environment Variables

  1. In your project directory, create a .env file if you haven't already
  2. Add the following variables:
    DISCORD_TOKEN=your_discord_token_here
    DISCORD_CLIENT_ID=your_client_id_here
    DISCORD_USER_ID=your_user_id_here
    SPOUSE_USER_ID=spouse_user_id_here
    

7. Get OpenAI API Key

  1. Go to OpenAI's website
  2. Sign up or log in
  3. Navigate to the API keys section
  4. Create a new API key
  5. Copy the key
  6. Add it to your .env file:
    OPENAI_API_KEY=your_openai_api_key_here
    

8. Register Bot Commands

  1. In your project directory, run:
    node discord-commands.js
    
  2. You should see "Successfully registered application commands."

9. Invite the Bot to Your Server

  1. In the Discord Developer Portal, click on "OAuth2" in the left sidebar
  2. Click on "URL Generator"
  3. Under "Scopes", select:
    • bot
    • applications.commands
  4. Under "Bot Permissions", select:
    • Read Messages/View Channels
    • Send Messages
    • Read Message History
  5. Copy the generated URL at the bottom
  6. Paste the URL in your browser
  7. Select a server where you want to add the bot
  8. Click "Authorize"
  9. Complete the CAPTCHA if prompted

10. Deploy to Val.town (Optional)

  1. Install the Val.town CLI:
    npm install -g val-town-cli
    
  2. Authenticate with Val.town:
    vt login
    
  3. Deploy the application:
    node val-town-deploy.js
    
  4. Set up environment variables in Val.town:
    • Go to the Val.town dashboard
    • Find your deployed functions
    • Add the same environment variables from your .env file
  5. Set up a scheduled task for periodic DM fetching:
    • Create a new scheduled task in Val.town
    • Use the discordDMBotCron function
    • Choose a schedule (e.g., every 6 hours)

11. Test Your Bot

  1. Start the local server:

    node index.js
    
  2. In Discord, use one of the slash commands:

    • /search What did we say about the IKEA table?
    • /links sofa
    • /categories
    • /analyze Summarize our furniture discussions
  3. You can also manually trigger a DM fetch:

    node val-town-cron.js
    

Troubleshooting

Bot not responding to commands

  • Make sure the bot is running (node index.js)
  • Check if commands were registered successfully
  • Verify that the bot has proper permissions

Cannot fetch DMs

  • Ensure both you and your spouse have DMed each other at least once
  • Verify that all tokens and IDs in the .env file are correct
  • Check Discord developer portal for any token or permission issues

Val.town deployment issues

  • Make sure you're logged in to Val.town CLI
  • Check for any errors in the deployment logs
  • Verify that environment variables are set correctly in Val.town

Next Steps

  1. Customize the bot's responses by editing the API endpoints in api-server.js
  2. Add more categories for link classification in database.js
  3. Enhance the AI queries by modifying prompts in query-service.js
  4. Create a web interface that connects to your API endpoints
FeaturesVersion controlCode intelligenceCLI
Use cases
TeamsAI agentsSlackGTM
ExploreDocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.