npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

litecommands

v2.1.6

Published

Easily create a discord bot with lite commands handler

Downloads

4

Readme

Lite Commands

Lite commands is a discord js command handler made simple, simply just install with the steps below to get started!

Initiating the client

There are lots of different options to initiate the client that we'll go over later, but below is the basic, first run bot initiation.

const Client = require("litecommands");
const path = require("path");

Client({
  commandsDir: path.join(__dirname, "Commands"), // This will load commands from the directory named "Commands"
  defaultPrefix: "!", // This is the prefix that is defaulted for every server the bot joins
});

Client.login("Your bot token"); // This can be found when creating a bot application inside the discord developer portal

Important Note

Make sure your bot has guild members intent enabled in the discord developer portal.

Advanced Configuration

Now that you've got the basic bot structure, let's get into the more advanced options of initiation the client object.

const Client = require("litecommands");
const path = require("path");

Client({
  commandsDir: path.join(__dirname, "Commands"), // This will load commands from the directory named "Commands"
  featuresDir: path.join(__dirname, "Features"), // Features are also just custom events, these can be used to create your own events when something happens
  testServers: ["Guild 1", "Guild 2"], // Test servers can be used to make certain commands only available inside the listed guilds
  botOwners: ["User 1", "User 2"], // Bot owners can be used to make certain commands only accessible to the users in this array
  ignoreBots: true / false, // This will make it where if a bot runs a command your bot will not execute.
  defaultPrefix: "!", // This is the prefix that is defaulted for every server the bot joins
  mongoUri: "Mongo Connection URL", // You can connect mongo directly within the client for easier mongodb setup
  dbOptions: {
    // Here you can define options explained in Mongoose documentation
    keepAlive: true, // Highly recommended option
  },
});

Client.login("Your bot token");

Commands

To create a command is simple, create a file called ping.js inside your commands directory. Here we can create a simple command that will reply with Pong! There are a lot of options you can provide into a command object, but we'll get into that later.

Commands/ping.js

module.exports = {
  name: "ping", // Required
  description: "Replies with pong!", // Required
  execute() {
    return "Pong!";
  },
};

And that's it! You created your first command!

Command Object Options

module.exports = {
  name: "commandname",
  description: "command description",

  permission: "Permission Node", // A full permission list can be found by doing a quick google search "DiscordJS permissions list"

  minArgs: 1, // The minimum amount of arguments required
  maxArgs: 2, // The maximum amount of arguments that can be provided,
  expectedArgs: "<user> <reason>", // Used to define what arguments are required in the syntax error message
  syntaxError: "Syntax error! Correct syntax: {PREFIX}commandname {ARGUMENTS}", // Will return when a user uses a command incorrectly

  ownerOnly: true / false, // Only bot owners can run this command
  testOnly: true / false, // Command can only be ran in test servers

  async execute({
    client, // Returns the client
    message, // Returns the message object
    args, // Returns the arguments provided
    Instance, // Returns the bot instance
    user, // Returns the user's object
    member, // Returns the user's member object
    guild, // Returns the guild the command was ran in
    channel, // Returns the channel the command was ran in
    prefix, // Returns the prefix for the guild
    text, // Returns the full text of arguments
  }) {},
};

Types of returns in a command

There are different ways you can return text or embeds after a command is ran, they are shown in examples below. Returning a string

module.exports = {
  name: "ping",
  description: "Replies with pong!",
  execute() {
    return "Hello"; // This will simply send a message in the channel "Hello"
  },
};

Returning an embed

const { MessageEmbed } = require("discord.js");

module.exports = {
  name: "ping",
  description: "Replies with pong!",
  execute() {
    return new MessageEmbed({
      title: "Hello!",
    }); // This will send a custom embed into the channel
  },
};

Returning a custom object

const { MessageEmbed } = require('discord.js')

module.exports = {
  name: 'ping',
  description: 'Replies with pong!',
  execute() {
    return {
      custom: true,
      content: "This is a normal message",
      embeds: [
        new MessageEmbed({
          title: "This is an embed"
        })
      ],
      files: [Message Attachments],
      components: [Message Components]
    }
  }
}

Returning your own object

const { MessageEmbed } = require("discord.js");

module.exports = {
  name: "ping",
  description: "Replies with pong!",
  execute({ channel }) {
    channel.send({ content: "I sent this with my own function!" });
    return;
  },
};

Features

Features can be used to create custom events when something happens within the server. Create a file in your Features directory named welcome.js

Features/welcome.js

module.exports = {
  name: "guildMemberAdd", // The event name
  async execute(member) {
    // The arguments will always be ...args, client. ex. (member, client)
    const WelcomeChannel = member.guild.channels.cache.find(
      (channel) => channel.name == "Welcome"
    );

    WelcomeChannel.send({ content: `Welcome ${member.user.tag}!` });
  },
};

Using mongoose

You can research how to create mongoose models within google, using them will be the same. You will not need to supply a connection, LiteCommands does everything for you, all that's left for you to do is create models and create amazing commands that use them!

Per-Guild Prefix Command

This will be a simple command that creates a per-guild prefix command and cache, just in case you're having trouble you can use this as a guide!

Models/GuildPrefixes.js

const mongoose = require("mongoose");

const Schema = new mongoose.Schema({
  GuildID: {
    type: String,
    required: true,
  },
  Prefix: {
    type: String,
    required: true,
  },
});

const name = "litecmdsprefixes";

module.exports = mongoose.models[name] || mongoose.model(name, Schema);

Commands/Moderation/prefix.js

const { MessageEmbed } = require("discord.js");
const PrefixSchema = require("../Models/GuildPrefixes");

module.exports = {
  name: "prefix",
  description: "Change the custom prefix for this guild",
  permission: "ADMINISTRATOR",
  async execute({ args, guild, Instance }) {
    const result = await PrefixSchema.findOne({ GuildID: guild.id });
    if (!result) {
      await PrefixSchema.create({
        GuildID: guild.id,
        Prefix: args[0],
      });
    } else {
      await PrefixSchema.findOneAndUpdate(
        {
          GuildID: guild.id,
        },
        {
          Prefix: args[0],
        }
      );
    }

    Instance.guildPrefixes[guild.id] = args[0];

    return {
      custom: true,
      embeds: [
        new MessageEmbed()
          .setTitle("Configuration")
          .setDescription(`Prefix updated to \`${args[0]}\``),
      ],
    };
  },
};

Features/PrefixCache.js

const PrefixSchema = require("../Models/GuildPrefixes");

module.exports = {
  name: "ready",
  async execute({ Instance }) {
    const prefixes = await PrefixSchema.find();

    prefixes.map((guild) => {
      Instance.guildPrefixes[guild.GuildID] = guild.Prefix;
    });
  },
};

And that's it!

You're now ready to use LiteCommands to create an awesome discord bot! Happy coding!