Ledis
It's a typescript library for building discord bots by decorators, based on discord.js, like discord.ts or nest.js
Index
Installation
Install library to your project
npm i ledis
Activate experimental decorators in your tsconfig.json
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
}
Usage
import {
bootstrap,
App,
Client,
Command,
CommandError,
CommandNotFound,
Arg,
ErrorArg,
Once,
DiscordClient
} from 'ledis';
const token = 'Your token';
class Commands {
@Command('hi :id')
public hi(@Arg('id') id: string){
return `hi ${id}`;
}
}
@App({
prefix: '!',
imports: [Commands]
})
class Application {
@Once('ready')
public readyHandler(@Client() client: DiscordClient) {
console.log(`Logged in as ${client?.user?.tag}!`);
}
@CommandError()
public commandError(@ErrorArg() error: Error) {
console.error(error);
}
@CommandNotFound()
public commandNotFound() {
return 'command not found!';
}
}
const main = () => bootstrap(Application).login(token);
main();
Api
Decorators
@App(AppMetadata)
Define class as root application
@App({
prefix: string | ({ message: DiscordMessage }) => Promise<string>
imports: Class[]
})
class Application {}
@Command(patternOrCommandMeta: string | CommandMeta)
Define method as command handler.
If return not undefined, send this value to channel (message.channel.send(returnedValue))
@App()
class Application {
@Command('hi')
public hi(){
return `hi!`;
}
@Command({
pattern: 'hi :id',
description: 'hi command'
exact: true,
})
public exactHi(
@Arg('id') id: string;
){
return `hi ${id}!`;
}
}
@ComandError()
Define method as command error handler
If return not undefined, send this value to channel (message.channel.send(returnedValue))
@App()
class Application {
@CommandError()
public commandErrorHandler(
@ErrorArg() error: Error
){
console.log(error);
}
}
@CommandNotFound()
Define method as command not found handler
If return not undefined, send this value to channel (message.channel.send(returnedValue))
@App()
class Application {
@CommandNotFound()
public commandNotFoundHandler(
@Message() message: DiscordMessage
){
console.log(message.content);
}
}
@Gateway(handler: (context, next) => void)
Define gateway (middleware) for command handler
@App()
class Application {
@Gateway(async (context, next) => next())
@Gateway((context, next) => {
if (context.message.content !== '!hi') return;
next();
})
@Command('hi')
public hi(){
return 'hi!'
}
}
@On(eventName: string)
Define on discord.js handler
@App()
class Application {
@On('message')
public messageHandler(
@Arguments() [message]: ArgumentsOf<"message">
){
console.log(message);
}
}
@Once(eventName: string)
Define once discord.js handler
@App()
class Application {
@Once('ready')
public readyHandler(
@Client() client: DiscordClient
){
console.log(`Logged in as ${client?.user?.tag}!`);
}
}
@Arg(argName: string)
Define parameter as command argument
@App()
class Application {
@Command('hi :id')
public hi(
@Arg('id') id: string
){
return `hi ${id}!`;
}
}
@Arguments()
Define parameter as discord.js on/once handler arguments
@App()
class Application {
@On('message')
public messageHandler(
@Arguments() [message]: ArgumentsOf<"message">
){
console.log(message);
}
}
@Client()
Define parameter as discord.js client
@App()
class Application {
@Once('ready')
public readyHandler(
@Client() client: DiscordClient
){
console.log(`Logged in as ${client?.user?.tag}!`);
}
}
@Commands()
Define parameter as LedisCommands
@App()
class Application {
@Command('help')
public help(
@Commands() commands: LedisCommands
){
const helpMessage = commands.map(...);
return helpMessage;
}
}
@ErrorArg()
Define parameter as command error
@App()
class Application {
@CommandError()
public commandErrorHandler(
@ErrorArg() error: Error
){
console.log(error);
}
}
@Message()
Define parameter as discord.js message
@App()
class Application {
@Command('hi :id')
public hi(
@Arg('id') id: string
@Message() message: DiscordMessage
){
message.reply(`hi ${id}!`);
}
}
Bootstrap
bootstrap(App: Class, discordClientOptions?: DiscordClientOptions) => DiscordClient
Function for bootstrap your Application
const main = () => bootstrap(Application).login(token);
main();