This project is a discord bot that, when added to your server, provides various stoic quotes.
It has a command /speak
, which lets you interact with the bot and receive insightful quotes.
It uses public API, sourced from Public APIs on GitHub, to provide stoic quotes.
Project interfaces with Discord API directly, both HTTP API
and Gateway API.
More details about usage and implementation are provided below.
This bot is not in production at the time. If you want to create your own bot with the same functionality, you can follow these steps:
- Create new application from the Discord Developer Portal.
- Configure bot permissions. Mine uses four permissions:
Send Messages
,Embed Links
,Read Message History
,Mention Everyone
. - Generate OAuth2 URL with the bot permissions above and
applications.commands
scope. This URL will allow you to invite the bot to your server. - Clone this repository and navigate to it.
- Create
.env
file and putAPPLICATION_ID
,PUBLIC_KEY
andDISCORD_TOKEN
variables in it. These values can be found on your general information page of Discord Developer Portal. - Configure "Interactions Endpoint URL" from the general information page. I use Ngrok to generate the public URL address during the development. It is as simple as running
ngrok http 3000
command. - Run
npm install
to install packages. - Run
npm register
to register slash commands for the bot.
At this point your bot should be ready to be used. You can use npm start
to run the application.
If you want to automatically rerun the server after changing the code, use npm dev
command.
- Tests are written in Jest and run using GitHub Actions on push and pull requests.
- You can also run tests locally using
npm test
. supertest
package is used for testing HTTP endpoints.- Project is configured as module so I had to use
babel-jest
to convert tested files to CJS. - Existence of
process.env.JEST_WORKER_ID
is checked in application code to detect if the test is running.
commands.js
file describes slash commands and when run as a script it registers them.- Slash command interactions are handled by
routes/interactions.js
route file. - Gateway API is handled by
gatewayClient.js
file. Bot only updates its presence and then maintains the connection. app.js
is the main entry point for the app.
The bot relies on the following external libraries and tools:
ws
: Used ingatewayClient.js
to establish a WebSocket connection to the Discord Gateway API.express
: Powers the HTTP server that handles slash command interactions.axios
: Enables HTTP requests for fetching stoic quotes and interacting with APIs.discord-interactions
: Facilitates interaction with the Discord API, specifically slash commands.
This project is licensed under the MIT License, allowing you to use, modify, and distribute the code as needed. See the LICENSE file for more details.