This commit is contained in:
@ -1,18 +1,23 @@
|
||||
import getDb from '../db/index.js';
|
||||
|
||||
export async function getRandomRecipe(limit = 5) {
|
||||
logger(`随机${limit}个食谱`)
|
||||
const data = await getDb();
|
||||
const len = data.length;
|
||||
logger(len)
|
||||
const list = [];
|
||||
const indexList = new Set();
|
||||
|
||||
while (indexList.size === limit) {
|
||||
const index = Math.floor(Math.random() * len);
|
||||
logger(`index: ${index}`)
|
||||
if (!indexList.has(index)) {
|
||||
indexList.set(index);
|
||||
list.push(data[index]);
|
||||
}
|
||||
}
|
||||
|
||||
logger(list.length)
|
||||
|
||||
return list;
|
||||
}
|
||||
|
@ -13,11 +13,12 @@ const commandHandle = {
|
||||
};
|
||||
|
||||
async function randomCook(text, data) {
|
||||
logger(text, data.type, data.sender.id, data.sender.group.id);
|
||||
const limit = Number.parseInt(text) > 0 ? Number.parseInt(text) : 5;
|
||||
const recipeList = await getRandomRecipe(limit);
|
||||
logger(JSON.stringify(recipeList))
|
||||
logger(JSON.stringify(recipeList));
|
||||
const message = genRecipeMessage(recipeList, '给你推荐');
|
||||
logger(message)
|
||||
logger(message);
|
||||
switch (data.type) {
|
||||
case 'GroupMessage':
|
||||
Bot.sendMessageToGroup(data.sender.group.id, message);
|
||||
|
@ -8,7 +8,7 @@ export function genRecipeMessage(recipeList, header, footer) {
|
||||
}
|
||||
|
||||
if (header) {
|
||||
textList.shift(header);
|
||||
textList.unshift(header);
|
||||
}
|
||||
|
||||
if (footer) {
|
||||
|
Reference in New Issue
Block a user