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",
"version": "2.0.0-oreo.p240608a",
"version": "2.1.0-oreo.d240609a",
"main": "dist/index.js",
"private": true,
"dependencies": {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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