Skip to content

Latest commit

 

History

History
 
 

discord-bot

discord-bot

This sample demonstrates using Mongoose to build a sample Discord bot.

Setup

Make sure you have a local Stargate instance running as described on the main page of this repo.

Running This Example

Due to Discord dependencies, this sample app requires Node >= 18.

  1. Create a .env file with the keys from .env.example and the values from the developer portal and your Discord server.*
  2. Run npm install
  3. Run npm run seed to create the Bot collection in Astra
  4. Run node ./deploy-commands.js
  5. Run node ./index.js
  • DISCORD_GUILD_ID: go into your server's settings and click on "Widget". The "Server ID" is the guildId. See screenshot 1 below.
  • DISCORD_CLIENT_ID: go to the Discord developer portal, click your bot, and then click "OAuth2". The "Application ID" is the clientId. See screenshot 2 below.
  • DISCORD_TOKEN: go to the Discord developer portal. Navigate to OAuth2 -> URL Generator", select 'bot' in the SCOPES and 'Use Slash Commands' in the 'BOT PERMISSIONS' section. Copy the URL that is generated, go to the URL on a new tab, and authorize as required. Then go back to the Discord developer portal, click on your bot under "My Applications", click on the "Bot" tab, and find the token under "Build-A-Bot". See screenshot 3 below.
  • Make sure to enable slash commands when selecting bot permissions.

For more information on setting up your bot and adding it to your server: https://discordjs.guide/preparations/setting-up-a-bot-application.html

Commands

Once the Discord bot is running, you should also see the bot "Online" in the Discord server's members list as shown in the following screenshot.

image

You should be able to execute the following commands in the "general" chat under "TEXT-CHANNELS"

  • /ping: replies "Pong!"
  • /count: returns the number of documents currently in your local MongoDB collection.
  • /createdocument: inserts a new document into your local MongoDB collection.

Below is a screenshot demonstrating executing each of the commands.

image

Anytime you add or update commands in the command folder, run step 3 again.