This Discord bot helps server moderators identify popular forum posts within specified forum-type channels by generating a report listing threads ordered by the number of reactions (votes) they have received.
/forum-rank
Slash Command: Lists top posts in a forum channel.channel
(required): The forum channel to analyze.number_of_posts
(optional): Limits the results to the top X posts. Defaults to all posts.direct_message
(optional): Sends the report as a direct message to the user. Defaults tofalse
(sends to channel).
- Counts only the primary reaction (default emoji) on the initial post of each thread.
- Accessible only to server moderators.
- Clone the repository (if applicable) or create a new directory:
git clone <your-repo-url> cd discord-forum-bot # OR mkdir discord-forum-bot cd discord-forum-bot
- Initialize Node.js project:
npm init -y
- Install dependencies:
npm install discord.js dotenv
-
Create a new Discord Application:
- Go to the Discord Developer Portal.
- Click on
New Application
. - Give your application a name (e.g.,
ForumRankBot
) and clickCreate
.
-
Create a Bot User:
- In your application's settings, navigate to
Bot
on the left sidebar. - Click
Add Bot
and confirm. - IMPORTANT: Under
Privileged Gateway Intents
, enablePRESENCE INTENT
,SERVER MEMBERS INTENT
, andMESSAGE CONTENT INTENT
.
- In your application's settings, navigate to
-
Get your Bot Token:
- On the
Bot
page, clickReset Token
and copy the token. Keep this token secret!
- On the
-
Get your Client ID:
- Navigate to
General Information
on the left sidebar. - Copy the
Application ID
. This is yourDISCORD_CLIENT_ID
.
- Navigate to
-
Invite the Bot to your Server:
- Go to
OAuth2
->URL Generator
. - Under
SCOPES
, selectbot
andapplications.commands
. - Under
BOT PERMISSIONS
, select the following:View Channels
Send Messages
Manage Channels
(required for the bot to check moderator permissions)Read Message History
- Copy the generated URL and paste it into your browser to invite the bot to your desired server.
- Go to
- Create a file named
.env
in the root of your project (same level aspackage.json
). - Add the following lines to your
.env
file, replacing the placeholders with your actual values:DISCORD_BOT_TOKEN=YOUR_BOT_TOKEN_HERE DISCORD_CLIENT_ID=YOUR_CLIENT_ID_HERE DISCORD_GUILD_ID=YOUR_GUILD_ID_HERE # The ID of the Discord server (guild) where you want to register slash commands for development. For global commands, this is not needed.
- To get your
GUILD_ID
, enable Developer Mode in Discord (User Settings -> Advanced -> Developer Mode), then right-click on your server icon and selectCopy ID
.
- To get your
- Start the bot:
npm start # Or for development with potential restarts (if you set up a dev script): # npm run dev
These instructions outline the general steps for deploying your Discord bot to a hosting service. Specific steps may vary depending on your chosen provider.
-
Prepare Your Project for Deployment:
- Ensure your project is version-controlled (e.g., with Git) and pushed to a remote repository (e.g., GitHub, GitLab, Bitbucket).
- Make sure all necessary dependencies are listed in your
package.json
file.
-
Choose a Hosting Provider:
- Select a hosting service that supports Node.js applications and allows for long-running processes (as Discord bots require a persistent connection).
- Examples include Railway, Render, DigitalOcean, AWS EC2, Heroku (check current free tier availability).
-
Configure Environment Variables on Your Host:
- Most hosting providers have a section for managing environment variables.
- Add the following variables, ensuring their values are correct and kept secret:
DISCORD_BOT_TOKEN
: Your bot's token from the Discord Developer Portal.DISCORD_CLIENT_ID
: Your bot's application ID.DISCORD_GUILD_ID
: (Optional) The ID of your development guild for guild-specific command registration.
- Crucially, do NOT commit your
.env
file to your version control system.
-
Set Build and Start Commands:
- Your hosting provider will typically ask for commands to build and start your application.
- Build Command:
npm install
(oryarn install
if you use Yarn). - Start Command:
node src/index.js
(This command runs your main bot file).
-
Deploy Your Application:
- Follow your hosting provider's specific instructions to deploy your project. This often involves connecting your Git repository and triggering a deployment.
-
Register Slash Commands:
- Your bot is configured to register slash commands when it starts up.
- Ensure your
DISCORD_CLIENT_ID
andDISCORD_GUILD_ID
(if applicable) are correctly set as environment variables on your hosting service. - The bot needs to be running successfully on your host for the commands to be registered with Discord.
discord-forum-bot/
├── .env
├── package.json
├── src/
│ ├── index.js
│ ├── commands/
│ │ └── forum-rank.js
│ └── utils/
│ ├── discord-api.js
│ ├── message-formatter.js
│ └── report-generator.js
└── README.md
To run unit tests (requires Jest to be installed: npm install --save-dev jest
):
npx jest
# Or to run specific tests:
npx jest src/commands/forum-rank.test.js