Giter Club home page Giter Club logo

djs-marshal's Introduction

djs-marshal

npm downloads discord

⚠️ Unmaintained, not recommended to be used anymore

A lightweight command handler for discord.js interactions

This package requires discord.js v13 or higher to be installed

Installation

# with npm
npm install djs-marshal --save
# with yarn
yarn add djs-marshal

# this package also requires discord.js
npm install discord.js --save

Documentation

You can find the full documentation here

Setup

You can set up your bot to handle commands in 2 ways:

initializeBot()

This is the recommended way to set up your bot. It sets up various things like commands' directory, logging and handlers for various events required.

import Marshal from 'djs-marshal';
import path from 'path';

const client = Marshal.initializeBot({
  // the path where slash commands are stored
  slashCommandsPath: path.join(__dirname, 'commands'),
  // the path where buttons are stored
  buttonsPath: path.join(__dirname, 'commands'),
  // the path where select menus are stored
  selectMenusPath: path.join(__dirname, 'commands'),
  // (optional) message to log on ready event
  readyMessage: 'Logged in as {tag}',
  // (optional) bot's token, will login if provided
  token: process.env.BOT_TOKEN,
  // (default: 'warn') the level of logs to log in the console
  logLevel: 'verbose',
  // (default: 'simple') how to style the logs
  logStyle: 'extended',
});

// if you didn't provide the token above, log in yourself
// client.login(process.env.BOT_TOKEN);

Slash Commands

Now in your commands' folder, you can start creating command files!

Examples

// ping.js|ts
import { defineSlashCommand } from 'djs-marshal';

// a pretty basic command. command is a discord.js
// CommandInteraction and you can use its methods like
// reply, defer, editReply, etc.
export default defineSlashCommmand({
  name: 'ping',
  description: 'Play ping-pong with me',
  commandType: 'global',
  execute (command) {
    command.reply('Pong!')
  }
});
// https://deathvenom54.github.io/djs-marshal/modules.html#SlashCommand
// guildPing.js|ts
import { defineSlashCommand } from 'djs-marshal';

// any command with a guildId specified is registered as
// as a guild command and will only work in that guild
export default defineSlashCommand({
  name: 'guildping',
  description: 'Play ping-pong with me in this server',
  commandType: 'guild',
  guildId: '873232757508157470',
  execute (command) {
    command.reply('Peng!');
  }
});
// deferredPing.js|ts
import { defineSlashCommand } from 'djs-marshal';

// you can pass in beforeExecute.defer as true to defer the interaction beforehand
export default defineSlashCommand({
  name: 'slothping',
  description: 'piiiiiinnnnnng',
  commandType: 'global',
  beforeExecute: {
    // you can also use deferEphemeral to mark the reply ephemeral
    defer: true,
  },
  execute (command) {
    setInterval(() =>{
      command.editReply('Pooooooonnnngg!');
    }, 3000)
  }
});
// secret.js|ts
import { defineSlashCommand } from 'djs-marshal';

export default defineSlashCommand({
  name: 'secret',
  description: 'Only for server moderators',
  // this command is registered in all the
  // guilds the bot is in
  commandType: 'allGuild',
  // this disables the command for anyone who
  // doesn't have any of these permissions
  allowWithPermission: ['MANAGE_SERVER'],
  async execute (command) {
    command.reply('Secret moderators stuff')
  }
});

Buttons

In your defined buttons directory, you can create message button interceptors in a similar manner to slash commands

import { defineButtonCommand } from 'djs-marshal';

export default defineButtonCommand({
  // can also be a regex, like /button-\d+/
  customId: 'my-button',
  // you can specify beforeExecute options 
  // just like in slash commands
  beforeExecute: {
    deferEphemeral: true
  },
  async execute(int) {
    await int.editReply('Beep boop!')
  }
});

Select Menus

In your defined select menus' directory, you can create select menu interceptors in a similar manner to slash commands

import { defineSelectMenuCommand } from 'djs-marshal';

export default defineSelectMenuCommand({
  // can also be a regex, like /menu-\d+/
  customId: 'my-menu',
  // you can specify beforeExecute options 
  // just like in slash commands
  beforeExecute: {
    deferEphemeral: true
  },
  async execute(int) {
    await int.editReply('Thank you, we have noted your choice!')
  }
});

Contributing

Please read CONTRIBUTING.md for the guidelines to contribute to this project.

djs-marshal's People

Contributors

0xdevansh avatar

Stargazers

 avatar  avatar Shiven Bajpai avatar

Watchers

 avatar

Forkers

dsicordt

djs-marshal's Issues

allowWithPermission not exist

Hi we try to use your command handler working well but only : allowWithPermission not working

error :

The argument of type '{ name: string; description: string; commandType: "allGuild"; allowWithPermission: string[]; execute(command: CommandInteraction<CacheType>): Promise<void>; }' is not assignable to the 'SlashCommand' type parameter.
  An object literal can only characterize known properties, and 'allowWithPermission' does not exist in type 'RegularSlashCommand'.ts(2345)

exemple use :

export default defineSlashCommand({
    name: 'secret',
    description: 'Only for server moderators',
    // this command is registered in all the
    // guilds the bot is in
    commandType: 'allGuild',
    // this disables the command for anyone who
    // doesn't have any of these permissions
    allowWithPermission: ['MANAGE_SERVER'],
    async execute(command) {
        command.reply('Secret moderators stuff')
    }
});

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.