A wrapper for Discord.js to make your experience better and easier with typescript
npm install machina.tstypescript
import { Machina, extractClasses, arrify } from "machina.ts"
// Make the instance
const Bot = new Machina("TOKEN HERE", "< ", {name: "Hamziniii", icon: ""})
// Load everything
// I would recommand using extractClasses with the dir param, because it makes things easier
// Essentially loads all the files from the commands folder
// If you want to blacklist a file, you can
Bot.loadClasses(extractClasses("dir", "commands"))
// Bot.loadCommands() // This is for loading commands. It takes in one or more MachinaFunctions
// Start the bot
Bot.initizalize()
// Run the on message
Bot.client.on("message", msg => {
// This gets the command and other information
let command = Bot.evaluateMsg(msg)
// Command reason gives information of what is the status of the command
// This just checks if no commands are available
if(command.reason == "no commands available")
// This displays a mesasge saying that there are no commands
return Machina.noCommands(msg, command.extra)
// This just checks if multiple commands are available (if multiple commands have the same name)
else if(command.reason == "permission check passed multiple")
// This just sends a message saying to choose a specific command
return Machina.multipleCommands(msg, arrify(command.value))
// This checks if there actually are fucntions that you can call
// command.value(Bot, msg) works as well, asumming if you check to see if command.value is a singular function rather than a list
if(command.value)
// This takes the commands which could either be in a list or a single function and make it into a list
// [x, y, z, ...] or x => [x, y, z, ...] or [x]
// The for each calls them, passing in the instance of the bot and the message which is required
arrify(command.value).forEach(f => f(Bot, msg))
})
`
Basic Implementation for commands
Below is an example of how a file that uses decorators would look like.
`typescript
// commands/fun.ts
import { machinaDecoratorInfo, MachinaFunction, MachinaFunctionParameters, MachinaMessage } from "machina.ts"
// Here is the class that will contain all the fun commands
export class Fun {
// BASIC COMMAND STRUCTURE
// The decorator: you need to add monikers (the words that the user calls for the function to run) and a description
// You can add more things like strictArgs: boolean which forces the user to write parameters
// You can use shift space (autocomplete, or something like that) to get all the options that you can add
// You can also look at node_modules/machina.ts/helper/machinaFunction.ts to see all the details
@machinaDecoratorInfo({monikers: ["rN", "randomNumbers"], description: "generates a random number between 1 and 10"})
// Here you make a property for the Fun class
// It has to be static if you want it to be used as a command
// Set the funtion to type MachinaFunction
// Create an anonymous function with one parameter, params of type MachinaFunctionParameters
// If you want you can add async before the "(" to make it an asychronous function
// Essentially this is what params has:
/*
Information about the command that was inputted in the decorator
info: MachinaFunctionDefinition,
The instance of the Machina class (the bot)
Bot: Machina,
The users message (discord.js message)
msg: Message,
The Users arguments
args: MachinaArgsTypes[],
Information about the inputted argumets
argsInfo: MachinaArgsInfo
*/
static randomNumber: MachinaFunction = async (params: MachinaFunctionParameters) => {
// Generate a random integer between 1 and 10
let num = Math.floor(Math.random() * 10) + 1
// You can use the regular discord embed class, but here I am using mine
// It takes in the discord message options (all optional, but you should have at least something with contnet)
// It also takes in the user's message, and and option if I want default settings (a footer with the user's message, the color which is the same as the user's role color, timestamp, etc.)
let message = new MachinaMessage({title: "Random Number:", description: "The random number you have rolled is " + num}, params.msg, true)
// This just sends a message
message.send()
}
// After this, you can add as many commands as you want, and classes
// I would also recommend going through the machina files and looking at the types and what they mean
}
`
If you do not want decorators, I am going to assume you know what you are doing. Here is an example of what that would look like:
`typescript
// commands/fun2.ts
// When loading commands and not classes, use .loadCommands instead of .loadClasses in your starting file
// Make sure you are not importing this file or files without classes when you import classes
export const randomNumber: MachinaFunction = machinaDecoratorInfo
/ This is the same as the above example /
({monikers: ["rN", "randomNumbers"], description: "generates a random number between 1 and 10"})
/* Right here, you are basically giving the information that the class would have given you.
Fun would have been the name of the class, and randomNumber would have been the propertyKey of the class.
Since its not a decorator, you have to give that information, including the function itself
*/
("Fun", "randomNumber", (params: MachinaFunctionParameters) => {
let num = Math.floor(Math.random() * 10) + 1
let message = new MachinaMessage({title: "Random Number:", description: "The random number you have rolled is " + num}, params.msg, true)
message.send()
})
``