feat: 유저가 한말 DB에 저장

This commit is contained in:
Siwoo Jeon 2023-01-24 16:43:52 +09:00
parent f7b666c5b3
commit 889e4b7081
Signed by: migan
GPG key ID: C4151385FFD2082A
7 changed files with 31 additions and 23 deletions

View file

@ -3,7 +3,9 @@
"version": "1.0.0", "version": "1.0.0",
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"type": "module",
"dependencies": { "dependencies": {
"array-shuffle": "^3.0.0",
"discord.js": "^14.7.1", "discord.js": "^14.7.1",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"sqlite3": "^5.1.4" "sqlite3": "^5.1.4"
@ -18,7 +20,7 @@
}, },
"scripts": { "scripts": {
"build": "tsup", "build": "tsup",
"dev": "ts-node src", "dev": "ts-node-esm src",
"start": "node dist" "start": "node dist"
}, },
"prettier": "@migan/prettier-config" "prettier": "@migan/prettier-config"

View file

@ -1,19 +1,7 @@
import sqlite3 from 'sqlite3' import sqlite3 from 'sqlite3'
import { ResponseData } from './types' import { ResponseData } from './types.js'
import type { Client, Message } from 'discord.js' import type { Client, Message } from 'discord.js'
import arrayShuffle from 'array-shuffle'
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
@ -40,6 +28,7 @@ export default class ChatBot {
public train(client: Client): ChatBot { public train(client: Client): ChatBot {
client.on('messageCreate', msg => { client.on('messageCreate', msg => {
if (msg.author.bot) return
if (msg.author.id === '1026185545837191238') { if (msg.author.id === '1026185545837191238') {
this.db.run( this.db.run(
`INSERT INTO statement(text) VALUES('${msg.content}');`, `INSERT INTO statement(text) VALUES('${msg.content}');`,
@ -48,9 +37,17 @@ export default class ChatBot {
this.getResponse(msg) this.getResponse(msg)
} }
) )
} else {
if (!msg.content.startsWith('머핀아 ')) return
const sql = `INSERT INTO statement(text) VALUES('${msg.content.replace(
'머핀아 ',
''
)}')`
this.db.run(sql, err => {
if (err) throw err
})
} }
}) })
return this return this
} }
} }

View file

@ -1,10 +1,13 @@
import { ActivityType, Client, GatewayIntentBits } from 'discord.js' import { ActivityType, Client, GatewayIntentBits } from 'discord.js'
import ChatBot from './ChatBot' import ChatBot from './ChatBot.js'
import { join } from 'node:path' import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import 'dotenv/config' import 'dotenv/config'
export default class MuffinAI extends Client { export default class MuffinAI extends Client {
private chatBot = new ChatBot(join(__dirname, '..', 'db', 'db.sqlite3')) private chatBot = new ChatBot(
join(dirname(fileURLToPath(import.meta.url)), '..', 'db', 'db.sqlite3')
)
public constructor() { public constructor() {
super({ super({
intents: [ intents: [

View file

@ -1 +1 @@
import('./Client').then(a => new a.default().login()) import('./Client.js').then(a => new a.default().login())

View file

@ -26,10 +26,10 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */ /* Modules */
"module": "commonjs", "module": "Node16",
/* Specify what module code is generated. */ /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */ // "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ "moduleResolution": "Node16", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */

View file

@ -2,7 +2,8 @@ import { defineConfig } from 'tsup'
export default defineConfig({ export default defineConfig({
clean: true, clean: true,
format: ['cjs'], format: ['esm'],
entry: ['src/index.ts'], entry: ['src/index.ts'],
minify: true, minify: true,
splitting: true,
}) })

View file

@ -278,6 +278,11 @@ arg@^4.1.0:
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089" resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA== integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
array-shuffle@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/array-shuffle/-/array-shuffle-3.0.0.tgz#9a927cc2ca4c8ef4d71cddef7c9ee7b3b733d54b"
integrity sha512-rogEGxHOQPhslOhpg12LJkB+bbAl484/s2AJq0BxtzQDQfKl76fS2u9zWgg3p3b9ENcuvE7K8A7l5ddiPjCRnw==
array-union@^2.1.0: array-union@^2.1.0:
version "2.1.0" version "2.1.0"
resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d" resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d"