Skip to main content

command 组织

如果我还有其他命令,我该如何组织,以便更好的维护

alt text

code

为了更好地组织和维护多个命令,你可以将命令逻辑分离到不同的模块或文件中。这有助于保持代码清晰和可维护。以下是如何组织代码的建议:

目录结构

src/
commands/
buy.ts
sell.ts
create.ts
wallet.ts
index.ts

示例代码

buy.ts

import { Context } from "grammy";

export async function handleBuy(ctx: Context) {
const [token, amount] = ctx.match.split(" ");
if (!token || !amount) {
return ctx.reply("Usage: /buy <token> <amount>");
}
// 调用买入逻辑
await buyToken(token, parseFloat(amount));
ctx.reply(`Bought ${amount} of token ${token}`);
}

async function buyToken(token: string, amount: number) {
console.log(`Buying ${amount} of ${token}`);
}

sell.ts

import { Context } from "grammy";

export async function handleSell(ctx: Context) {
const [token, amount] = ctx.match.split(" ");
if (!token || !amount) {
return ctx.reply("Usage: /sell <token> <amount>");
}
// 调用卖出逻辑
await sellToken(token, amount);
ctx.reply(`Sold ${amount} of token ${token}`);
}

async function sellToken(token: string, amount: string) {
console.log(`Selling ${amount} of ${token}`);
}

create.ts

import { Context } from "grammy";

export async function handleCreate(ctx: Context) {
const [type, token, amountPrice, exp] = ctx.match.split(" ");
if (!type || !token || !amountPrice) {
return ctx.reply("Usage: /create <type> <token> <amount@price> [-exp <seconds>]");
}

const [amount, price] = amountPrice.split("@");
const expiration = exp ? parseInt(exp.split(" ")[1]) : 259200; // 默认3天

if (type === "limitbuy") {
await createLimitBuy(token, parseFloat(amount), parseFloat(price), expiration);
ctx.reply(`Created limit buy order for ${amount} of ${token} at $${price} with expiration ${expiration} seconds`);
} else if (type === "limitsell") {
await createLimitSell(token, parseFloat(amount), parseFloat(price), expiration);
ctx.reply(`Created limit sell order for ${amount} of ${token} at $${price} with expiration ${expiration} seconds`);
} else {
ctx.reply("Invalid order type. Use 'limitbuy' or 'limitsell'.");
}
}

async function createLimitBuy(token: string, amount: number, price: number, expiration: number) {
console.log(`Creating limit buy order for ${amount} of ${token} at $${price} with expiration ${expiration}`);
}

async function createLimitSell(token: string, amount: number, price: number, expiration: number) {
console.log(`Creating limit sell order for ${amount} of ${token} at $${price} with expiration ${expiration}`);
}

wallet.ts

import { Context } from "grammy";

export async function handleWallet(ctx: Context) {
// 模拟获取钱包信息
const walletInfo = {
address: "Gf8mzX...",
balance: "0 SOL",
wsolBalance: "0 WSOL",
inviteLink: "https://t.me/GMGN_sol_bot?start=i_IL0eSqHJ",
};

const message = `
钱包地址: ${walletInfo.address}
钱包余额: ${walletInfo.balance}
WSOL余额: ${walletInfo.wsolBalance}

🔗 邀请链接: ${walletInfo.inviteLink}
`;

await ctx.reply(message, {
reply_markup: {
inline_keyboard: [
[{ text: "刷新", callback_data: "refresh" }],
[{ text: "更换新钱包", callback_data: "change_wallet" }],
[{ text: "导出私钥", callback_data: "export_key" }],
[{ text: "设置转出钱包", callback_data: "set_transfer_wallet" }],
[{ text: "转换 WSOL → SOL", callback_data: "convert_wsol_sol" }],
],
},
});
}

index.ts

import { Bot } from "grammy";
import { handleBuy } from "./commands/buy";
import { handleSell } from "./commands/sell";
import { handleCreate } from "./commands/create";
import { handleWallet } from "./commands/wallet";

const bot = new Bot("<your-bot-token>");

bot.command("buy", handleBuy);
bot.command("sell", handleSell);
bot.command("create", handleCreate);
bot.command("wallet", handleWallet);

bot.start();

说明

  • 模块化:每个命令放在单独的文件中,便于维护和扩展。
  • 清晰结构:主文件 index.ts 负责注册命令,保持代码清晰。
  • 可扩展性:添加新命令时,只需创建新的文件并在 index.ts 中注册即可。