New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ledis

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ledis

typescript library for building discord bots by decorators

  • 1.0.8
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Ledis

It's a typescript library for building discord bots by decorators, based on discord.js, like discord.ts or nest.js

Powered by discord.js
Inspired by discord.ts and 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> // prefix for commands
  imports: Class[] // classes with handlers(commands, command error handlers, on handlers and etc)
})
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', // pattern
    description: 'hi command' // command description
    exact: true, // exact, runs only if recivedArgs.length === args.length
  })
  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();

Keywords

FAQs

Package last updated on 14 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc