Giter VIP home page Giter VIP logo

da-slash's Introduction

Repository Discontinued and is Now Archived

Please Take Note When Using

Installation

Run npm install on command line or terminal.
npm install da-slash

Initialization

Set up bot and listen for commands.
index.js

const config = {
"commands": {
    "directory": "/path/to/commands", //path to commands folder
    "subcategories": "false" //if commands are divided by folders change to "true"
  },
  "bot": {
    "token": "bot_token_here"
  }    
}

const Discord = require('discord.js');
const client = new Discord.Client();
const Slash = require('da-slash');
const slash = new Slash.Client(client, config);

client.once('ready', () => {
  //updates Commands
  slash.postCommands();
})

//emitted when a slash command is detected
client.ws.on('INTERACTION_CREATE', async request => {
  const interaction = new Slash.Interaction(client, request);
  //finds the matching slash command and executes it
  slash.matchCommand(interaction); 
})

client.login(config.bot.token);

Creating Commands

A file for each command. All files should be contained in one folder or if files are separated by folders, all command folders should be under one command folder.
commandOne.js

const Slash = require('da-slash');
module.exports = new Slash.GlobalCommand({
  name: 'echo',
  description: 'sends a message',
  permissions: ["SEND_MESSAGES"],
  options: [{
    "name": "content",
    "description": "message the bot will send",
    "type": 3 // Type 3 is string
  }],
  execute(interaction) {
    // access discord.Client() through interaction.client
    const client = interaction.client;
    // access the data related to the slash command emitted
    const request = interaction.request;
    // access the arguments passed
    const content = request.data.options.find(arg => arg.name === "content").value;
    // sends message containing the argument
    interaction.sendMessage(content);
  }
})
commandTwo.js

const Slash = require('da-slash');
module.exports = new Slash.GuildCommand({
  name: 'hello',
  description: 'sends a hello message',
  guilds: ["GuildIdHere"],
  permissions: ["SEND_MESSAGES"],
  execute(interaction) {
    interaction.sendMessage("hello");
  }
})

Resources

da-slash Documentation

Discord Slash Commands Documentation

Discord.js Documentation

da-slash's People

Contributors

ming-suhi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

fasko-web

da-slash's Issues

Delete All Commands Function

While da-slash does provide a delete function, it's only setup for commands it created and that still exists.
So if I delete a command's file, I can no longer call it up and delete it, I'd have to recreate the command with the same name, then delete it.

Here's a deleteCommands function that could be added to client.js, allowing you to delete all commands initialized by the bot, either globally or per guild via await slash.deleteCommands(guild_ids, del_global).
guild_ids being an array of guild ids.
del_global being a boolean to delete global commands or not.

Function

async deleteCommands(guild_ids = false, del_global = false) {
  let deletedCommands = new Array();
  if (guild_ids) {
    let postedGuildCommands = new Array();
    for (let guild_id of guild_ids) {
      const guildCommands = await this.client.api.applications(this.client.user.id).guilds(guild_id).commands.get();
      for (let i = 0; i < guildCommands.length; i++) {
        this.client.api.applications(this.client.user.id).guilds(guild_id).commands(guildCommands[i].id).delete();
      }
      postedGuildCommands.push({ guild: guild_id, commands: guildCommands });
    }
    deletedCommands.push({ guilds: postedGuildCommands });
  }
  if (del_global) {
    const postedGlobalCommands = await this.client.api.applications(this.client.user.id).commands.get();
    for (let i = 0; i < postedGlobalCommands.length; i++) {
      this.client.api.applications(this.client.user.id).commands(postedGlobalCommands[i].id).delete();
    }
    deletedCommands.push({ global: postedGlobalCommands });
  }
  return deletedCommands;
}

Usage

client.on('ready', async () => {
  await slash.deleteCommands(['613425648685547541','197038439483310086'], true);
  slash.postCommands();
});

Originally posted by @fasko-web in #8

[commands() & findCommand()] [Subcategory Require functions grab from da-slash src node_module location]

Describe the bug
When using the categories option it requires command files from da-slash's node_modules directory, instead of the location the module is required at. IE. Commands are in './commands/folders/' but it's looking for them in './node_modules/da-slash/src/commands/folders/'

commands() & findCommand(command_name)

// ./bot.js

config = {
  commands: {
    directory: './commands',
    subcategories: 'true'
  }
}

// ./node_modules/da-slash/src/client.js

9  | async commands() {
21 |   let command = require(`${dir}/${folder}/${file}`);

37 | async findCommand(command_name) {
49 |   let command = require(`${dir}/${subfolderRef}/${file}`);

The error being produced
UnhandledPromiseRejectionWarning: Error: Cannot find module './commands/subcategory/command.js'

Expected behavior
Expected to find existing module at specified location.

Additional context
Adding require.main.require(); to line 21 and 49 in client.js fixed the issue for me

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.