Coder Social home page Coder Social logo

lucienbl / discordjs-bot-starter Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 1.34 MB

Simple class based DiscordJS bot starter with two example commands.

TypeScript 97.22% JavaScript 2.78%
discord discord-bot discord-js discordjs template template-project starter starter-project starter-template simple

discordjs-bot-starter's Introduction

DiscordJS Bot Starter

Simple class based DiscordJS bot starter with two example commands.

Commands

  • !ping : Returns gateway ping
  • !announce : Sends a message in a given channel

How it works?

Each command is based in a class. To create a new command you just need to create it's corresponding class and enjoy. ๐ŸŽ‰

How to start?

  1. Clone the project
  2. Run yarn
  3. Create a .env file and paste & replace the content of .env.example
  4. Run yarn start:watch for development.

How to add a new command?

Create the corresponding command file in the /commands directory and follow the example structure. The bot will automatically take all the commands exported from /commands/index.ts.

How to structure a command class?

This is the basic structure of a command class :

class HelloCommand extends Command {
  
  /**
   * The constructor will basically receive all the command metadata, what the command should do etc... It's the command configuration !
   **/
  constructor(message: Message) {
    super(message, {
      command: "hello", // this is to what the bot should react to.
      args: [ // here you add an array arguments (optional).
        {
          key: "channel", // the internal argument key
          description: "The channel where the hello should be sent.",
          required: true // if the argument is required or not
        },
      ],
      description: "Says \"hello\" in a specified channel." 
    })
  }

  /**
   * This is the main command handler, this is the method that will be called when the command is executed.
   **/
  handler = async () => {
    // Get the channel object from the command arguments
    const channel: TextChannel = <TextChannel>this.message.guild.channels.resolve(this.argument("channel").value.match(/[0-9]+/g)[0]);

    // The class has two important methods : message & argument
    // this.message : returns the message object that hired the command
    // this.argument("argument key") : returns an argument by it's key

    // You can throw errors that will be returned to the used !
    if (!channel) throw new Error("Unrecognized channel !");

    // Delete the message that hired the command
    await this.message.delete();
    
    // Send the Hello World message
    await channel.send("Hello world !");

    // Feedback "Done !" to the user and delete the message after 1,5 seconds  
    await this.message.channel.send("Done !").then(msg => msg.delete({ timeout: 1500 }));
  }
}

Please see the two examples in the starter project, and feel free to contact me (open an issue?) for any questions ! ๐Ÿ˜Š

discordjs-bot-starter's People

Contributors

lucienbl avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.