fix: array shuffle system
This commit is contained in:
parent
6ff9e2bef0
commit
7280ac7f35
1 changed files with 15 additions and 2 deletions
|
@ -2,6 +2,19 @@ import sqlite3 from 'sqlite3'
|
||||||
import { ResponseData } from './types'
|
import { ResponseData } from './types'
|
||||||
import { Message } from 'discord.js'
|
import { Message } from 'discord.js'
|
||||||
|
|
||||||
|
function arrayShuffle<T>(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 {
|
export default class ChatBot {
|
||||||
private db: sqlite3.Database
|
private db: sqlite3.Database
|
||||||
public constructor(dbPath: string) {
|
public constructor(dbPath: string) {
|
||||||
|
@ -12,8 +25,8 @@ export default class ChatBot {
|
||||||
this.db.all('select * from statement', [], (err, rows: ResponseData[]) => {
|
this.db.all('select * from statement', [], (err, rows: ResponseData[]) => {
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
const a = msg.content.replace('머핀아', '')
|
const a = msg.content.replace('머핀아', '')
|
||||||
const data: ResponseData[] = [...rows]
|
const data = arrayShuffle([...rows])
|
||||||
data.sort(() => Math.random() - 0.5)
|
console.log(data)
|
||||||
let r = data[0].text
|
let r = data[0].text
|
||||||
if (!r) r = '살ㄹ려주세요'
|
if (!r) r = '살ㄹ려주세요'
|
||||||
console.log(`⌨️ㅣ${a}`)
|
console.log(`⌨️ㅣ${a}`)
|
||||||
|
|
Loading…
Reference in a new issue