artibot
Version:
Modern, fast and modular open-source Discord bot
213 lines • 6.5 kB
JavaScript
import { ApplicationCommandType, ContextMenuCommandBuilder } from "discord.js";
/** Base class for Artibot modules */
export class Module {
/** Name of the module */
name;
/** ID of this module */
id;
/** Version of the module (ex.: "1.2.3"). You should use the same as in your package.json if you want to publish it. */
version = "0.0.0";
/** List of supported languages (ex.: ["en", "fr"]). If this does not apply, set to "any". */
langs = "any";
/** List of parts of the module */
parts;
/** List of required intents */
additionalIntents = [];
/** GitHub repository of the module (ex.: "Artivain/artibot") */
repo;
/** Package name of the module on NPM (ex.: "artibot") */
packageName;
/**
* @param config - Module configuration
*/
constructor({ name, id, version, langs = "any", parts, intents = [], repo, packageName }) {
if (!name || !id || !version || !langs || !parts)
throw new Error("Missing module informations!");
this.name = name;
this.id = id;
this.version = version;
this.langs = langs;
this.parts = parts;
this.additionalIntents = intents;
this.repo = repo;
this.packageName = packageName;
}
}
/**
* Base class for module parts
*/
export class BasePart {
/** ID of the part */
id;
/** The function when the part is executed */
execute;
/** The function executed on bot startup */
init;
/**
* @param config - Part configuration
*/
constructor({ id, mainFunction, initFunction }) {
if (!id || !mainFunction)
throw new Error("Missing parameter(s)");
this.id = id;
this.execute = mainFunction;
this.init = initFunction;
}
}
/**
* Command part for a module
* @extends BasePart
*/
export class Command extends BasePart {
/** Name of the command */
name;
/** Description of the command */
description;
/** List of alternative names */
aliases = [];
/** Help text on how to use the command */
usage;
/** Minimum time (in seconds) between usages */
cooldown = 0;
/** If the command can only be executed by the owner of the bot */
ownerOnly = false;
/** Required permissions */
permissions;
/** Minimum amount of arguments */
args = 0;
/** If the command can only be executed in a guild */
guildOnly = true;
/**
* @param config - Command configuration
*/
constructor(config) {
super(config);
this.name = config.name;
this.description = config.description;
if (config.aliases)
this.aliases = config.aliases;
this.usage = config.usage;
if (config.cooldown)
this.cooldown = config.cooldown;
if (config.ownerOnly)
this.ownerOnly = config.ownerOnly;
this.permissions = config.permissions;
if (config.requiresArgs)
this.args = 1;
if (config.requiredArgs)
this.args = config.requiredArgs;
if (config.guildOnly)
this.guildOnly = config.guildOnly;
}
}
/**
* Slash command part for a module
* @extends BasePart
*/
export class SlashCommand extends BasePart {
/** Data to register into the Discord API */
data;
/** Minimum time (in seconds) between usages */
cooldown = 0;
/**
* @param config - Slash command configuration
*/
constructor({ id, data, cooldown = 0, mainFunction, initFunction }) {
if (!data)
throw new Error("Missing data parameter");
super({ id, mainFunction, initFunction });
this.data = data;
this.cooldown = cooldown;
}
}
/**
* Button interaction part for a module
* @extends BasePart
*/
export class Button extends BasePart {
/**
* @param config - Button configuration
*/
constructor({ id, mainFunction, initFunction }) {
super({ id, mainFunction, initFunction });
}
}
/**
* Message context menu option part for a module
* @extends BasePart
*/
export class MessageContextMenuOption extends BasePart {
/** Data to register into the Discord API */
data = new ContextMenuCommandBuilder()
.setType(ApplicationCommandType.Message);
/**
* @param config - Message context menu option configuration
*/
constructor({ id, name, mainFunction, initFunction }) {
if (!name)
throw new Error("Missing name parameter!");
super({ id, mainFunction, initFunction });
this.data.setName(name);
}
}
/**
* User context menu option part for a module
* @extends BasePart
*/
export class UserContextMenuOption extends BasePart {
/** Data to register into the Discord API */
data = new ContextMenuCommandBuilder()
.setType(ApplicationCommandType.User);
/**
* @param config - User context menu option configuration
*/
constructor({ id, name, mainFunction, initFunction }) {
if (!name)
throw new Error("Missing name parameter!");
super({ id, mainFunction, initFunction });
this.data.setName(name);
}
}
/**
* Select menu option part for a module
* @extends BasePart
*/
export class SelectMenuOption extends BasePart {
/**
* @param config - Select menu option configuration
*/
constructor({ id, mainFunction, initFunction }) {
super({ id, mainFunction, initFunction });
}
}
/** Configuration for a trigger group */
export class TriggerGroup extends BasePart {
/** List of triggers */
triggers;
/**
* @param config - Trigger group configuration
*/
constructor({ id, triggers, mainFunction, initFunction }) {
if (!triggers || !triggers.length)
throw new Error("Triggers cannot be empty!");
super({ id, mainFunction, initFunction });
this.triggers = triggers;
}
}
/**
* Global part for a module
* - Special part which is not managed by a event handler and only ran at startup
* @extends BasePart
*/
export class Global extends BasePart {
/**
* @param {Object} config - Config for this global
* @param {string} config.id - ID of this global
* @param {function(Artibot): void|Promise<void>} config.mainFunction - Function executed on bot startup
*/
constructor({ id, mainFunction }) {
super({ id, mainFunction });
this.init = mainFunction;
}
}
//# sourceMappingURL=modules.js.map