owengombas / discord.ts

🤖 Create your discord bot by using TypeScript and decorators!
https://owencalvin.github.io/discord.ts/
324 stars 40 forks source link

Implemented `@On` Restrictions #4

Closed Fredi100 closed 4 years ago

Fredi100 commented 4 years ago

I have implemented the possibility to further specify the @On() decorator with a restrictor. In addition to the event it takes a function like String.prototype.startsWith and an appropriate argument. When a new message gets sent it gets compared to the given argument with the specified function. Example:

@On("message")
async onMessage(message: Message, client: Client){
    if(message.content.startsWith("!command")){
        // Do something with the command
    }
}
// The above function can now alternatively be written like this
@On("message", [String.prototype.startsWith,"!command"])
async onMessage(message: Message, client: Client){
    // Do something with the command
}

With only one command this is not really necessary but as soon as there are multiple commands, having seperate functions instead of one with a lot of if's and else's is greatly improving readability of the code.

Currently this is only supported with "message" events as it requires a text content which can be compared.

owengombas commented 4 years ago

So I implemented the @Guard (and the Prefix function) decorator ! The documentation is here! :)

Thank's for your work!

Fredi100 commented 4 years ago

That @Guard decorator looks good. Definitely a cleaner way of implementing this functionality.

Gonna close the PR as it is no longer needed.