UNPKG

rashi-discord-bot-lib

Version:

🚀 Powerful Discord bot framework with built-in database, event handling, and utilities

116 lines 4.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.FileWatcher = void 0; const chokidar_1 = require("chokidar"); const Logger_1 = require("./Logger"); class FileWatcher { pathToWatch; webhookURL; mention; watcher; logger; constructor(pathToWatch, webhookURL, mention, logger) { this.pathToWatch = pathToWatch; this.webhookURL = webhookURL; this.mention = mention; this.logger = logger || new Logger_1.Logger(); } async start() { this.watcher = (0, chokidar_1.watch)(this.pathToWatch, { ignored: /(^|[\/\\])\../, // ignore dotfiles persistent: true }); this.watcher .on('change', (path) => this.onFileChange(path)) .on('add', (path) => this.onFileAdd(path)) .on('unlink', (path) => this.onFileDelete(path)); this.logger.info(`👀 File watcher started for: ${this.pathToWatch}`); } async stop() { if (this.watcher) { await this.watcher.close(); this.logger.info('👀 File watcher stopped'); } } async onFileChange(filePath) { this.logger.debug(`📝 File changed: ${filePath}`); await this.sendWebhook('File Changed', filePath, '🔄'); } async onFileAdd(filePath) { this.logger.debug(`📄 File added: ${filePath}`); await this.sendWebhook('File Added', filePath, '✅'); } async onFileDelete(filePath) { this.logger.debug(`🗑️ File deleted: ${filePath}`); await this.sendWebhook('File Deleted', filePath, '❌'); } async sendWebhook(action, filePath, emoji) { try { const fetch = (await Promise.resolve().then(() => __importStar(require('node-fetch')))).default; await fetch(this.webhookURL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ content: this.mention || null, embeds: [{ title: `${emoji} ${action}`, description: `\`\`\`\n${filePath}\`\`\``, color: this.getColorForAction(action), timestamp: new Date().toISOString(), footer: { text: 'Development File Watcher' } }] }) }); } catch (error) { this.logger.error('Failed to send webhook:', error); } } getColorForAction(action) { switch (action) { case 'File Added': return 0x00ff00; // Green case 'File Changed': return 0xffff00; // Yellow case 'File Deleted': return 0xff0000; // Red default: return 0x0099ff; // Blue } } } exports.FileWatcher = FileWatcher; //# sourceMappingURL=FileWatcher.js.map