feat: Edit database code
This commit is contained in:
parent
b2742af957
commit
ba2fd54e6d
7 changed files with 41 additions and 53 deletions
|
@ -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": {
|
||||||
|
|
|
@ -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 modules(): Collection<string, Command> {
|
||||||
get dokdo() {
|
return this.#modules
|
||||||
return new Dokdo(this, {
|
}
|
||||||
|
public dokdo: Dokdo = new Dokdo(this, {
|
||||||
aliases: ['dokdo', 'dok'],
|
aliases: ['dokdo', 'dok'],
|
||||||
owners: [config.bot.owner_ID],
|
owners: [config.bot.owner_ID],
|
||||||
noPerm,
|
noPerm,
|
||||||
prefix,
|
prefix,
|
||||||
})
|
})
|
||||||
}
|
public prefix = prefix
|
||||||
get modules(): Collection<string, Command> {
|
|
||||||
return this.#modules
|
|
||||||
}
|
|
||||||
#modules: Collection<string, Command> = new Collection()
|
|
||||||
public constructor() {
|
public constructor() {
|
||||||
super({
|
super({
|
||||||
intents: [
|
intents: [
|
||||||
|
@ -75,7 +73,6 @@ 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())
|
||||||
|
|
||||||
|
@ -90,7 +87,6 @@ export default class MuffinBot extends Client {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
})
|
})
|
||||||
return super.login(config.bot.token)
|
return super.login(config.bot.token)
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,15 +20,15 @@ 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] &&
|
||||||
|
args.startsWith(learnData[0].command)
|
||||||
|
) {
|
||||||
return `${learnData[0].result}\n\`${
|
return `${learnData[0].result}\n\`${
|
||||||
(await msg.client.users.fetch(learnData[0].user_id)).username
|
(await msg.client.users.fetch(learnData[0].user_id)).username
|
||||||
}님이 알려주셨어요.\``
|
}님이 알려주셨어요.\``
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let response: string
|
let response: string
|
||||||
if ((msg.channel as TextChannel).nsfw) {
|
if ((msg.channel as TextChannel).nsfw) {
|
||||||
|
@ -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,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -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[]>(
|
||||||
|
|
|
@ -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],
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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],
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue