-
Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathinstallplugin.js
More file actions
49 lines (49 loc) · 2.18 KB
/
Copy pathinstallplugin.js
File metadata and controls
49 lines (49 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import axios from 'axios';
import fs from 'fs';
import path from 'path';
export default {
command: 'addplugin',
aliases: ['installplugin', 'install'],
category: 'owner',
description: 'Install a plugin from a GitHub Gist URL (owner only)',
usage: '.addplugin <Gist URL>',
/**
* @param {object} sock - Baileys sock
* @param {object} message - the original message object
* @param {Array} args - command arguments
* @param {object} context - additional context
*/
async handler(sock, message, args, context) {
const chatId = context.chatId || message.key.remoteJid;
const text = args?.[0];
if (!text) {
return await sock.sendMessage(chatId, {
text: 'Please provide a plugin URL.\nExample: .addplugin https://gist.github.com/username/gistid'
}, { quoted: message });
}
const gistMatch = text.match(/(?:\/|gist\.github\.com\/)([a-fA-F0-9]+)/);
if (!gistMatch) {
return await sock.sendMessage(chatId, { text: '❌ Invalid plugin URL.' }, { quoted: message });
}
const gistId = gistMatch[1];
const gistURL = `https://api.github.com/gists/${gistId}`;
try {
const response = await axios.get(gistURL);
const gistData = response.data;
if (!gistData || !gistData.files) {
return await sock.sendMessage(chatId, { text: '❌ No valid files found in the Gist.' }, { quoted: message });
}
const pluginDir = path.join(process.cwd(), 'plugins');
for (const file of Object.values(gistData.files)) {
const pluginName = file.filename;
const pluginPath = path.join(pluginDir, pluginName);
await fs.promises.writeFile(pluginPath, file.content);
}
await sock.sendMessage(chatId, { text: '*✅ Successfully installed plugin from Gist.*' }, { quoted: message });
}
catch (error) {
console.error('install plugin error:', error);
await sock.sendMessage(chatId, { text: `❌ Error fetching or saving the plugin: ${error.message}` }, { quoted: message });
}
}
};