A bot created for the Lospec Discord server, with the source made available for community contributions or for learning to make your own similar bot.
This is version 4, rewritten from the ground up to use the latest version of discord.js and node modules.
Not all of the functionality from version 3 has been ported over, and the bots are both run concurrently.
- Fork this project on Github
- Locally clone from your fork.
- In a the project working directory run
npm installto download the required nodeJS modules - (Optional) Configure your local git repo to have the original repository as
upstreamso you can directly pull from it
Make sure you have a discord server set-up where you have admin permissions before following these steps
- Enter the Discord Developer Portal and create a
New Application. This will be your testing application. - With the Application created, select the
Bottab from the left panel. ChooseAdd bot. - Under the username, there will be a section labelled
token, copy this to your clipboard - Create a file called
.envon the root of the project - Add
DISCORD_BOT_TOKEN=to the file, then paste the token afterwards and save
If you have access to a mongodb database, you can set it up to match the production database, or you instead use the local datastore option which just uses .json files.
- Create a MongoDB database
- Download your certificate and save it to
ca-certificate.crt - Add
MONGO_URI=to the .env file with your connection string, and append&tlsCAFile=ca-certificate.crtto the end - Add
DB=LospecBotV4to the .env file - Run the bot and it will automatically create the necessary tables and documents and the appropriate values
- Edit the documents to fill in any blank values
- Reboot the bot
Please note, if you update these documents manually by editing them, you must reboot the bot immediately for the changes to be recognizes. The data is stored in memory and overwrites the document whenever it's changed.
If you don't have access to a mongodb database, or find it too confusing to set up (understandably), you can just use the local option:
- Add
LOCAL_DATA_STORAGE=trueto the .env file - Run the bot, and it will automatically create a
./_datafolder and the necessary .json files - Edit the documents to fill in any blank values
- Reboot the bot
Please note, if you update these files manually by editing them, you must reboot the bot immediately for the changes to be recognizes. The data is stored in memory and overwrites the file whenever it's changed.
To run the bot, run the command npm start from the command line from the project root.
How to expand the bot with new functionality.
Create a command that users can trigger
Add a file to the ./commands folder with the following exports:
config- an object containing the JSON configuration for a commandexecute- an async function that is called when the function is run (passes interaction as first argument)
To create a command with subcommands (e.g. /parent sub), create a folder inside the ./commands directory (e.g. ./commands/parent). Then, create a JavaScript file for each subcommand (e.g. ./commands/parent/sub.js).
Each subcommand file should have a default export which is an async function that is called when the subcommand is run. This function will receive the interaction object as its first argument.
The parent command (e.g. parent in /parent sub) should have a config export in a file named after the parent command directly in the ./commands folder (e.g. ./commands/parent.js). This file does not need an execute export if all functionality is handled by subcommands.
Respond to a message that matches a filter
Add a file to the ./responses folder with the following exports:
filter- an async function that checks if the message should trigger a responseexecute- an async function that is called when the filter matches
Add an autocomplete that provides suggestions for command options.
Add a file to the ./autocompletes folder with a default export async function that takes the interaction as its first argument. The function should return an array of choices, either as strings or as objects with { name, value } properties. The loader will handle formatting and calling interaction.respond() with the results (up to 25 entries).
Autocompletes are loaded automatically from the autocompletes folder, similar to commands. You can use the same autocomplete in multiple commands by referencing its name.
To use an autocomplete in a command, set the autocomplete property to true in the command option config, and make sure there is a corresponding autocomplete where the file name matches the option name.
To run the bot, you must have set up one of the two data storage options, explained above. Both options have identical APIs.
First you must import the appropriate document from the data module:
import {CONFIG} from '../data.js';
Modules that use a lot of properties should be set up with their own data store, which is defined in the data module:
export const MYDATAMODULENAME = new Data('my-data-module-slug');
- .get(
<string>key ) - Get the value associated with the provided key - .set(
<string>key,<any>value ) - Set the value of the provided key to the provided value - .assert(
<string>key,<bool>required [optional] ) (async) - Ensure a value exists in the data store, and if not, create a blank value and throw an error (unless required is set to false). Must be awaited. This gives the developer a place to enter the value manually. This should only be used in the top level of a file, so commands will not be loaded if the assertion fails.