1.10.8 • Published 4 years ago

sern_handler v1.10.8

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

sern_handler

sern_handler is a flexible, setup-friendly, utility package for your discord bot. Easy-to-use features and lots of customization. Supports custom events and default events

Documentation Upcoming Updates Source

Installation ▶️

npm install sern_handler

Usage ▶️

//Basic Setup
const {sern_handler, Payload} = require('sern_handler')

const payload = new Payload( 
{commands: '/yourCommandsFolder', //folder where all commands are located.
events: '/events'                 //events folder directory          
owners: ['182326315813306369'],  //String [] of Discord ID(s)
prefix: 'tcp',                   //prefix for your bot
client: client                   //instance of Discord.Client
})                              

const handler = new sern_handler(payload) //default Message event enabled

❗️ NOTE: If you want custom events enabled, add to your Payload the events header ❗️ NOTE: If you want custom events disabled, your Payload events header should be marked null

//{commands : '/yourCommandsFolder',
events: '/yourEventsFolder', //enabled
events: null //disabled
//owners: ['182326315813306369'] 

Event file

Name of file is the name of listener. For example, this file name is "message.js"

//this is a "message" event
// callback for when event is emitted has params being the payload + respective event parameters
const {handler} = require("your main file")
module.exports = {

    listener: 'on',   // the type of listener
    callback: (payload, message) => {   



        if(handler.isNotValidMessage(message)) return; //check Class Argument for more info!

        let command = await handler.fetchEmittedCommand(message) //fetches the command headers of a command

        if (command.ownerOnly) {
            if (!payload.data.owners.includes(message.author.id)) {
                return message.reply(command.notOwnerError);
            }
        }

        if(command.usesArguments) {

            let {
                usesArguments: {
                    argType = 'string',
                    array = false,
                    validate,
                    typeError,
                    validateError = "Arguments did not pass the test",
                    noArgumentsError = 'Please provide arguments',
                    
                }
            } = command

            let argument = new Argument(handler.formatMessage(message), array, argType, validate)
            argument.setArray()

            if(argument.type !== argType) {
                return message.reply("Incorrect Types!") 
            }

        return command.callback(payload, message, argument)
    }
        return command.callback(payload, message)

    }


    }

}

Using the sern_handler custom event class, You can easily customize your commands to your liking. Above is a message event template you can copy. If you need help understanding the code, join the discord! ❗️ NOTE: Overriding the default message event will be automatically done. To be sure there are no duplicate listeners, you can call the .overrideAllListeners to ensure no resources are being leeched.

Quick Docs 📜

class sern_handler

constructor : (payload : Payload)

properties :
commandsAndAliases - glorified payload.commands() method but in a property. methods :


class CustomEventHandler extends sern_handler

### constructor : (payload : Payload) ### properties :

  • commandsAndAliases - glorified payload.commands() method but in a property.

    methods :


class Payload

constructor : (data : {commands: string, events: string, owners: String[] , prefix: string, client: Discord.Client } )

methods :


class Argument

constructor : (argument : String [], array : boolean, argType : string, validate: Function )

properties:

  • utils : {check (message: Message) }
    • A bunch of util functions packed in a closure function. More info in command file section
  • type : given an array, returns the type of each in a string line.

methods :


Command Files


A barebone, minimal command file should look something like the following. In other words, these headers are required for every command :

module.exports = {
  name: "profile",                                      // name of command
  description: "shows user profile",                    // a description
  callback: async (payload, message) => {               // function to run command
  
  },
};

No arguments were attached, and the command would only activate when a user uses prefix + profile. You can build this simple skeleton into something more complex :

module.exports = {
  name: "profile", 
  description: "shows user profile",
  usesArguments: {                                    
     array: true,                                     
     argType: 'flex flex',                            
     validate: (args) => {                            
       return args?.[0] === 'Baloney' //good practice to optional chain your validate function!
     },
     validateError: "Not valid, bro",                 
     noArgumentsError: "No arguments were attached",
     typeError: "Incorrect types received"   
   },
  aliases: ["p", "prof", "profil"],
  ownerOnly: false,                                  
  notOwnerError: "You do not have perms.",            

  callback: async (payload, message, {argument, utils: {check}}) => { 
  
     console.log(argument)
  },
};

usesArguments : required if you want to enable argument handling of a commmand array : do you want an array of arguments, split by a space? argType : For each argument wanted in the command, type check each.

❗️NOTE: If you want arguments attached, the array and argType header are required. ❗️NOTE: if array is false, argType must be one string only. Supported Types :

validate : validator function with parameter of your arguments. must return Boolean. If arguments does not pass boolean test, validateError will occur validateError: Optional error message instead of default. noArgumentsError : Optional no arguments error instead of default. ownerOnly : accessible to owners given by the payload notOwnerError : Optional not owner error instead of given default. typeError : Optional not type error instead of given default.

Let's break down the callback function really quickly.

 callback: async (payload, message, argument) => { 
  
     
     //accessing argument properties by dot notation 
     console.log(argument.argument)
     argument.utils.check(message) //closure. 
  },
};
  • You can make the function async.
  • payload is your Payload instance
  • message is from a message event
  • argument is an instance of Argument. Meaning, you need to destructure it for its properties or access it by dot notation. view: Argument Argument parameter is packed with some util functions that can make coding easier. If you are unfamiliar with closures and nested functions (which will be structure of most argument.util functions), view docs here. Another resource.
//closure function

argument.utils.check(message) -> 

@returns Array : [

function isNSFW () -> @returns boolean, checks if channel is NSFW,
function memberHave(permissions: PermissionsResolvable as string) -> @returns boolean, checks if person sending message has these permissions. ,
function joinedAt() -> @returns Date, checks when member joined your discord server
function hasRole(id: String ) -> @returns boolean, checks if member has roles
  parameter id = String to check role ids 
]

Upcoming Changes / Updates

This list goes in descending order of priority.

  • Default events for sern_handler coming soon
  • The merging of event handler and custom event handler for ease of use.
  • Refactoring code
  • More argument utils functions
  • More argument customization
  • Cleaner docs.

Bugs 🐛/ Suggestions ❓

Report here

1.10.8

4 years ago

1.10.7

4 years ago

1.10.71

4 years ago

1.10.5

4 years ago

1.10.6

4 years ago

1.10.4

4 years ago

1.10.3

4 years ago

1.10.2

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.10.1

4 years ago

1.10.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago