This commit is contained in:
Siwoo Jeon 2023-06-05 14:06:03 +09:00
parent 99d2bc2d80
commit 6a8c329ddc
Signed by: migan
GPG key ID: C4151385FFD2082A
9 changed files with 38 additions and 47 deletions

3
.gitignore vendored
View file

@ -129,6 +129,9 @@ dist
.yarn/install-state.gz
.pnp.*
# config file
config.json
# database
db/

16
config.example.json Normal file
View file

@ -0,0 +1,16 @@
{
"bot": {
"owner_ID": "",
"token": ""
},
"train": {
"user_ID": ""
},
"mysql": {
"user": "",
"host": "",
"password": "",
"database": "",
"port": 3306
}
}

View file

@ -1,8 +0,0 @@
DISCORD_TOKEN=''
MYSQL_USER=''
MYSQL_HOST=''
MYSQL_PASSWORD=''
MYSQL_DATABASE=''
MYSQL_PORT=''
OWNER_ID=''
TRAIN_USER_ID=''

View file

@ -8,8 +8,8 @@ import {
import { Command, noPerm, ChatBot, NODE_ENV } from './modules'
import { readdirSync } from 'node:fs'
import { join } from 'node:path'
import 'dotenv/config'
import { execSync } from 'node:child_process'
import config from '../config.json'
const prefix = '멒힌아 '
@ -70,13 +70,13 @@ export default class MuffinAI extends Client {
if (NODE_ENV === 'development') console.log(args)
const command = this.#modules.get(args.shift()!.toLowerCase())
if (!command) return
if (command.noPerm && msg.author.id !== process.env.OWNER_ID)
if (command.noPerm && msg.author.id !== config.bot.owner_ID)
return await noPerm(msg)
command.execute(msg, args)
}
})
return super.login()
return super.login(config.bot.token)
}
public override destroy() {

View file

@ -6,7 +6,7 @@ export default class extends Command {
super('학습데이터량')
}
public async execute(msg: Message, args: string[]) {
const db = await msg.client.chatBot.db
const db = await msg.client.chatBot.db.getConnection()
const [rows] = await db.execute<ResponseData[]>('SELECT * FROM statement;')
const [nsfw] = await db.execute<NSFWData[]>('SELECT * FROM nsfw_content;')
const muffin: ResponseData[] = []
@ -17,5 +17,6 @@ export default class extends Command {
msg.channel.send(
`머핀 데이터: ${muffin.length}\nnsfw 데이터: ${nsfw.length}`
)
db.release()
}
}

View file

@ -1,6 +1,7 @@
import type { Client, Message } from 'discord.js'
import database, { ResponseData } from './database'
import { TextChannel } from 'discord.js'
import config from '../../config.json'
export default class ChatBot {
get db() {
@ -8,9 +9,7 @@ export default class ChatBot {
}
#db = database
public async getResponse(msg: Message): Promise<string> {
const db = await this.db
const request = msg.content.replace('머핀아 ', '')
console.log(`req: ${request}`)
const db = await this.db.getConnection()
const [rows] = await db.execute<ResponseData[]>('SELECT * FROM statement;')
let response: string
if ((msg.channel as TextChannel).nsfw) {
@ -23,15 +22,15 @@ export default class ChatBot {
response = rows[Math.floor(Math.random() * rows.length)].text
}
if (!response) response = '살ㄹ려주세요'
console.log(`res: ${response}`)
db.release()
return response
}
public train(client: Client): ChatBot {
public async train(client: Client): Promise<ChatBot> {
const db = await this.db.getConnection()
client.on('messageCreate', async msg => {
if (msg.author.bot) return
const db = await this.db
if (msg.author.id === process.env.TRAIN_USER_ID) {
if (msg.author.id === config.train.user_ID) {
const response = await this.getResponse(msg)
const [rows] = await db.execute<ResponseData[]>(
'SELECT * FROM statement;'
@ -68,10 +67,11 @@ export default class ChatBot {
}
}
})
db.release()
return this
}
public async destroy() {
this.db.then(db => db.destroy())
this.db.end()
}
}

View file

@ -1,9 +1,5 @@
import {
createConnection,
RowDataPacket,
ConnectionOptions,
} from 'mysql2/promise'
import 'dotenv/config'
import { RowDataPacket, createPool } from 'mysql2/promise'
import config from '../../config.json'
export interface BaseData extends RowDataPacket {
id: number
@ -19,14 +15,6 @@ export interface ResponseData extends BaseData {
search_in_response_to: string
}
export const config: ConnectionOptions = {
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
port: (process.env.MYSQL_PORT as unknown as number) || 3306,
}
export { BaseData as NSFWData }
export default createConnection(config)
export default createPool(config.mysql)

View file

@ -1,15 +1,6 @@
import ChatBot from './ChatBot'
import Command from './Command'
import database, { ResponseData, config, NSFWData } from './database'
import database, { ResponseData, NSFWData } from './database'
import noPerm from './noPerm'
import { NODE_ENV } from './env'
export {
ChatBot,
Command,
database,
noPerm,
ResponseData,
config,
NODE_ENV,
NSFWData,
}
export { ChatBot, Command, database, noPerm, ResponseData, NODE_ENV, NSFWData }

View file

@ -37,7 +37,7 @@
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "resolveJsonModule": true, /* Enable importing .json files. */
"resolveJsonModule": true, /* Enable importing .json files. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */