https://www.google.com/search?sca_esv=008685988fa29350&rlz=1C1GCEA_enPK1232PK1232&sxsrf=APpeQnue-dEgHmYuzmy8hlbiS7QGDJXSsA%3A1789786366156&vsint=&ntc=1&cs=1&sa=X&ved=0CBMQ2_wOahcKEwjY4oiy2Y2XAxUAAAAAHQAAAAAQaQ&biw=1280&bih=873&dpr=1&atvm=2&mstk=AUtExfCLmielRJZ_df9vcDKSpYhxF93eYX6dxws6x7ZFauP_PeQ6Ljq6oU7Qa9-I9U5iUJkSYHt8_02g1_IdO2iuvZpIEmLeRSEJZjd0x5RHbdnJhPcQYJgxkg023_DliO7mq5mpJQTNXQhCEGVcCPHuCgzzSoILLoBEbDzuSwMqHugWRyVYmti6VzxOytmmW8uUyq0X8LadGs6IAloCmi6mvt4cEvk_1HOATEp19O8Cvr7y9TyxU1HmjiKQlugg2IjYp6CRBlQAUZj7Bg&mtid=NvibavHKKfzd7_UPrdPwyAQ&csuir=1&aep=26&q=how+to+use+discord+js.selfbot.13&elids=CJ6Nppm19bqmuwEQARgBIhDeU9P8sPvhQg-rgILfTPycOPCgxry92ZYDSAFQAmoA%2CCJWjq4HP4_iqowEQARgBIhAeiM7HJuVfeQMxfsKajUgoOPDEvOW92ZYDSAFQAmoA%2CCKXwjdmx3a_7pgEQARgBIhDLJ3dnBA8bz6yCS8orTpz5OLitvOW92ZYDSAFQAmoA%2CCLGMs6SxuozhiAEQARgBIhDFmV7cySwgrXn3-vu_D7EMOIi9vOW92ZYDSAFQAmoA%2CCLPb1_yJ8qzBgAEQARgBIhC2k-L5rscQFBGQUdF5RvKNOPjHxry92ZYDSAFQAmoA%2CCLTOlJn-gLvAvAEQARgBIhCzvWZMqIR6k5zkMVnwxEQuONCBxry92ZYDSAFQAmoA%2CCLnQxtXc09SWlAEQARgBIhCHthkzhHV1QA1J-93FhWvKOODPxry92ZYDSAFQAmoA%2CCM7qp6OY2bCWsgEQARgBIhBj1YOtRiyFFH4ThAZ_48llOLitvOW92ZYDSAFQAmoA%2CCMHLs9ayivbSngEQARgBIhDMIb8rY9Tmcv2Ktqfv0iIGOPDEvOW92ZYDSAFQAmoA%2CCMaot4Lmgoy6gQEQARgBIhDl_vSQl5PZBrxBL1YRUmQLONjMvOW92ZYDSAFQAmoA%2CCMvT_u63kK_3vAEQARgBIhD0kzOvyOll3ihRqj3kr8RIOOidvOW92ZYDSAFQAmoA%2CCND8-fe-uoy5mwEQARgBIhBGcGEZI7miP2ZwCS1yk0dvOMDUvOW92ZYDSAFQAmoA%2CCND_uIL_956_jAEQARgBIhDxjwXgyJWx5oc-ZiTUY_E9OJDAxry92ZYDSAFQAmoA%2CCOO3quG4-pT-uAEQARgBIhBVecCFavm7iRv1zWgx6didOMCwxry92ZYDSAFQAmoA%2CCOTVqZ__qpTSqwEQARgBIhD-Qons8mlJPpB_XTNdIlxiOKCRxry92ZYDSAFQAmoA%2CCP3jk9LsqPivqQEQARgBIhCJl59l5KcQgSK4KHsw18R6OKi4xry92ZYDSAFQAmoA&udm=50

# 🏗️ Custom Module Design & Engineering Blueprint

