node-discord-bot
v0.0.2
Published
This package, based on Discord.js package, is meant to be used in a Node process. It could help you to register commands for you Discord Server, or crons with logging in a channel.
Downloads
2
Readme
Discord Bot
This package, based on Discord.js package, is meant to be used in a Node process. It could help you to register commands for you Discord Server, or crons with logging in a channel.
Installation
npm install --save-dev node-discord-bot
or
yarn add node-discord-bot
Moreover, you will need to setup a Discord Application on Discord Developer Console, then go to the Bot tab, click on Add Bot
and store the token, you will need it later.
To add your Bot to you Discord Server, go to the OAuth2 tab, add the bot
scope and the permissions you want, then navigate to the generated URL.
Initialize your Bot
The main class you have to use is DiscordClient
which is a singleton in your whole application.
To get an instance :
const discordClient = DiscordClient.getInstance(secretToken);
secretToken
needs to be set only once, at the first call of getInstance
.
Then, you can initialize the connection :
discordClient.init().then(() => {
console.info('Bot is running...');
});
Register a Cron
A Cron is a class that needs to extend AbstractCron
:
import { AbstractCron } from 'node-discord-bot';
export class CheckHourly extends AbstractCron {
constructor() {
super();
this.frequency = "0 * * * * *";
}
run(): void {}
}
You must set a crontab-based frequency, and implement what your cron does in the run
method.
Then, register you cron :
DiscordClient.getInstance().registerCron('<cron_name>', CheckHourly);
NB : The cron name is not used at the moment.
Register a Command
To listen to new message incoming, you must call this instruction after init :
DiscordClient.getInstance().handleMessage();
A Command is a class that needs to extend from AbstractCommand
:
export class Assign extends AbstractCommand {
private name: string;
constructor(message) {
super(message);
}
parse() {
const splitContent = this.message.content.split(" ");
this.name = splitContent.length >= 2 ? splitContent[1] : '';
}
process(): Promise<unknown> {}
}
To register the command :
DiscordClient.getInstance().registerCommand('<command_name>', Assign);
<command_name>
is used to match the class with user input
Use Discord.js function
You can get the base client with :
DiscordClient.getInstance().getClient()