feat: Edit database code

This commit is contained in:
Siwoo Jeon 2024-06-09 21:17:20 +09:00
parent b2742af957
commit ba2fd54e6d
Signed by: migan
GPG key ID: C4151385FFD2082A
7 changed files with 41 additions and 53 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "muffinbot", "name": "muffinbot",
"version": "2.0.0-oreo.p240608a", "version": "2.1.0-oreo.d240609a",
"main": "dist/index.js", "main": "dist/index.js",
"private": true, "private": true,
"dependencies": { "dependencies": {

View file

@ -14,22 +14,20 @@ import Dokdo from 'dokdo'
const prefix = '머핀아 ' const prefix = '머핀아 '
export default class MuffinBot extends Client { export default class MuffinBot extends Client {
#modules: Collection<string, Command> = new Collection()
get chatBot() { get chatBot() {
return new ChatBot() return new ChatBot()
} }
get dokdo() {
return new Dokdo(this, {
aliases: ['dokdo', 'dok'],
owners: [config.bot.owner_ID],
noPerm,
prefix,
})
}
get modules(): Collection<string, Command> { get modules(): Collection<string, Command> {
return this.#modules return this.#modules
} }
#modules: Collection<string, Command> = new Collection() public dokdo: Dokdo = new Dokdo(this, {
aliases: ['dokdo', 'dok'],
owners: [config.bot.owner_ID],
noPerm,
prefix,
})
public prefix = prefix
public constructor() { public constructor() {
super({ super({
intents: [ intents: [
@ -75,19 +73,17 @@ export default class MuffinBot extends Client {
if (args[0].startsWith('dokdo') || args[0].startsWith('dok')) { if (args[0].startsWith('dokdo') || args[0].startsWith('dok')) {
await this.dokdo.run(msg) await this.dokdo.run(msg)
} else { } else {
if (msg.channel instanceof TextChannel) { await msg.channel.sendTyping()
await msg.channel.sendTyping() const command = this.modules.get(args.shift()!.toLowerCase())
const command = this.modules.get(args.shift()!.toLowerCase())
if (command) { if (command) {
if (command.noPerm && msg.author.id !== config.bot.owner_ID) if (command.noPerm && msg.author.id !== config.bot.owner_ID)
return await noPerm(msg) return await noPerm(msg)
await command.execute(msg, args) await command.execute(msg, args)
} else { } else {
const response = await this.chatBot.getResponse(msg) const response = await this.chatBot.getResponse(msg)
await msg.reply(response) await msg.reply(response)
}
} }
} }
} }
@ -100,5 +96,7 @@ declare module 'discord.js' {
interface Client { interface Client {
get chatBot(): ChatBot get chatBot(): ChatBot
get modules(): Collection<string, Command> get modules(): Collection<string, Command>
dokdo: Dokdo
prefix: string
} }
} }

View file

@ -9,12 +9,9 @@ export default class ChatBot {
return new MaaDatabase() return new MaaDatabase()
} }
public async getResponse(msg: Message): Promise<string> { public async getResponse(msg: Message): Promise<string> {
const prefix = msg.client.prefix
const data = await this.db.statement.all() const data = await this.db.statement.all()
const args = msg.content const args = msg.content.slice(prefix.length).trim().split(/ +/g).join(' ')
.slice('머핀아 '.length)
.trim()
.split(/ +/g)
.join(' ')
const learnData = await this.db.learn.findOne(args) const learnData = await this.db.learn.findOne(args)
const randomNumber = Math.round(Math.random() * (2 - 1) + 1) const randomNumber = Math.round(Math.random() * (2 - 1) + 1)
@ -23,14 +20,14 @@ export default class ChatBot {
console.log(args) console.log(args)
} }
if (randomNumber === 1) { if (
if (learnData[0]) { randomNumber === 1 &&
if (args.startsWith(learnData[0].command)) { learnData[0] &&
return `${learnData[0].result}\n\`${ args.startsWith(learnData[0].command)
(await msg.client.users.fetch(learnData[0].user_id)).username ) {
} .\`` return `${learnData[0].result}\n\`${
} (await msg.client.users.fetch(learnData[0].user_id)).username
} } .\``
} }
let response: string let response: string
@ -46,27 +43,24 @@ export default class ChatBot {
} }
public async train(client: Client): Promise<ChatBot> { public async train(client: Client): Promise<ChatBot> {
const prefix = client.prefix
client.on('messageCreate', async msg => { client.on('messageCreate', async msg => {
if (msg.author.bot) return if (msg.author.bot) return
if (msg.author.id === config.train.user_ID) { if (msg.author.id === config.train.user_ID) {
const response = await this.getResponse(msg) const response = await this.getResponse(msg)
const data = await this.db.statement.all()
await this.db.statement.insert({ await this.db.statement.insert({
id: ++data[data.length - 1].id,
text: msg.content, text: msg.content,
persona: 'muffin', persona: 'muffin',
in_response_to: response, in_response_to: response,
}) })
} else { } else {
if (!(msg.channel as TextChannel).nsfw) return if (!(msg.channel as TextChannel).nsfw) return
if (!msg.content.startsWith('머핀아 ')) return if (!msg.content.startsWith(prefix)) return
const user = `user:${msg.author.username.slice(0, 50).toLowerCase()}` const persona = `user:${msg.author.username.slice(0, 50).toLowerCase()}`
const text = msg.content.replace('머핀아 ', '') const text = msg.content.replace('머핀아 ', '')
const data = await this.db.nsfwContent.all()
await this.db.nsfwContent.insert({ await this.db.nsfwContent.insert({
id: ++data[data.length - 1].id,
text, text,
persona: user, persona,
}) })
} }
}) })

View file

@ -60,7 +60,7 @@ export class LearnTable implements BaseTable<LearnData, string> {
} }
public async findOneAnotherKey( public async findOneAnotherKey(
key: 'command' | 'result' | 'user_id' | 'created_at', key: 'id' | 'command' | 'result' | 'user_id' | 'created_at',
data: any, data: any,
): Promise<LearnData[]> { ): Promise<LearnData[]> {
const [rows] = await this._database.execute<LearnData[]>( const [rows] = await this._database.execute<LearnData[]>(

View file

@ -21,17 +21,13 @@ export class NSFWContentTable implements BaseTable<NSFWData, number> {
return rows return rows
} }
public async insert(data: { public async insert(data: { text: string; persona: string }): Promise<void> {
id: number
text: string
persona: string
}): Promise<void> {
const db = await this._database.getConnection() const db = await this._database.getConnection()
await run(db, async () => { await run(db, async () => {
await db.execute( await db.execute(
'INSERT INTO nsfw_content (id, text, persona) VALUES (?, ?, ?);', 'INSERT INTO nsfw_content (text, persona) VALUES (?, ?);',
[data.id, data.text, data.persona], [data.text, data.persona],
) )
}) })
} }

View file

@ -22,7 +22,6 @@ export class StatementTable implements BaseTable<ResponseData, number> {
} }
public async insert(data: { public async insert(data: {
id: number
text: string text: string
persona: string persona: string
in_response_to: string in_response_to: string
@ -31,8 +30,8 @@ export class StatementTable implements BaseTable<ResponseData, number> {
await run(db, async () => { await run(db, async () => {
await db.execute( await db.execute(
'INSERT INTO statement (id, text, persona, in_response_to) VALUES (?, ?, ?, ?);', 'INSERT INTO statement (text, persona, in_response_to) VALUES (?, ?, ?);',
[data.id, data.text, data.persona, data.in_response_to], [data.text, data.persona, data.in_response_to],
) )
}) })
} }

View file

@ -16,6 +16,7 @@ export interface ResponseData extends BaseData {
} }
export interface LearnData extends RowDataPacket { export interface LearnData extends RowDataPacket {
id: number
command: string command: string
result: string result: string
user_id: Snowflake user_id: Snowflake