|
|
| import fs from "fs";
|
| import path from "path";
|
| import { pathToFileURL } from "url";
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| export async function loadCommands(dir) {
|
| const commands = new Map();
|
|
|
| async function loadDir(folder) {
|
| const files = fs.readdirSync(folder);
|
|
|
| for (const file of files) {
|
| const full = path.join(folder, file);
|
| const stat = fs.statSync(full);
|
|
|
| if (stat.isDirectory()) {
|
|
|
| await loadDir(full);
|
| continue;
|
| }
|
|
|
| if (!file.endsWith(".js")) continue;
|
|
|
| try {
|
| const fileUrl = pathToFileURL(full).href;
|
| const cmdModule = (await import(fileUrl)).default;
|
|
|
| if (!cmdModule || !cmdModule.command) continue;
|
|
|
| const aliases = Array.isArray(cmdModule.command)
|
| ? cmdModule.command
|
| : [cmdModule.command];
|
|
|
| for (const alias of aliases) {
|
| const key = String(alias).toLowerCase();
|
| commands.set(key, cmdModule);
|
| }
|
| } catch (err) {
|
| console.error(`❌ Gagal load command: ${full}`, err);
|
| }
|
| }
|
| }
|
|
|
| await loadDir(dir);
|
| return commands;
|
| }
|
|
|
| |
| |
|
|
| function getMessageText(msg) {
|
| return (
|
| msg.message?.conversation ||
|
| msg.message?.extendedTextMessage?.text ||
|
| msg.message?.imageMessage?.caption ||
|
| msg.message?.videoMessage?.caption ||
|
| msg.message?.buttonsResponseMessage?.selectedButtonId ||
|
| msg.message?.listResponseMessage?.singleSelectReply?.selectedRowId ||
|
| ""
|
| );
|
| }
|
|
|
| |
| |
| |
| |
| |
|
|
| export async function handleCommand(sock, msg, commands) {
|
| try {
|
| const from = msg.key.remoteJid;
|
| const isGroup = from.endsWith("@g.us");
|
| const sender = msg.key.participant || msg.key.remoteJid;
|
|
|
| const body = getMessageText(msg);
|
| if (!body) return;
|
|
|
|
|
| const prefixMatch =
|
| typeof global.prefix === "string"
|
| ? body.startsWith(global.prefix)
|
| ? [global.prefix]
|
| : null
|
| : body.match(global.prefix);
|
|
|
| if (!prefixMatch) return;
|
|
|
| const prefix = prefixMatch[0];
|
|
|
| const [rawCmd, ...args] = body
|
| .slice(prefix.length)
|
| .trim()
|
| .split(/\s+/);
|
|
|
| if (!rawCmd) return;
|
|
|
| const command = rawCmd.toLowerCase();
|
| const plugin = commands.get(command);
|
| if (!plugin) return;
|
|
|
|
|
| const quoted =
|
| msg.message?.extendedTextMessage?.contextInfo?.quotedMessage || null;
|
| const mentionedJid =
|
| msg.message?.extendedTextMessage?.contextInfo?.mentionedJid || [];
|
|
|
|
|
|
|
|
|
| const m = {
|
| ...msg,
|
| from,
|
| sender,
|
| isGroup,
|
| body,
|
| text: body,
|
| args,
|
| prefix,
|
| command,
|
| quoted,
|
| mentionedJid,
|
|
|
| |
| |
| |
| |
|
|
| reply: async (text, options = {}) => {
|
| return sock.sendMessage(
|
| from,
|
| { text: String(text) },
|
| { quoted: msg, ...options }
|
| );
|
| },
|
|
|
| |
| |
| |
| |
|
|
| send: async (content, options = {}) => {
|
| return sock.sendMessage(from, content, { quoted: msg, ...options });
|
| },
|
|
|
| |
| |
| |
|
|
| react: async (emoji) => {
|
| try {
|
| return sock.sendMessage(from, {
|
| react: { text: emoji, key: msg.key },
|
| });
|
| } catch (err) {
|
| console.error("❌ Gagal react:", err);
|
| }
|
| },
|
|
|
| |
| |
|
|
| error: async (err) => {
|
| console.error("❌ Plugin error:", err);
|
| const message =
|
| typeof err === "string" ? err : err?.message || "Terjadi kesalahan";
|
| return m.reply("⚠️ Error: " + message);
|
| },
|
| };
|
|
|
|
|
|
|
| await plugin.run(m, { conn: sock, sock });
|
| } catch (e) {
|
| console.error("❌ Error handleCommand:", e);
|
| }
|
| } |