fix
This commit is contained in:
parent
99d2bc2d80
commit
6a8c329ddc
9 changed files with 38 additions and 47 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -129,6 +129,9 @@ dist
|
||||||
.yarn/install-state.gz
|
.yarn/install-state.gz
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
|
# config file
|
||||||
|
config.json
|
||||||
|
|
||||||
# database
|
# database
|
||||||
db/
|
db/
|
||||||
|
|
||||||
|
|
16
config.example.json
Normal file
16
config.example.json
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"bot": {
|
||||||
|
"owner_ID": "",
|
||||||
|
"token": ""
|
||||||
|
},
|
||||||
|
"train": {
|
||||||
|
"user_ID": ""
|
||||||
|
},
|
||||||
|
"mysql": {
|
||||||
|
"user": "",
|
||||||
|
"host": "",
|
||||||
|
"password": "",
|
||||||
|
"database": "",
|
||||||
|
"port": 3306
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +0,0 @@
|
||||||
DISCORD_TOKEN=''
|
|
||||||
MYSQL_USER=''
|
|
||||||
MYSQL_HOST=''
|
|
||||||
MYSQL_PASSWORD=''
|
|
||||||
MYSQL_DATABASE=''
|
|
||||||
MYSQL_PORT=''
|
|
||||||
OWNER_ID=''
|
|
||||||
TRAIN_USER_ID=''
|
|
|
@ -8,8 +8,8 @@ import {
|
||||||
import { Command, noPerm, ChatBot, NODE_ENV } from './modules'
|
import { Command, noPerm, ChatBot, NODE_ENV } from './modules'
|
||||||
import { readdirSync } from 'node:fs'
|
import { readdirSync } from 'node:fs'
|
||||||
import { join } from 'node:path'
|
import { join } from 'node:path'
|
||||||
import 'dotenv/config'
|
|
||||||
import { execSync } from 'node:child_process'
|
import { execSync } from 'node:child_process'
|
||||||
|
import config from '../config.json'
|
||||||
|
|
||||||
const prefix = '멒힌아 '
|
const prefix = '멒힌아 '
|
||||||
|
|
||||||
|
@ -70,13 +70,13 @@ export default class MuffinAI extends Client {
|
||||||
if (NODE_ENV === 'development') console.log(args)
|
if (NODE_ENV === 'development') console.log(args)
|
||||||
const command = this.#modules.get(args.shift()!.toLowerCase())
|
const command = this.#modules.get(args.shift()!.toLowerCase())
|
||||||
if (!command) return
|
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)
|
return await noPerm(msg)
|
||||||
|
|
||||||
command.execute(msg, args)
|
command.execute(msg, args)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return super.login()
|
return super.login(config.bot.token)
|
||||||
}
|
}
|
||||||
|
|
||||||
public override destroy() {
|
public override destroy() {
|
||||||
|
|
|
@ -6,7 +6,7 @@ export default class extends Command {
|
||||||
super('학습데이터량')
|
super('학습데이터량')
|
||||||
}
|
}
|
||||||
public async execute(msg: Message, args: string[]) {
|
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 [rows] = await db.execute<ResponseData[]>('SELECT * FROM statement;')
|
||||||
const [nsfw] = await db.execute<NSFWData[]>('SELECT * FROM nsfw_content;')
|
const [nsfw] = await db.execute<NSFWData[]>('SELECT * FROM nsfw_content;')
|
||||||
const muffin: ResponseData[] = []
|
const muffin: ResponseData[] = []
|
||||||
|
@ -17,5 +17,6 @@ export default class extends Command {
|
||||||
msg.channel.send(
|
msg.channel.send(
|
||||||
`머핀 데이터: ${muffin.length}개\nnsfw 데이터: ${nsfw.length}`
|
`머핀 데이터: ${muffin.length}개\nnsfw 데이터: ${nsfw.length}`
|
||||||
)
|
)
|
||||||
|
db.release()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import type { Client, Message } from 'discord.js'
|
import type { Client, Message } from 'discord.js'
|
||||||
import database, { ResponseData } from './database'
|
import database, { ResponseData } from './database'
|
||||||
import { TextChannel } from 'discord.js'
|
import { TextChannel } from 'discord.js'
|
||||||
|
import config from '../../config.json'
|
||||||
|
|
||||||
export default class ChatBot {
|
export default class ChatBot {
|
||||||
get db() {
|
get db() {
|
||||||
|
@ -8,9 +9,7 @@ export default class ChatBot {
|
||||||
}
|
}
|
||||||
#db = database
|
#db = database
|
||||||
public async getResponse(msg: Message): Promise<string> {
|
public async getResponse(msg: Message): Promise<string> {
|
||||||
const db = await this.db
|
const db = await this.db.getConnection()
|
||||||
const request = msg.content.replace('머핀아 ', '')
|
|
||||||
console.log(`req: ${request}`)
|
|
||||||
const [rows] = await db.execute<ResponseData[]>('SELECT * FROM statement;')
|
const [rows] = await db.execute<ResponseData[]>('SELECT * FROM statement;')
|
||||||
let response: string
|
let response: string
|
||||||
if ((msg.channel as TextChannel).nsfw) {
|
if ((msg.channel as TextChannel).nsfw) {
|
||||||
|
@ -23,15 +22,15 @@ export default class ChatBot {
|
||||||
response = rows[Math.floor(Math.random() * rows.length)].text
|
response = rows[Math.floor(Math.random() * rows.length)].text
|
||||||
}
|
}
|
||||||
if (!response) response = '살ㄹ려주세요'
|
if (!response) response = '살ㄹ려주세요'
|
||||||
console.log(`res: ${response}`)
|
db.release()
|
||||||
return response
|
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 => {
|
client.on('messageCreate', async msg => {
|
||||||
if (msg.author.bot) return
|
if (msg.author.bot) return
|
||||||
const db = await this.db
|
if (msg.author.id === config.train.user_ID) {
|
||||||
if (msg.author.id === process.env.TRAIN_USER_ID) {
|
|
||||||
const response = await this.getResponse(msg)
|
const response = await this.getResponse(msg)
|
||||||
const [rows] = await db.execute<ResponseData[]>(
|
const [rows] = await db.execute<ResponseData[]>(
|
||||||
'SELECT * FROM statement;'
|
'SELECT * FROM statement;'
|
||||||
|
@ -68,10 +67,11 @@ export default class ChatBot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
db.release()
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public async destroy() {
|
public async destroy() {
|
||||||
this.db.then(db => db.destroy())
|
this.db.end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
import {
|
import { RowDataPacket, createPool } from 'mysql2/promise'
|
||||||
createConnection,
|
import config from '../../config.json'
|
||||||
RowDataPacket,
|
|
||||||
ConnectionOptions,
|
|
||||||
} from 'mysql2/promise'
|
|
||||||
import 'dotenv/config'
|
|
||||||
|
|
||||||
export interface BaseData extends RowDataPacket {
|
export interface BaseData extends RowDataPacket {
|
||||||
id: number
|
id: number
|
||||||
|
@ -19,14 +15,6 @@ export interface ResponseData extends BaseData {
|
||||||
search_in_response_to: string
|
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 { BaseData as NSFWData }
|
||||||
|
|
||||||
export default createConnection(config)
|
export default createPool(config.mysql)
|
||||||
|
|
|
@ -1,15 +1,6 @@
|
||||||
import ChatBot from './ChatBot'
|
import ChatBot from './ChatBot'
|
||||||
import Command from './Command'
|
import Command from './Command'
|
||||||
import database, { ResponseData, config, NSFWData } from './database'
|
import database, { ResponseData, NSFWData } from './database'
|
||||||
import noPerm from './noPerm'
|
import noPerm from './noPerm'
|
||||||
import { NODE_ENV } from './env'
|
import { NODE_ENV } from './env'
|
||||||
export {
|
export { ChatBot, Command, database, noPerm, ResponseData, NODE_ENV, NSFWData }
|
||||||
ChatBot,
|
|
||||||
Command,
|
|
||||||
database,
|
|
||||||
noPerm,
|
|
||||||
ResponseData,
|
|
||||||
config,
|
|
||||||
NODE_ENV,
|
|
||||||
NSFWData,
|
|
||||||
}
|
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
||||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||||
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
// "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. */
|
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
||||||
|
|
||||||
/* JavaScript Support */
|
/* JavaScript Support */
|
||||||
|
|
Loading…
Reference in a new issue