The Lightweight Argument Parser for discord.js (pronounced Lager)
npm
npm i lw-arger
yarn
yarn add lw-arger
Examples given are as "library agnostic" as possible.
Simplest example:
const larger = require('lw-arger')
const content = '?kick user "bad behaviour"'
const prefix = '?'
const {command, args} = larger(content, prefix)
switch(command) {
case 'kick':
const userToKick = args[0]
const reason = args[1]
kickUser(userToKick, reason) //Implementation varies
break;
}
It's possible to run Larger with a handler, which can handle commands in a functional way.
const larger = require('lw-arger')
const prefix = '?'
larger.handler({
kick: (user, reason) => {
console.log(`kicking ${user} for reason: ${reason}`)
}
})
function messageReceived(content) {
larger(content, prefix)
}
You can also pass through objects from your library that may be needed (discord.js example)
const larger = require('lw-arger')
const prefix = '?'
larger.handler({
kick: (message, user, reason) => {
message.reply(`${user} was kicked`)
//kick user
}
})
//Called from discord.js message event
function messageReceived(message) {
larger(message, message.content, prefix)
}
The handler also has support for fallback methods, which will be called if the command doesn't exist.
const larger = require('lw-arger')
const prefix = '?'
const content = '?kick user reason'
larger.handler({
fallback: () => {
console.log('command not found...')
}
})
const {command, args} = larger(content, prefix)
If there is only parameter on a handler then all of the arguments will be concatenated together which removes the need for quotes.
const larger = require('lw-arger')
const prefix = '?'
const content = '?say hello this is a long message'
larger.handler({
say: (message) => {
//message will be 'hello this is a long message'
}
})
const {command, args} = larger(content, prefix)