Hooks store commands and data
npm install zihooksbash
npm install zihooks
`
or
`bash
yarn add zihooks
`
Quick Start
`javascript
const { useClient, useCommands, useLogger } = require("zihooks");
const { Client } = require("discord.js");
const winston = require("winston");
// Initialize services (first call with the instance)
const client = useClient(new Client({ intents: [] }));
const commands = useCommands(new Map());
const logger = useLogger(winston.createLogger({}));
// Access services from anywhere (subsequent calls)
const myClient = useClient(); // Returns the same instance
const myCommands = useCommands();
const myLogger = useLogger();
`
Available Hooks
$3
| Hook | Purpose | Example |
| -------------- | ------------------------------ | ------------------------------- |
| useClient | Discord.js Client instance | useClient(discordClient) |
| useCommands | Command storage and management | useCommands(commandMap) |
| useFunctions | Function registry | useFunctions(functionMap) |
| useCooldowns | Cooldown management | useCooldowns(cooldownMap) |
| useLogger | Winston logger instance | useLogger(logger) |
| useDB | Database connection | useDB(databaseConnection) |
| useConfig | Application configuration | useConfig(configObject) |
| useStatus | Status management | useStatus(statusMap) |
| useAI | AI service instance | useAI(aiService) |
| useUntil | Utility service | useUntil(untilService) |
| useGiveaways | Giveaway manager | useGiveaways(giveawayManager) |
| useWelcome | Welcome message handler | useWelcome(welcomeMap) |
| useResponder | Response handler | useResponder(responderMap) |
$3
#### Method 1: Direct Map Access
`javascript
const { useHooks, useStore } = require("zihooks");
// useHooks - for managing hook configurations
useHooks.set("myHook", { name: "example" });
const myHook = useHooks.get("myHook");
// useStore - for storing generic data
useStore.set("myData", { count: 42 });
const myData = useStore.get("myData");
`
#### Method 2: Chained Map Methods
`javascript
const { useHooks, useStore } = require("zihooks");
// useHooks - chain get() and set() operations
useHooks.set("config", { debug: true });
useHooks.set("version", "2.0.0");
const config = useHooks.get("config"); // { debug: true }
const version = useHooks.get("version"); // "2.0.0"
// useStore - chain operations
useStore.set("cache", { timestamp: Date.now() });
useStore.get("cache"); // { timestamp: ... }
// Check if key exists before getting
if (useHooks.has("config")) {
const config = useHooks.get("config");
}
// Delete entries
useHooks.delete("oldKey");
useStore.clear(); // Clear all entries
`
Usage Examples
$3
`javascript
const { useClient, useCommands, useLogger } = require("zihooks");
const { Client } = require("discord.js");
// In your main bot file
const client = new Client({ intents: ["Guilds", "GuildMessages", "DirectMessages"] });
const commands = new Map();
const logger = require("./logger"); // your winston logger
// First initialization (must provide the instance)
useClient(client);
useCommands(commands);
useLogger(logger);
client.login(process.env.TOKEN);
`
$3
`javascript
// In any other file, no need to import the instances directly
const { useClient, useCommands, useLogger } = require("zihooks");
function myFunction() {
const client = useClient(); // Get the stored client
const commands = useCommands(); // Get the stored commands
const logger = useLogger(); // Get the stored logger
logger.info("Processing with client and commands");
}
`
$3
`javascript
const { modinteraction } = require("zihooks");
client.on("messageCreate", async (message) => {
// Extend message with interaction-like features
await modinteraction(message);
// Now you can use:
console.log(message.getText()); // Get message text
console.log(message.getArgs()); // Get message arguments
console.log(message.getUserId()); // Get mentioned user IDs
console.log(message.getUsers()); // Get User objects
console.log(message.isOwner()); // Check if sender is owner
if (message.hasPermission("ADMINISTRATOR")) {
// Handle admin commands
}
});
`
Message Helper Methods
The modinteraction function extends Discord.js Message objects with additional utilities:
$3
- getText() - Get normalized message content
- getArgs() - Get message as array of arguments
- hasText(text) - Check if message contains text
$3
- getUserId() - Extract user IDs from mentions
- getChannelId() - Extract channel IDs from mentions
- getRoleId() - Extract role IDs from mentions
- getAllMentions() - Get all mentions as object
$3
- getUsers() - Resolve User objects from mentions
- getChannels() - Resolve Channel objects from mentions
- getRoles() - Resolve Role objects from mentions
$3
- isOwner() - Check if sender is bot owner
- hasPermission(perm) - Check if user has permission
$3
- isDM() - Check if message is in DMs
- isGuild() - Check if message is in a guild
$3
`javascript
const cooldowns = new Map();
message.cooldown(cooldowns, 3000); // 3 second cooldown
`
Important Notes
ā ļø Initialization: Each hook must be initialized with an instance on first call. Subsequent calls without arguments return the
cached instance.
`javascript
// First call - MUST provide the instance
const commands = useCommands(new Map());
// Subsequent calls - no argument needed
const commands = useCommands(); // Returns same instance
// Without initialization
const commands = useCommands(); // Returns false and logs error
`
API Reference
$3
Extended Map class for storing hook data:
`javascript
const { useCommands } = require("zihooks");
const commands = useCommands(new customMap());
`
$3
Extended Message interface that behaves like Discord Interaction:
`typescript
interface CommandInteraction extends Message {
user: User;
guildId: string | null;
deferReply(): Promise;
editReply(content: string | object): Promise;
followUp(content: string | object): Promise;
replyEphemeral(content: string | object): Promise;
// ... additional methods
}
``