This workspace runs on an **Event-Driven Micro-Kernel Pattern**. The central router (`index.js`) handles the raw Discord gateway stream and provides a mutable shared memory channel. To add features, you simply drop a new `.js` file into the `mods/` directory.

---

## 💎 1. The Core Variable Contract

Every command file must export an object containing a strict execution contract matching this precise signature layout:

```javascript
module.exports = {
  name: 'yourcommand', // Trigger keyword called via: PREFIX + name
  category: 'Misc',    // Category tag for automated organization sorting

  async execute(message, args, context) {
    // Structural logic actions go here
  }
};
```

### 🧠 The Three Immutable Object Properties:
1. **`message`**: The live `discord.js` Message object that triggered the action. Contains properties like `message.channel`, `message.guild`, and `message.author`. 
   * *Note:* During automated startup recovery sequences, the engine calls the module with `message` set to `null`. Always safeguard properties targeting the client directly using fallbacks like `message ? message.client : context.client`.
2. **`args`**: An **Array** containing every word typed after the command name, stripped of the prefix. 
   * *Trap Warning:* Never execute `.toLowerCase()` directly on `args` (it is an array, not a string). Isolate individual arguments first (e.g., `const subcommand = args[0] ? args[0].toLowerCase() : null`).
3. **`context`**: The cross-module state transport vehicle. It holds global sets, memory maps, system tracking states, and custom functions.

---

## 🛡️ 2. Defensive Programming & Safety Rules

To protect your user account token from automated anti-cheat filters, any custom code must follow these boundaries:

### Rule A: Bypassing Rate Limits & Server Blocks
Never use raw `message.edit()` inside custom files. If you call it in a server where you lack permissions or where text-editing is blocked, the API will throw a `403 Forbidden` error and crash your script runtime. Always pipe responses through the engine's safe wrapper:
```javascript
// SAFE PROTOCOL (Automatically handles slowmode pacing and permission block fallbacks)
await context.sendResponse(message, 'Your text response payload block.');
```

### Rule B: Rate Limit Traps (Pacing Delays)
If you loop actions (like cleaning logs, formatting chat history, or sending messages), you must inject a manual delay window between individual API hits to prevent token suspension loops:
```javascript
// Add this helper inside your loop to pause execution safely
await new Promise(resolve => setTimeout(resolve, 300)); // 300ms safety window
```

---

## 🚀 3. Structural Code Examples

### Example A: A Standard Command (Manual Trigger)
This file template reads arguments, runs arithmetic logic, and outputs text layouts cleanly into active chats:

```javascript
module.exports = {
  name: 'multiplier',
  category: 'General',
  async execute(message, args, context) {
    const value = parseFloat(args[0]);
    if (isNaN(value)) {
      await context.sendResponse(message, '❌ Please supply a valid numerical parameter.');
      return;
    }

    const outcome = value * 1.15;
    await context.sendResponse(message, `📈 **Calculation Complete:** Reference input \`\${value}\` multiplied by 15% offset: **${outcome}**`);
  }
};
```

### Example B: An Advanced Background Listener
If your module needs to run automatically in the background (like logging text, checking user statuses, or monitoring phrases), add the **`initBackground(context)`** hook. The micro-kernel will catch it on boot and wire it to your central message processing streams automatically:

```javascript
module.exports = {
  name: 'logger',
  category: 'Media',
  async execute(message, args, context) {
    // Code for manual trigger (e.g. tracking toggle switches)
    await context.sendResponse(message, '📊 Logger monitoring parameters active.');
  },

  // 🚀 BACKGROUND INJECTION AUTOMATION HOOK
  initBackground(context) {
    context.registerBackgroundHandler(async (message) => {
      // Wires directly to the core client stream
      // Runs automatically on every single incoming message
      if (message.author.id !== context.client.user.id && message.content === 'ping') {
        console.log(`[LOGGER BLIP] Captured target string inside channel: ${message.channel.id}`);
      }
    });
  }
};
```
