Coder Social home page Coder Social logo

discorb's Introduction

Discorb

A flexible framework for creating a Discord bot in Typescript.

Installation

yarn add discorb discord.js

What do I need to get started?

  • To create a Discord application
  • To create a bot user attached to your application
  • To have the bot's secret token (suggested to add to the environment and loaded via dotenv)
  • A vision for the future

Simple example

import { Bot } from 'discorb'
import { Client } from 'discord.js'

const bot = new Bot({ prefix: ';;' })
  // ;;ping
  // ;;ping This will also be returned
  .register('ping', (request) => {
    request.message.reply(
      request.args.length > 0
        ? `Pong! (with args: ${request.args.map((a) => `\`${a}\``).join(' ')})`
        : 'Pong!'
    )
  })
  // ;;ping --help
  .help(
    'ping',
    `Responds with "Pong!" as well as returning any additional arguments.`
  )
  // ;;ping pong
  .register('ping pong', (request) => {
    request.message.reply('Ping pong!')
  })
  // ;;ping pong --help
  .help('ping pong', 'Just says ping pong! Simple as that!')

if (process.env.DISCORD_LOGIN == undefined) {
  throw new Error('Please provide a DISCORD_LOGIN env variable')
} else {
  const client = new Client()
  client.login(process.env.DISCORD_LOGIN)
  bot.listen(client)
}

process.on('exit', () => {
  bot.close()
})

For more, take a look at the examples directory.

Writing plugins

Discorb is made with plugins in mind! The typical pattern is below:

import { Plugin } from 'discorb'

export const MyAwesomePlugin = (commandName = 'heckyeah'): Plugin => ({
  command: commandName,
  action: async (request) => { ... },
  help: function () {
    return `Call ${this.prefix}${commandName} to get a heck yeah!`
  },
  sub: {
    ohno: {
      action: async (request) => { ... },
      help: 'Oh no!'
    }
  }
})

It can then be added like this:

import { Bot } from 'discorb'
import { MyAwesomePlugin } from 'my-awesome-plugin'

const bot = new Bot({ prefix: ';;' })
  // Use default command name
  .register(MyAwesomePlugin())
  // Use custom command name
  .register(MyAwesomePlugin('heck'))

Documentation

View documentation here.

License

This project uses the following license: Hippocratic v2.1.

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.