Skip to content

TCMine/botpanel.js

Repository files navigation

drawing BotPanel.js

NPM Version NPM Unpacked Size GitHub License finbar

BotPanel.js is a simple JavaScript/TypeScript library for integrating Discord bots with Bot Panel, the bot dashboard creation platform.

Installation

Install with NPM

npm install botpanel.js

Example Usage

Creating and authenticating a client

Import Client and construct it using your application's authentication info, then use ".login()" to connect to the WebSocket and authenticate.

import { Client } from 'botpanel.js'; // CommonJS/require also supported

const client = new Client({id: id, secret: secret});
client.login();

Receiving events

You can add an event listener for each of the following events. These event names can be seen through the "OperationCodes" enumerator.
GUILD_INTERACTION - Guild dashboard is accessed and requesting data
MODIFY_GUILD_DATA - Guild data modified from dashboard
ERROR - Error from the WebSocket server
+ AUTHENTICATE & AUTH_SUCCESS

client.on('GUILD_INTERACTION', (interaction: DashboardRequestInteraction) => {
    // ...
});

Interactions

Below are examples for handling two types of interactions.

Dashboard Request Interaction (emitted through GUILD_INTERACTION):

client.on('GUILD_INTERACTION', (interaction: DashboardRequestInteraction) => {
  const guildData: object | undefined = allGuildData[interaction.guildId];
  const isBotInGuild: boolean = true;
  interaction.send({
    inGuild: isBotInGuild,
    data: guildData,
  });
});

The ID of the accessed guild can be accessed from the interaction (guildId).
Use .send() to return an object of the requested guild information. This must be used for every guild interaction event or no dashboard will be shown.

The object parameter can contain the following entries:
inGuild: boolean : Whether the bot is in the guild or not. If this is false, the dashboard will display a message to the user instead of the dashboard's inputs.
data: object : Object containing entries with the input names as keys and their input values as values. If present, this will be used to display the currently set data on the dashboard.

Guild Elements

These values should be used when requested by the guild interaction. An array of names for the requested elements is stored in .requestedElements: textChannels, voiceChannels, categories, roles
These entries must hold an array of objects with a name: string and id: string. You may optionally specify a position: number, for better sorting on the dashboard.
Role objects have an optional managed: boolean key for bot roles.

Dashboard Change Interaction (emitted through MODIFY_GUILD_DATA):

client.on('MODIFY_GUILD_DATA', (interaction: DashboardChangeInteraction) => {
    if (interaction.input.type == ComponentType.Text && interaction.input.value.length < 6) return interaction.acknowledge({success: false, message: "Text is too small!"});
    allGuildData[interaction.guildId][interaction.input.name] = interaction.input.value;
    interaction.acknowledge();
    console.log(`User (${interaction.userId}) changed guild data "${interaction.input.name}"!`);
});

These interactions hold the ID of the guild (guildId), dashboard user (userId) and information of the input, such as the name, type and value, inside an object (input).
The interaction is acknowledged with the .acknowledge() method to display a success message. This method accepts an object containing these entries:

success?: boolean is a boolean value that determines whether to show an input success or failure message on the dashboard. This is true by default.
message?: string is a string that displays a custom response message on the user's dashboard.
newValue? is a (string, number, string[]) value that replaces the user's input on the dashboard after saving. This does not work when success is false.

Disconnecting the client

The client can be disconnected with .disconnect().

client.disconnect();

Information

Bot Panel (the service that this module is made for) is made by @oneandonlyfinbar & @bunnywasnothere. You can join their Discord server here: https://discord.gg/RdPTks5gd9
This module is not affiliated or created by the developers of Bot Panel.