The Next.js Discord server indexed in the web
This repo contains the code for both the Discord bot that index the posts and the front-end app
pnpm installIf you are developing locally, you need to create .env files in both the apps/web and app/bot folder. Refer to the table below for all the env vars in the project
| Name | Description | Required? |
|---|---|---|
DATABASE_URL |
The read-only connection string to connect to the DB, used to query the posts and messages | ✔️ |
REVALIDATE_SECRET |
The secret that allows remote revalidations to the app cache. This var should also be set in the bot app | ✔️ |
NEXT_PUBLIC_BASE_URL |
The URL where the app is hosted | ❌ |
| Name | Description | Required? |
|---|---|---|
DISCORD_BOT_TOKEN |
The token for the bot. If you don't have a bot yet, go to the bot project section for more details on how to create one | ✔️ |
DISCORD_CLIENT_ID |
Client ID of the bot app | ✔️ |
DEV_GUILD_ID |
The ID of the Discord server to register dev commands with pnpm dev:register-commands |
❌ |
PUBLIC_PROFILE_ROLE_ID |
The ID of the role to make Discord profiles public in the database | ❌ |
HELPER_ROLE_ID |
The ID of the role that allows for selecting answer on behalf of owner | ❌ |
MODERATOR_ROLE_ID |
The ID of the role to set moderator status in the database (also can select answer) | ❌ |
REGULAR_MEMBER_ROLE_ID |
The ID of the role to add to users when they reach the points milestone | ❌ |
INDEXABLE_CHANNEL_IDS |
Comma-separated list of forum channels to index | ✔️ |
MOD_LOG_CHANNEL_ID |
The ID of the channel to log things for mods | ❌ |
DATABASE_URL |
The connection string to connect to the DB | ✔️ |
REVALIDATE_SECRET |
The same secret from the web project |
✔️ |
WEB_URL |
The address of the web service, used to make the call to revalidate the cache | ✔️ |
| Name | Description | Required? |
|---|---|---|
DATABASE_URL |
The admin connection string to connect to the DB, used to modify the schema | ✔️ |
To run both the web and bot projects at the same time, use the following command:
pnpm devNote: You don't need to run both projects always at the same time, they can work separately
You will need your own bot to run the project locally, it is also recommended to create a new Discord server as a testing playground.
- Go to https://discord.com/developers/applications and click on New Application
- In the General Information page, copy the
APPLICATION ID, this is the value of theDISCORD_CLIENT_IDenv var - Go to the Bot page, click on Reset Token. Copy the new token and store it in the
DISCORD_BOT_TOKENenv var. DO NOT share this token anywhere - In the "Privileged Gateway Intents" section, enable
SERVER MEMBERS INTENTandMESSAGE CONTENT INTENT
To invite the bot to your own server, go to the OAuth2 > URL Generator page, select the bot scope and add the following permissions:
- Manage Roles
- Send Messages
- Send Messages in Threads
- Manage Message
- Manage Threads
- Embed Links
- Read Message History
- Add Reactions
- Use Slash Commands
Copy the Generated URL and open it in your browser
To use the context and slash commands you first need to register them in Discord. The easiest way to do that is by running this command:
pnpm dev:register-commandsNotice the dev: prefix in the command. Discord limits how many times you can register commands with their API, but by registering the command in a specific server you can do this as many times as you want. You need the DEV_GUILD_ID env var set to use this command
This project uses PostgreSQL for the database, the easiest way to get it up and running is by using Docker. Start the database with this command:
docker compose upAnd use this for the environment variable:
DATABASE_URL='postgresql://nextjsuser:nextjspassword@localhost:5432/nextjsforum'If for some reason you want to start the database from scratch you can use the following command (this will erase all the data!):
docker compose down -vOnce the database is running, add the previous DATABASE_URL variable to the packages/db/.env file and run this command:
pnpm migrate