Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fixed tdollskin2 output path #126

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 84 additions & 154 deletions src/commands/tdoll/register.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import { IRegister, ParamsType } from '../../types';
import {
getMatchedTDollData,
getMatchedTDollDataWithCategory,
getTDollDataRes,
getTDollDataWithCategoryRes,
getTDollSkinReplyText,
printTDoll2Png,
printTDollSkin2Png,
} from './utils/utils';
import { TDollSvc } from './services/tdoll.service';
import { TDollSkinSvc } from './services/tdollskin.service';
import { logger } from '../../utils/logger';
Expand All @@ -18,120 +9,68 @@ import {
TDOLL_SKIN_NOT_FOUND_MSG,
} from './types/constants';
import { getStaticHttpPath } from '../../utils/cmdreq';

// 工具函数
const getQueryParams = (params: ParamsType): string[] =>
Array.from(params.keys()).map(String);

const validateParams = async (
ctx: any,
expectedSize: number,
errorMessage: string
): Promise<boolean> => {
if (ctx.params.size !== expectedSize) {
await ctx.reply(errorMessage);
return false;
}
return true;
};

const handleError = async (ctx: any, error: any, commandName: string) => {
logger.error(`${commandName} command error: ${error}`);
await ctx.reply('查询失败,请稍后重试');
};

const getTDollReply = async (ctx: any, query: string, query2?: string) => {
const tdollData = await TDollSvc.getData();
if (query2) {
return getTDollDataWithCategoryRes(tdollData, query, query2);
}
return getTDollDataRes(tdollData, query);
};

const getTDoll2Reply = async (ctx: any, query: string, query2?: string) => {
const tdollData = await TDollSvc.getData();
const matchedRes = query2
? getMatchedTDollDataWithCategory(tdollData, query, query2)
: getMatchedTDollData(tdollData, query);

if (matchedRes.length === 0) {
await ctx.reply('未找到指定枪名,请检查输入是否有误!');
return null;
}

await printTDoll2Png(query, matchedRes, TDOLL2_OUTPUT_FILE);
return `[CQ:image,file=${getStaticHttpPath(
ctx.env,
TDOLL2_OUTPUT_FILE
)},cache=0,c=8]`;
};

