From 7280ac7f3571f30df3f9237b0a00378901798eea Mon Sep 17 00:00:00 2001 From: Migan178 Date: Tue, 24 Jan 2023 14:24:52 +0900 Subject: [PATCH] fix: array shuffle system --- src/ChatBot.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ChatBot.ts b/src/ChatBot.ts index b211d26..a21d6d2 100644 --- a/src/ChatBot.ts +++ b/src/ChatBot.ts @@ -2,6 +2,19 @@ import sqlite3 from 'sqlite3' import { ResponseData } from './types' import { Message } from 'discord.js' +function arrayShuffle(array: T[]): T[] { + array = [...array] + + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)) + const temp = array[i] + array[i] = array[j] + array[j] = temp + } + + return array +} + export default class ChatBot { private db: sqlite3.Database public constructor(dbPath: string) { @@ -12,8 +25,8 @@ export default class ChatBot { this.db.all('select * from statement', [], (err, rows: ResponseData[]) => { if (err) throw err const a = msg.content.replace('머핀아', '') - const data: ResponseData[] = [...rows] - data.sort(() => Math.random() - 0.5) + const data = arrayShuffle([...rows]) + console.log(data) let r = data[0].text if (!r) r = '살ㄹ려주세요' console.log(`⌨️ㅣ${a}`)