npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

discord-controller

v1.0.0

Published

A wrapper around discord.js for easily creating discord bots with dependency injection

Downloads

26

Readme

Discord Controller

A discord bot framework with inbuilt dependency injection

Installation

  1. Install module and the discord sdk

    npm install discord-controller discord.js

  2. reflect-metadata shim is required

    npm install reflect-metadata

    and make sure to import it before using discord-controller

  3. Its important to set the following options in the tsconfig.json file of your project

    {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
    }

Example of useage

  1. Register a new bot with discord, add it to your server and copy your auth token

  2. Create a file MyCommand.ts

    import { Action, Command, Interaction } from 'discord-controller';
    
    @Command({ description: 'This is the description' })
    export class MyCommand extends Action {
      constructor() {
        super();
      }
       
      async run(message: Interaction) {
        await message.reply('This command will reply to the user');
      }
    }

    this class will register a command with the bot

  3. Create a file bot.ts

    import { createServer } from 'discord-controller';
    import { MyCommand } from './MyCommand';
       
    (async () => {
      try {
        const bot = await createServer({
          permissions: [],
          token: 'TOKEN',
          guildId: 'GUILD_ID',
          clientId: 'CLIENT_ID',
          commands: [
            MyCommand,
          ],
        });
        bot.start();
      } catch (e) {
        console.log('Unable to start bot');
      }
    })();
    
  4. Run your bot and type /mycommand in your discord server. The bot will respond with "This command will reply to the user"

More Examples

Sub Commands

If you are designing a command that has options, you can use @SubCommand in addition to @Command to add flags to your command.

import { Action, Command, Interaction, SubCommand, Flag, FlagType } from 'discord-controller';

@Command({ description: 'This is the description' })
export class Test extends Action {
  constructor() {
    super();
  }

  async run(
    message: Interaction
  ): Promise<void> {
    await message.reply('This will be send from the base command');
  }

  @SubCommand({ description: 'This is the description' })
  async subCommand(
    interaction: Interaction
  ) {
    await interaction.reply('This will be sent from the subcommand');
  }
}

This will create 2 commands for MyCommand, a default command and a subCommand command. Accesable like /mycommand default and /mycommand subcommand

/mycommand default will respond with "This will be sent from the base command" and /mycommand subcommand will respond with "This will be sent from the subcommand"

Dependency Injection

discord-controller has inbuilt dependency injection that will work automatically when using the @Service decorator

import { Command, Interaction, Service, Action } from 'discord-controller';

@Service()
export class MyService {
    myFunction() {
        return 'this is from a service';
    }
}

@Command({ description: 'description' })
export class MyCommand extends Action {
    constructor(private service: MyService) {
        super();
    }

    run(message: Interaction) {
        message.channel.send(this.service.myFunction());
    }
}

Authorisation

If you are designing a command that needs to be restricted to users with a particular role, you can use @Authorized in addition to @Command to restrict access.

import { Command, Authorized, Action } from 'discord-controller';
import { Message } from 'discord.js'

@Authorized('AllowedRole')
@Command({ description: 'description' })
export class MyCommand extends Action {
  constructor() {
    super();
  }

  run(message: Interaction) {
    message.channel.send('This command can be used by members who have the `AllowedRole` role');
  }
}

Listeners

If you are to monitor for a certain action being performed, you can use @Listen and pass in the event that you wish to monitor

import { ClientEvents, Events } from 'discord.js';
import { Listen, Listener, DiscordEvents } from 'discord-controller';

@Listen(Events.MessageCreate)
export class TestListener implements Listener<DiscordEvents.Message> {
  async listen(parameters: ClientEvents[Events.MessageCreate]) {
    const [message] = parameters;
    return message.content.includes('hello');
  }

  run(parameters: ClientEvents[Events.MessageCreate]) {
    const [message] = parameters;
    message.channel.send('someone sent a greeting');
  }
}

NOTE: If you find that the listener is not working, check that you have added the required permissions during start up

To view supported events visit the discord.js documentation