Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

v1.0.0 Command Structure #103

Open
sr229 opened this issue Sep 8, 2017 · 4 comments
Open

v1.0.0 Command Structure #103

sr229 opened this issue Sep 8, 2017 · 4 comments

Comments

@sr229
Copy link
Contributor

sr229 commented Sep 8, 2017

since the inception of this project, we've used the chalda/DiscordBot layout for making commands, which is

exports.commands= [
  'xyz'
];
exports.xyz = {}

However there's limtiations on this implementation

  • we can't use numbers in declaration names for export objects
  • it does not encourage clean code.

so to improve this, we'll be doing a class-based approach instead of the export object approach we usually adopted.

class CommandName {
    constructor(description, usage, adminOnly) {
          description: 'nya',
          usage: '<ayn>',
          adminOnly: true;
          }
     async main() {
             await ctx.createMessage('noud is cute');
           }
}

exports.commands= CommandName;

this is usually cleaner and requires less code for subcommanding compared to the exports approach

class CommandName {
    constructor(description, usage, adminOnly) {
          description: 'nya',
          usage: '<ayn>',
          adminOnly: true;
          }
   async main() {
       await ctx.createMessage('noud is cute');
         }
   async delet() {
         this;
      }
}
exports.commands = CommandName;
@sr229
Copy link
Contributor Author

sr229 commented Sep 8, 2017

CC @Ovyerus for your feedback

FYI for Karen maintainers, you don't need to implement this, feel free to use cogs and stuff or equivalent

@sr229 sr229 added this to the 1.0.0 milestone Sep 8, 2017
@sr229 sr229 added the HighPrio label Sep 8, 2017
@Ovyerus
Copy link
Contributor

Ovyerus commented Sep 8, 2017

Exactly what I was thinking.

@Ovyerus
Copy link
Contributor

Ovyerus commented Oct 26, 2017

For subcommands or commands that aren't part of the group (designated with static), we'll need to return some object so we know all the info about it.

New Proposal

const {Command, SubCommand} = require('./modules/CommandSystem'); // Or we can set `global.Command`/`global.SubCommand`.

class MyCommand {
    constructor(bot) {
        this.bot = bot; // Sets the Clara instance onto the command so we don't need to pass it to commands.
        this.description = 'The best command in the world!';
        this.owner = true;
        // ...
    }

    // Any methods called `init` will not be included in the command, and will instead be run on command setup.
    // This is equivalent to the current `exports.init = bot => {}`
    // Users can do any asynchronous setup they need in here.
    async init() {
        // ...
    }

    async subcommand() {
        return new SubCommand({
            description: 'The best subcommand in the world!',
            name: 'foo' // This is optional. By default, the name of the method will be used.
            owner: false // Will override the owner setting of the parent command if its set.
        }, async ctx => { // This is the actual function
            await ctx.createMessage('I am a banana!');
        });
    }

    static async separateCommand() {
        return new Command({
            // Some structure as SubCommand.
        }, async ctx => {
            // ...
        });
    }
}

module.exports = [MyCommand]

I was thinking of having commands extend Command, but I wouldn't know how to allow statics to be a new command, without having two separate classes ICommand and Command, although I may plan on doing this, we'll have to see.

@sr229
Copy link
Contributor Author

sr229 commented Oct 27, 2017

@Ovyerus looks good to me, if you like, you can update the spec in example/1.x if you like that to be out final structure

One little note though, I prefer the the Command system require as a global instead to lessen LoCs

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants