UNPKG

artibot

Version:

Modern, fast and modular open-source Discord bot

200 lines 7.73 kB
import { log } from "./logger.js"; import Localizer from "artibot-localizer"; import chalk from "chalk"; import figlet from "figlet"; import * as discord from "discord.js"; import { createRequire } from 'module'; import coreModule from "./core/index.js"; import { readdirSync } from "fs"; import axios from "axios"; import path from "path"; import { fileURLToPath } from "url"; import Embed from "./embed.js"; export * from "./modules.js"; export * from "./logger.js"; export * from "./types.js"; export * from "./interactionManager.js"; export * from "./config.js"; export * from "./embed.js"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const require = createRequire(import.meta.url); const { version } = require('../package.json'); /** * Powerful Discord bot system. * @author Artivain <info@artivain.com> * @author Thomas Fournier <thomas@artivain.com> * @see https://github.com/Artivain/artibot * @see https://artibot.artivain.com * @see https://docs.artibot.artivain.com * @license GPL-3.0-or-later */ export class Artibot { /** Localizer using the default strings for Artibot */ localizer = new Localizer({ filePath: path.join(__dirname, "../locales.json") }); /** Stores the Artibot config object */ config; /** Version of Artibot */ version = version; /** Store cooldowns for the commands */ cooldowns = new discord.Collection(); /** Registered modules */ modules = new discord.Collection(); /** Discord.js client */ client; /** Instance of {@link InteractionManager} */ interactionManager; /** The token to login into Discord */ #token = ""; /** Lists of people who contributed to the Artibot */ contributors = require("../contributors.json"); /** * @deprecated Please directly use the exported {@link log} directly */ log = log; /** * @param config - Configuration object for Artibot, use {@link ArtibotConfigBuilder} to make this easily. */ constructor(config) { const { ownerId, testGuildId, lang = "en" } = config; // Verify that the owner ID is set if (!ownerId) throw new Error("You must set the owner ID."); // Verify that the test guild ID is set if (!testGuildId) throw new Error("You must set the test guild ID."); // Create a localizer for the core this.localizer.setLocale(lang); // Store config this.config = config; // Send artwork to console console.log(chalk.blue(figlet.textSync('Artibot', { font: 'ANSI Shadow', horizontalLayout: 'fitted' }))); log("Artibot", this.localizer._("Initialized!") + " v" + version, "info", true); // Register the Core module this.registerModule(coreModule); } /** * Create an embed * @param data - Data to set in the embed * @returns A new embed builder, already configured with the defaults from the config */ createEmbed = (data) => { return new Embed(this.config, data); }; /** * @param config - Advanced config for the bot * @param config.token - The login token for the Discord bot * @param config.additionalIntents - Additional intents to register in the Discord client * @method * @async */ login = async ({ token = this.#token, additionalIntents = [] }) => { if (!token) throw new Error("Token not set!"); this.#token = token; const moduleIntents = []; this.modules.forEach(module => module.additionalIntents.forEach(intent => moduleIntents.push(intent))); const intents = [...new Set([ [ discord.GatewayIntentBits.Guilds, discord.GatewayIntentBits.GuildMessages, discord.GatewayIntentBits.GuildMembers, discord.GatewayIntentBits.GuildPresences, discord.GatewayIntentBits.MessageContent ], ...additionalIntents, ...moduleIntents ])]; this.client = new discord.Client({ intents }); log("Artibot", this.localizer._("Loading event listeners..."), "log", true); const eventFiles = readdirSync(path.join(__dirname, "events")).filter(file => file.endsWith(".js")); for (const file of eventFiles) { const { name, execute, once } = await import(`./events/${file}`); if (once) { this.client.once(name, (...args) => execute(...args, this)); } else { this.client.on(name, async (...args) => await execute(...args, this)); } } this.client.login(token); }; /** * Register a module in Artibot * @param module - The module to register or a function to initialize the module * @param config - Custom configuration for the module. See module documentation to learn more. */ registerModule = (module, config = {}) => { if (typeof module == "function") { try { module = module(this, config); } catch (err) { log("Artibot", this.localizer._("Error when registering module: ") + err, "err", true); process.exit(1); } } this.modules.set(module.id, module); log("Artibot", this.localizer._("Registered module: ") + module.name, "info", true); if (module.langs != "any" && !module.langs.includes(this.config.lang)) { log("Artibot", this.localizer.__(" -> This module does not support the [[0]] language!", { placeholders: [this.config.lang] }), "warn", true); } for (const part of module.parts) { log("Artibot", `- [${part.constructor.name}] ${part.id}`, "log", true); } if (Object.entries(config).length !== 0) { this.config[module.id] = config; log("Artibot", this.localizer.__("Custom configuration for [[0]] saved.", { placeholders: [module.name] }), "log", true); } }; /** * Get latest release version of a GitHub repository * @param repo - GitHub repository to get latest version * @returns Version number, or false if repo not found or an error happens */ checkForUpdates = async (repo = "Artivain/artibot") => { const request = await axios({ method: "GET", url: `https://api.github.com/repos/${repo}/releases/latest`, responseType: "json", headers: { "User-Agent": "Artibot/" + this.version }, validateStatus: () => true }); if (request.status != 200) return false; const { data } = request; return data.name.replace("v", ""); }; /** * Get latest release version of a NPM package * @param packageName - Package name on NPM * @returns Version number, or false if package not found or an error happens * @method * @async */ checkForPackageUpdates = async (packageName = "artibot") => { const request = await axios({ method: "GET", url: `https://api.npms.io/v2/package/${packageName}`, responseType: "json", headers: { "User-Agent": "Artibot/" + this.version }, validateStatus: () => true }); if (request.status != 200) return false; const { data } = request; return data.collected.metadata.version; }; } /** @ignore */ export default Artibot; //# sourceMappingURL=index.js.map