2.0.0-1 • Published 2 years ago
@pat.npm.js/discord-bot-framework v2.0.0-1
Description
discord-bot-framework is a basic command manager designed with discord.js
- Object oriented
- Written in TypeScript: includes typings
- Simple and easy-to-use If you need support or encounter any bugs please join our Discord server
Installation
This package requires Node.js 16.0.0 or later.
Example usage
import { Client, Command, CommandType, Parameter, ParameterType } from '@pat.npm.js/discord-bot-framework';
import { Intents } from 'discord.js';
const client = new Client({
intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_MESSAGES ]
});
client.on('ready', (ready) => console.log(`${ready.user.username} is online.`));
client.manager
.setPrefix('!')
// The bot will use this prefix to discriminate messages
.addDefaults()
// Adds commands provided by the package, including the 'help' command
.on('commandCreate', ({ name }) => console.log(`Command '${name}' created`))
.on('commandDelete', ({ name }) => console.log(`Command '${name}' deleted`))
.on('groupCreate', ({ name }) => console.log(`Group '${name}' created`))
.on('groupDelete', ({ name }) => console.log(`Group '${name}' deleted`));
client.manager.addCommand(
// If you want commands in individual files you must create a function which takes 1 argument and returns the command
client => new Command(client)
.setName('prune')
.setType(CommandType.Guild)
// The type determines where the command can be called
.addAliases('purge', 'bulkdelete')
.setDescription('Delete up to 100 messages')
.addUserPermissions('MANAGE_MESSAGES')
.addClientPermissions('MANAGE_MESSAGES')
.addParameters(
new Parameter()
.setKey('amount')
.setLabel('number')
.setDescription('The number of messages to be deleted')
.setType(ParameterType.Number)
.setRequired(true)
)
.onInvoke(async function(invocation) {
// This function will be executed when this command is invoked by a user
const number = invocation.inputs.getNumber('amount');
// Use CommandInvocation<CommandType>#inputs#... to fetch user inputs for your defined parameters
if (!invocation.message.inGuild())
return;
if (number < 2 || number > 100)
return void invocation.message.channel.send('Number must be between 2 and 100');
const deleted = await invocation.message.channel.bulkDelete(number);
invocation.message.channel.send(`Deleted ${deleted.size} messages`);
})
.onError(function(error, invocation) {
// This function will be executed in the event of an error whilst executing the invocation callback
console.warn(error);
invocation.message.channel.send('I encountered an error').catch(console.warn);
})
);
client.login('TOKEN');