@Command
Hướng dẫn sử dụng @Command decorator để tạo text commands cho bot.
Tổng quan
@Command decorator cho phép bạn định nghĩa text commands mà bot sẽ phản hồi. Command mặc định sử dụng prefix * và có thể tùy chỉnh.
Cú pháp cơ bản
@Command(name: string | NezonCommandOptions)
Ví dụ đơn giản
import { Injectable } from '@nestjs/common';
import { Command, AutoContext, SmartMessage } from '@n0xgg04/nezon';
import type { Nezon } from '@n0xgg04/nezon';
@Injectable()
export class PingHandler {
@Command('ping')
async onPing(@AutoContext() [message]: Nezon.AutoContext) {
await message.reply(SmartMessage.text('pong!'));
}
}
Sử dụng: *ping → Bot reply: pong!
Options
Với string (short form)
@Command('ping')
Tương đương với:
@Command({ name: 'ping' })
Với object (full options)
@Command({
name: 'ping',
aliases: ['pong', 'p'],
prefix: '!',
})
Type Definition
interface NezonCommandOptions {
name: string; // Tên command (bắt buộc)
aliases?: string[]; // Các tên thay thế
prefix?: string; // Prefix tùy chỉnh (mặc định: '*')
}
Aliases
Cho phép command có nhiều tên gọi:
@Command({ name: 'ping', aliases: ['pong', 'p'] })
async onPing(@AutoContext() [message]: Nezon.AutoContext) {
await message.reply(SmartMessage.text('pong!'));
}
Sử dụng:
*ping→pong!*pong→pong!*p→pong!
Custom Prefix
Thay đổi prefix mặc định:
@Command({ name: 'ping', prefix: '!' })
async onPing(@AutoContext() [message]: Nezon.AutoContext) {
await message.reply(SmartMessage.text('pong!'));
}
Sử dụng: !ping (thay vì *ping)
Arguments
Lấy arguments từ command với @Args():
import { Command, Args, AutoContext, SmartMessage } from '@n0xgg04/nezon';
import type { Nezon } from '@n0xgg04/nezon';
@Command('greet')
async onGreet(
@Args() args: Nezon.Args,
@AutoContext() [message]: Nezon.AutoContext,
) {
const name = args[0] ?? 'Anonymous';
await message.reply(SmartMessage.text(`Hello, ${name}!`));
}
Sử dụng: *greet John → Bot reply: Hello, John!
Lấy argument cụ thể
import { Command, Arg, AutoContext, SmartMessage } from '@n0xgg04/nezon';
import type { Nezon } from '@n0xgg04/nezon';
@Command('greet')
async onGreet(
@Arg(0) name: string | undefined,
@AutoContext() [message]: Nezon.AutoContext,
) {
const greeting = name ? `Hello, ${name}!` : 'Hello!';
await message.reply(SmartMessage.text(greeting));
}
Type:
@Arg(index: number): ParameterDecorator
// Trả về: string | undefined
Message Content
Lấy toàn bộ nội dung message (bao gồm cả command):
import { Command, MessageContent, AutoContext, SmartMessage } from '@n0xgg04/nezon';
import type { Nezon } from '@n0xgg04/nezon';
@Command('echo')
async onEcho(
@MessageContent() content: string | undefined,
@AutoContext() [message]: Nezon.AutoContext,
) {
const text = content?.replace('*echo', '').trim() ?? 'Nothing to echo';
await message.reply(SmartMessage.text(text));
}
Sử dụng: *echo Hello World → Bot reply: Hello World
Type:
@MessageContent(): ParameterDecorator
// Trả về: string | undefined
Context Injection
Command handler có thể inject nhiều context:
import { Command, User, Channel, Clan, Client, AutoContext, SmartMessage } from '@n0xgg04/nezon';
import type { Nezon } from '@n0xgg04/nezon';
@Command('info')
async onInfo(
@User() user: Nezon.User | undefined,
@Channel() channel: Nezon.Channel | undefined,
@Clan() clan: Nezon.Clan | undefined,
@Client() client: Nezon.Client,
@AutoContext() [message]: Nezon.AutoContext,
) {
const info = `
User: ${user?.username ?? 'unknown'}
Channel: ${channel?.name ?? 'unknown'}
Clan: ${clan?.name ?? 'unknown'}
`.trim();
await message.reply(SmartMessage.text(info));
}
Ví dụ hoàn chỉnh
import { Injectable } from '@nestjs/common';
import {
Command,
Args,
Arg,
MessageContent,
User,
Channel,
AutoContext,
SmartMessage,
} from '@n0xgg04/nezon';
import type { Nezon } from '@n0xgg04/nezon';
@Injectable()
export class ExampleHandler {
@Command({ name: 'ping', aliases: ['pong'] })
async onPing(@AutoContext() [message]: Nezon.AutoContext) {
await message.reply(SmartMessage.text('pong!'));
}
@Command('greet')
async onGreet(
@Args() args: Nezon.Args,
@AutoContext() [message]: Nezon.AutoContext,
) {
const name = args[0] ?? 'Anonymous';
await message.reply(SmartMessage.text(`Hello, ${name}!`));
}
@Command({ name: 'admin', prefix: '!' })
async onAdmin(
@Arg(0) action: string | undefined,
@User() user: Nezon.User | undefined,
@AutoContext() [message]: Nezon.AutoContext,
) {
if (action === 'kick') {
await message.reply(SmartMessage.text(`Kicking user: ${user?.username}`));
}
}
}
Best Practices
-
Sử dụng aliases cho commands phổ biến
@Command({ name: 'help', aliases: ['h', '?'] }) -
Validate arguments trước khi sử dụng
const name = args[0];
if (!name) {
await message.reply(SmartMessage.text('Please provide a name!'));
return;
} -
Sử dụng @AutoContext() cho message operations
@AutoContext() [message]: Nezon.AutoContext
// message.reply(), message.update(), message.delete()
API Reference
@Command()
function Command(
name: string | NezonCommandOptions
): MethodDecorator
@Args()
function Args(): ParameterDecorator
// Trả về: Nezon.Args = string[]
@Arg()
function Arg(index: number): ParameterDecorator
// Trả về: string | undefined
@MessageContent()
function MessageContent(): ParameterDecorator
// Trả về: string | undefined
Xem thêm
- @Component - Component handlers
- @On, @Once - Event listeners
- Decorators - Danh sách đầy đủ decorators