1.0.1 • Published 1 year ago

dcjs-botfuncs v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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 discord.js@14.1.2

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
0.8.9

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago

0.8.8

1 year ago

0.8.5

1 year ago

0.8.4

1 year ago

0.8.7

1 year ago

0.8.6

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.4

1 year ago

0.7.3

1 year ago

0.7.0

1 year ago

0.7.11

1 year ago

0.7.10

1 year ago

0.7.13

1 year ago

0.7.12

1 year ago

0.7.15

1 year ago

0.7.14

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago

0.8.3

1 year ago

0.8.2

1 year ago

0.7.9

1 year ago

0.7.6

1 year ago

0.7.5

1 year ago

0.7.8

1 year ago

0.7.7

1 year ago

0.6.20

2 years ago

0.6.19

2 years ago

0.6.18

2 years ago

0.6.17

2 years ago

0.6.16

2 years ago

0.6.15

2 years ago

0.6.14

2 years ago

0.6.13

2 years ago

0.6.12

2 years ago

0.6.11

2 years ago

0.6.10

2 years ago

0.6.9

2 years ago

0.6.8

2 years ago

0.6.7

2 years ago

0.6.6

2 years ago

0.6.5

2 years ago

0.6.4

2 years ago

0.6.3

2 years ago

0.6.2

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.16

2 years ago

0.5.15

2 years ago

0.5.13

2 years ago

0.5.12

2 years ago

0.5.11

2 years ago

0.5.10

2 years ago

0.5.9

2 years ago

0.5.8

2 years ago

0.5.7

2 years ago

0.5.6

2 years ago

0.5.4

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.24

2 years ago

0.4.23

2 years ago

0.4.22

2 years ago

0.4.21

2 years ago

0.4.20

2 years ago

0.4.19

2 years ago

0.4.18

2 years ago

0.4.17

2 years ago

0.4.16

2 years ago

0.4.15

2 years ago

0.4.14

2 years ago

0.4.13

2 years ago

0.4.12

2 years ago

0.4.11

2 years ago

0.4.10

2 years ago

0.4.9

2 years ago

0.4.8

2 years ago

0.4.7

2 years ago

0.4.6

2 years ago

0.4.5

2 years ago

0.4.4

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.9

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.10

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago