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

dcjs-botfuncs

v1.0.1

Published

discord bot helper - let the api do the hard work for you

Downloads

3

Readme

Discord.js Botfunctions

made by phil_not_funny

Discord: not funny#8951 Please report bugs to me via discord!

Description

Since discord likes to torture us with the new discord.js v14, I decided to create this package. This package is all about making bot-writing easier. It mainly focuses on creating server-settings, formatting messages and bot-config.

Implementing

Obviously: npm install [email protected]

You may import the functions by using

//      👇 only use when creating another Botfuncs instance
const BotfuncsType = require("dcjs-botfuncs");
const Botfuncs = new BotfuncsType();

Making a bot config and server storage:

In order to make implementation easier, your bot should have a config file. It should at least have the prop "prefix", to set a bot default-prefix, the prop "author" to give credit, and the prop "description" for context.

Example

Quick example of how to use dcjs-botfuncs:

bot-config.json: A bot default-prefix is the only prop that is required, but many people also include their token, client id, name, description, ... in their config. Example:

{
    "prefix": "!",
    "name": "Best Bot!",
    "author": "awesome people",
    "description": "does awesome things",
    "token": "your-token-here",
    "id": "your-application-id-here"
}

index.js:

/* INDEX.JS */

// import/require ...

Botfuncs.setBotConfig(file);  // 👈  file to your bot config
Botfuncs.initServers(file);   // 👈  file to store your servers
Botfuncs.validateServers();   // 👈  validates and updates the server's data (not absolutely necessary)

client.once("ready", () => {
  //ENTIRELY OPTIONAL: set Commands to use them in execCommand() later
  Botfuncs.setGlobalCommandDir("./commandsDirectory");    // 👈 set the directory with all the
  //                                                              commands inside (SEE: seperate command file example)

  const rest = new REST({version: '10'}).token(BOT_TOKEN);
  Botfuncs.putGlobalCommandsToAPI(client.user.id, rest);  // 👈 PUT the global commands on the discord api
  //                                                              and thereby making then slash-commands

  console.log("Bot is now ready");
});

client.on("interactionCreate", async (interaction) => {
  Botfuncs.onInteraction(interaction, (command, options, author, guildId) => {
    if(command === "help") return Botfuncs.execInteractionCommand("healp", guildId, ...params/*👈 your params in the interact() function */)
  })
})

client.on("messageCreate", async (message) => {
  Botfuncs.onMessage(message, (command, args, author, guildId, usedPrefix) => {
    Botfuncs.addServer(message.guildId); // 👈  will store, save and update the server the message was created on
    
    // if(usedPrefix === Botfuncs.getServerProp(guildId, "prefix"))
    //                👆 without filter, onMessage will react to commands using the SERVER-PREFIX AND CONFIG-PREFIX
    if(command === "ping") {
      //👇 "reply", message, deleteTimeout, asEmbed?, deleteReplyTimeout ... (delTimeout & delReplyTimeout: 0 = don't delete )
      return Botfuncs.sendMessage("pong", message, 4000, false, 0);
    } else if(command === "complexCommand") {
      return Botfuncs.execCommand("complexCommand", guildId, ...params /*👈 your params in the execute() function */ );
    }
    // ...
  });
});

// ...

Example of a seperate command file:

module.exports = {
  name: "commandName",                  // 👈 used as identifier
  description: "description",           
  args: [                                                         // 👈 important for slash-commands
    { name: "arg1", description: "descrp1", required: true },
    { name: "arg2", description: "descrp2" },
  ],
  private: false,                       // 👈 if this command should be hidden for the system

  execute(/* EXAMPLE PARAMETERS! Fully Customizable! */ message, args, client, prefix) {
    message.channel.send("I know it's complicated, but you'll get a hang of it!");
  },
  interact(/* EXAMPLE PARAMETERS! Fully Customizable! */ interaction, options) {
    interaction.reply("I know it's complicated, but you'll get a hang of it!");
  }
}

Documentation

Initializing the bot


must use before discord-client is ready:

  • initServers(String: serversFile) Initializes, verifies and loads the servers/guilds and other functions. serversFile - the file of the server storage
  • setBotConfig(String: configFile) Loads the bot config. configFile - the file to load the config from
  • setGlobalCommands(Command[]: globalCommands) Sets the global commands via an object. (used for methods: onMessage and onMessageAuto) globalCommands - the global commands in form of a Command-array Command should have the properties name, description, pattern, onExecute.

must use upon messageCreate:

  • onMessage(Discord.Message: message, Function: onMsg, Function: onRefuse, Object: filter) Ignores messages from unwated users and makes writing commands cleaner when using the param onMsg. message - the message onMsg - the function to perform when the user used the correct prefix and is not a bot onRefuse - the function to perform when the user did neither of the above filter - filter of the command-refuse (still in work)
  • addServer(String|Number: guildId, Boolean: nosave = false) Adds a server to the system. (best used in param "onMsg" in the function onMessage) guildId - the id of the guild (used as server identifier) nosave - if it shouldn't save the servers to the file, but rather just add it to the currently running system

should use in context of commands:

  • setGlobalCommandDir(String: directory) When you want to have some or every command in a seperate .js file, give them into one dir. Each seperate command file should have module.exports set to an object with the properties name, description and execute. See also: Example of seperate command files. directory - the path of the command directory
  • execCommand(String: name, String|Number: guildId, any[]: params) executes a command, identified by its name name - the command name (identifier) guildId - the id of the guild - if it is a server-command ...params - the params of the execute() function

should use in context of interactions:

  • putGlobalCommandsToAPI(Number: clientId, Discord.REST: rest) creates slash commands out of all your global commands clientId - the client id rest - an instance of REST from @discordjs/rest
  • clearGlobalCommandsInAPI(Number: clientId, Discord.REST: rest) deletes all the slash commands (not locally) clientId - the client id rest - an instance of REST from @discordjs/rest
  • execInteractionCommand(String: name, Number: guildId, any[]: params) executes a slash command, identified by its name name - the command name (identifier) guildId - the id of the guild - if it is a server-command ...params - the params of the execute() function

good-to-know functions:

  • sendMessage(String: reply, Message: message, Integer: delTimeout, Boolean: asAnswer, Boolean: embed, Integer: delOriginTimeout) Sends a message with the options listed above
  • sendInteractReply(String: reply, Interaction: interaction, Boolean: embed, Boolean: ephemeral, Integer: delTimeout, Boolean: defer) Sends an interaction reply the options listed above