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