@ming-suhi/djs-manager v2.4.0
I. About
A package for managing Discord Interactions; Slash Commands, Button, and Select Menus, that comes with a Database Manager for Firestore. For an in-depth documentation visit the official website. For users who want to opt for a similar package without a database manager, check out @ming-suhi/djs-local-manager from the same developer.
II. Getting Started
A. Installation
Run npm install on the command line or terminal.
npm install @ming-suhi/djs-manager
B. Setting environment
Create a
.env
file in the root directoryBOT_TOKEN = COMMANDS_FOLDER =
Get the Discord bot's token and store it as
BOT_TOKEN
.Create a folder to hold command files. Store the folder path from the root as
COMMANDS_FOLDER
.
C. Setting bot
Create an instance of Discord Client
const Discord = require('discord.js'); const client = new Discord.Client();
Attach an instance of Manager Client
const Manager = require('@ming-suhi/djs-manager'); client.msdm = new Manager.Client();
Login bot
client.login(client.msdm.token);
D. Creating commands
Create a file inside the commands folder, file name must be the same as command name
Require/import
Command
const {Command} = require('@ming-suhi/djs-manager');
Extend
Command
const myCommand = new class extends Command { constructor() { super(); // Properties here } }
Set class properties
this.name = "mycommand"; this.description = 'my custom command';
Create
execute
methodasync execute(service) { await service.send({content: 'Your command has been heard'}); }
Export created class
module.exports = myCommand;
Example
const {Command} = require('@ming-suhi/djs-manager'); const ping = new class extends Command { constructor() { super(); this.name = "ping"; this.description = 'pings bot to get latency'; } async execute(service) { await service.send({content: 'Pong'}); } } module.exports = ping;
E. Synching commands
It is suggested to sync commands on client ready. To sync commands just simply call on syncCommands
method.
client.on('ready', async() => {
client.msdm.syncCommands(client);
});
F. Setting interaction handler
This step setups an interaction handler that finds the corresponding file for the requested command. Upon receiving a slash command it calls on the executes method of that command. Upon receiving a button interaction it executes the onPress method, while receiving a select menu interaction executes the onSelect method.
client.ws.on('INTERACTION_CREATE', async interaction => {
client.msdm.handleInteraction(client, interaction);
})
III. Working with the Database
A. Creating base documents
Listen to
guildCreate
//Triggered when bot becomes a member of a guild client.on('guildCreate', async guild => { //code here });
Create guild doc. Fetch a pseduo-doc(not yet existing doc), and post to it basic infos. Create method posts name and id.
guildDb = await db.guilds.fetch(guild.id); await guildDb.create(guild);
Create member docs. Loop through guild members and do the same process as with the earlier process for the guilds.
const members = await guild.members.fetch(); for (let member of members) { const memberDb = await guildDB.members.fetch(member.id); await memberDb.create(member); }
Resulting Code:
client.on('guildCreate', async guild => { guildDb = await db.guilds.fetch(guild.id); await guildDb.create(guild); const members = await guild.members.fetch(); for (let member of members) { const memberDb = await guildDB.members.fetch(member.id); await memberDb.create(member); } });
B. Updating documents
Fetch document
const guildDb = await db.guilds.fetch(guildId);
Use update method
await guildDb.update({"updated": true});
Resulting Code:
const guildDb = await db.guilds.fetch(guildId); await guildDb.update({"updated": true});
IV. Contributing
A. Issues
This project uses GitHub Issues to track bugs and feature requests. Please search the existing issues before filing new issues to avoid duplicates. For new issues, file your bug or feature request as a new issue.
For help and questions about using this project, please open a GitHub issue.
B. Pull requests
Fork the project.
Create a topic branch from master.
Make some commits to improve the project.
Push this branch to your GitHub project.
Open a Pull Request on GitHub.
Discuss, and optionally continue committing.
V. License
MIT © 明suhi