From 889e4b70811a07b98a4b7e3a32cfd9372d137236 Mon Sep 17 00:00:00 2001 From: Migan178 Date: Tue, 24 Jan 2023 16:43:52 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9C=A0=EC=A0=80=EA=B0=80=20=ED=95=9C?= =?UTF-8?q?=EB=A7=90=20DB=EC=97=90=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +++- src/ChatBot.ts | 27 ++++++++++++--------------- src/Client.ts | 9 ++++++--- src/index.ts | 2 +- tsconfig.json | 4 ++-- tsup.config.ts | 3 ++- yarn.lock | 5 +++++ 7 files changed, 31 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index f7dd589..bf20829 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,9 @@ "version": "1.0.0", "main": "index.js", "license": "MIT", + "type": "module", "dependencies": { + "array-shuffle": "^3.0.0", "discord.js": "^14.7.1", "dotenv": "^16.0.3", "sqlite3": "^5.1.4" @@ -18,7 +20,7 @@ }, "scripts": { "build": "tsup", - "dev": "ts-node src", + "dev": "ts-node-esm src", "start": "node dist" }, "prettier": "@migan/prettier-config" diff --git a/src/ChatBot.ts b/src/ChatBot.ts index 3ca007d..f23a6a0 100644 --- a/src/ChatBot.ts +++ b/src/ChatBot.ts @@ -1,19 +1,7 @@ import sqlite3 from 'sqlite3' -import { ResponseData } from './types' +import { ResponseData } from './types.js' import type { Client, 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 -} +import arrayShuffle from 'array-shuffle' export default class ChatBot { private db: sqlite3.Database @@ -40,6 +28,7 @@ export default class ChatBot { public train(client: Client): ChatBot { client.on('messageCreate', msg => { + if (msg.author.bot) return if (msg.author.id === '1026185545837191238') { this.db.run( `INSERT INTO statement(text) VALUES('${msg.content}');`, @@ -48,9 +37,17 @@ export default class ChatBot { 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 } } diff --git a/src/Client.ts b/src/Client.ts index 8bacc98..8e0d8de 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -1,10 +1,13 @@ import { ActivityType, Client, GatewayIntentBits } from 'discord.js' -import ChatBot from './ChatBot' -import { join } from 'node:path' +import ChatBot from './ChatBot.js' +import { join, dirname } from 'node:path' +import { fileURLToPath } from 'node:url' import 'dotenv/config' 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() { super({ intents: [ diff --git a/src/index.ts b/src/index.ts index 952c418..a42a574 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1 @@ -import('./Client').then(a => new a.default().login()) +import('./Client.js').then(a => new a.default().login()) diff --git a/tsconfig.json b/tsconfig.json index 88431dc..cbeb134 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -26,10 +26,10 @@ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "commonjs", + "module": "Node16", /* Specify what module code is generated. */ // "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. */ // "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. */ diff --git a/tsup.config.ts b/tsup.config.ts index 7905912..7304091 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -2,7 +2,8 @@ import { defineConfig } from 'tsup' export default defineConfig({ clean: true, - format: ['cjs'], + format: ['esm'], entry: ['src/index.ts'], minify: true, + splitting: true, }) diff --git a/yarn.lock b/yarn.lock index 7f50502..59474d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -278,6 +278,11 @@ arg@^4.1.0: resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089" 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: version "2.1.0" resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d"