const createTDollCommand = (name: string, alias: string): IRegister => ({
name,
alias,
description: '根据枪名查询数据, 支持模糊匹配, 忽略大小写及符号.[10s CD]',
hint: [
import { CommandHelper } from './utils/commandHelper';

// 工具函数移动到 commandHelper.ts

/**
* 创建TDoll命令
* @param name 命令名称
* @param alias 命令别名
* @param isV2 是否使用V2版本
* @returns IRegister
*/
const createTDollCommand = (name: string, alias: string, isV2 = false): IRegister => {
const description = '根据枪名查询数据, 支持模糊匹配, 忽略大小写及符号.[10s CD]';
const hints = [
`按名称查询指定武器数据: #${alias} M4A1`,
`按名称模糊查询武器数据: #${alias} m4`,
`随机武器: #${alias} random`,
`随机 AR 武器: #${alias} random ar`,
],
timesInterval: 10,
isAdmin: false,
exec: async (ctx) => {
try {
if (ctx.params.size < 1 || ctx.params.size > 2) {
await ctx.reply(
'参数不正确, 示例: #tdoll M4A1, #tdoll random(random 为随机返回), #tdoll m4 ar(查询突击步枪), #tdoll random ar(随机突击步枪)'
);
return;
}

const [query, query2] = getQueryParams(ctx.params);
const replyText =
(await getTDollReply(ctx, query, query2)) +
'\n 注意: #tdoll2 命令即将在 2 个功能版本后替换 #tdoll 命令, 可尝鲜使用 #tdoll2 命令查询数据, 后续 #tdoll 命令输出调整为 #tdoll2 命令输出格式.';
if (replyText) {
await ctx.reply(replyText);
];

return {
name,
alias,
description,
hint: hints,
timesInterval: 10,
isAdmin: false,
exec: async (ctx) => {
try {
// 参数校验
if (!(await CommandHelper.validateParams(ctx, 1, 2))) {
await ctx.reply(
'参数不正确, 示例: #tdoll M4A1, #tdoll random(random 为随机返回), #tdoll m4 ar(查询突击步枪), #tdoll random ar(随机突击步枪)'
);
return;
}

const [query, query2] = CommandHelper.getQueryParams(ctx.params);
let replyText: string | null = null;

if (isV2) {
replyText = await CommandHelper.getTDoll2Reply(ctx, query, query2);
} else {
replyText = await CommandHelper.getTDollReply(ctx, query, query2);
if (replyText) {
replyText += '\n 注意: #tdoll2 命令即将在 1 个功能版本后替换 #tdoll 命令, 可尝鲜使用 #tdoll2 命令查询数据, 后续 #tdoll 命令输出调整为 #tdoll2 命令输出格式.';
}
}

if (replyText) {
await ctx.reply(replyText);
}
} catch (error) {
await CommandHelper.handleError(ctx, error, name);
logger.error(`Command ${name} execution failed`, { error, ctx });
}
} catch (error) {
await handleError(ctx, error, name);
}
},
});
},
};
};

export const TDollCommandRegister = createTDollCommand('tdoll', 'td');
export const TDoll2CommandRegister: IRegister = {
name: 'tdoll2',
alias: 'td2',
description: '根据枪名查询数据, 支持模糊匹配, 忽略大小写及符号.[10s CD]',
hint: [
'按名称查询指定武器数据: #tdoll2 M4A1',
'按名称模糊查询武器数据: #tdoll2 m4',
'随机武器: #tdoll2 random',
'随机 AR 武器: #tdoll2 random ar',
],
timesInterval: 10,
isAdmin: false,
exec: async (ctx) => {
try {
if (ctx.params.size < 1 || ctx.params.size > 2) {
await ctx.reply(
'参数不正确, 示例: #tdoll M4A1, #tdoll random(random 为随机返回), #tdoll m4 ar(查询突击步枪), #tdoll random ar(随机突击步枪)'
);
return;
}

const [query, query2] = getQueryParams(ctx.params);
const cqOutput = await getTDoll2Reply(ctx, query, query2);
if (cqOutput) {
await ctx.reply(cqOutput);
}
} catch (error) {
await handleError(ctx, error, 'tdoll2');
}
},
};
export const TDoll2CommandRegister = createTDollCommand('tdoll2', 'td2', true);

export const TDollSkinCommandRegister: IRegister = {
name: 'tdollskin',
Expand All @@ -142,13 +81,8 @@ export const TDollSkinCommandRegister: IRegister = {
isAdmin: false,
exec: async (ctx) => {
try {
if (
!(await validateParams(
ctx,
1,
'需要1个参数, 示例: #tdollskin 2'
))
) {
if (!(await CommandHelper.validateParams(ctx, 1))) {
await ctx.reply('需要1个参数, 示例: #tdollskin 2');
return;
}

Expand All @@ -157,18 +91,20 @@ export const TDollSkinCommandRegister: IRegister = {
TDollSvc.getData(),
TDollSkinSvc.getData(),
]);
logger.info(
`fetch tdoll & tdollSkinData time: ${Date.now() - start}ms`
);
logger.info('Fetched tdoll & tdollSkinData', {
duration: Date.now() - start,
tdollCount: tdollData.length,
skinCount: Object.keys(tdollSkinData).length
});

const [query] = getQueryParams(ctx.params);
const replyText =
getTDollSkinReplyText(query, tdollData, tdollSkinData) +
`\n${TDOLL_SKIN_END_TEXT}`;
const [query] = CommandHelper.getQueryParams(ctx.params);
let replyText = CommandHelper.getTDollSkinReplyText(query, tdollData, tdollSkinData);
replyText += `\n${TDOLL_SKIN_END_TEXT}\n 注意: #tdollskin2 命令即将在 1 个功能版本后替换 #tdollskin 命令, 可尝鲜使用 #tdollskin2 命令查询数据, 后续 #tdollskin 命令输出调整为 #tdollskin2 命令输出格式.`;

await ctx.reply(replyText);
await ctx.reply(`${replyText}\n${TDOLL_SKIN_END_TEXT}`);
} catch (error) {
await handleError(ctx, error, 'tdollskin');
await CommandHelper.handleError(ctx, error, 'tdollskin');
logger.error('TDollSkin command failed', { error, ctx });
}
},
};
Expand All @@ -182,13 +118,8 @@ export const TDollSkin2CommandRegister: IRegister = {
isAdmin: false,
exec: async (ctx) => {
try {
if (
!(await validateParams(
ctx,
1,
'需要1个参数, 示例: #tdollskin2 2'
))
) {
if (!(await CommandHelper.validateParams(ctx, 1))) {
await ctx.reply('需要1个参数, 示例: #tdollskin2 2');
return;
}

Expand All @@ -197,33 +128,32 @@ export const TDollSkin2CommandRegister: IRegister = {
TDollSvc.getData(),
TDollSkinSvc.getData(),
]);
logger.info(
`fetch tdoll & tdollSkinData time: ${Date.now() - start}ms`
);

const [query] = getQueryParams(ctx.params);

let replyText = '';
if (!(query in tdollSkinData)) {
replyText = TDOLL_SKIN_NOT_FOUND_MSG;
} else {

const outputPath = await printTDollSkin2Png(
logger.info('Fetched tdoll & tdollSkinData for V2', {
duration: Date.now() - start,
tdollCount: tdollData.length,
skinCount: Object.keys(tdollSkinData).length
});

const [query] = CommandHelper.getQueryParams(ctx.params);
let replyText = TDOLL_SKIN_NOT_FOUND_MSG;

if (query in tdollSkinData) {
await CommandHelper.printTDollSkin2Png(
query,
tdollData,
tdollSkinData,
TDOLL2_SKIN_OUTPUT_FILE
);

replyText = `[CQ:image,file=${getStaticHttpPath(
ctx.env,
outputPath
TDOLL2_SKIN_OUTPUT_FILE
)},cache=0,c=8]`;
}

await ctx.reply(replyText);
} catch (error) {
await handleError(ctx, error, 'tdollskin');
await CommandHelper.handleError(ctx, error, 'tdollskin2');
logger.error('TDollSkin2 command failed', { error, ctx });
}
},
};
Loading
Loading