3.0.0-Cake #5
8 changed files with 91 additions and 70 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "muffinbot",
|
"name": "muffinbot",
|
||||||
"version": "3.0.0-cake.d240622a",
|
"version": "3.0.0-cake.d240622b",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -25,7 +25,7 @@
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsup",
|
"build": "tsup",
|
||||||
"dev": "yarn build && cross-env NODE_ENV=development node dist",
|
"dev": "cross-env NODE_ENV=development tsup --watch --onSuccess \"node dist\"",
|
||||||
"start": "cross-env NODE_ENV=production node dist"
|
"start": "cross-env NODE_ENV=production node dist"
|
||||||
},
|
},
|
||||||
"packageManager": "yarn@4.2.2"
|
"packageManager": "yarn@4.2.2"
|
||||||
|
|
|
@ -1,25 +1,14 @@
|
||||||
import { noPerm, ChatBot, NODE_ENV, MaaDatabase } from './modules'
|
import { SapphireClient, container, LogLevel } from '@sapphire/framework'
|
||||||
import { SapphireClient, container } from '@sapphire/framework'
|
import { GatewayIntentBits, type Snowflake } from 'discord.js'
|
||||||
import { ActivityType, GatewayIntentBits, Snowflake } from 'discord.js'
|
import { ChatBot, NODE_ENV, MaaDatabase } from './modules'
|
||||||
import config from '../config.json'
|
import config from '../config.json'
|
||||||
import Dokdo from 'dokdo'
|
|
||||||
|
|
||||||
|
container.config = config
|
||||||
container.prefix = '머핀아 '
|
container.prefix = '머핀아 '
|
||||||
container.database = new MaaDatabase()
|
container.database = new MaaDatabase()
|
||||||
container.chatBot = new ChatBot(container.database)
|
container.chatBot = new ChatBot(container.database)
|
||||||
container.config = config
|
|
||||||
|
|
||||||
export default class MuffinBot extends SapphireClient {
|
export default class MuffinBot extends SapphireClient {
|
||||||
public database = container.database
|
|
||||||
public chatBot = container.chatBot
|
|
||||||
public prefix = container.prefix
|
|
||||||
public dokdo: Dokdo = new Dokdo(this, {
|
|
||||||
aliases: ['dokdo', 'dok'],
|
|
||||||
owners: [config.bot.owner_ID],
|
|
||||||
noPerm,
|
|
||||||
prefix: container.prefix,
|
|
||||||
})
|
|
||||||
|
|
||||||
public constructor() {
|
public constructor() {
|
||||||
super({
|
super({
|
||||||
intents: [
|
intents: [
|
||||||
|
@ -29,53 +18,18 @@ export default class MuffinBot extends SapphireClient {
|
||||||
],
|
],
|
||||||
loadMessageCommandListeners: true,
|
loadMessageCommandListeners: true,
|
||||||
defaultPrefix: container.prefix,
|
defaultPrefix: container.prefix,
|
||||||
|
logger: {
|
||||||
|
level: NODE_ENV === 'development' ? LogLevel.Debug : LogLevel.Info,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async login(): Promise<string> {
|
public override async login(): Promise<string> {
|
||||||
if (NODE_ENV === 'development') this.on('debug', console.info)
|
await container.chatBot.train(this)
|
||||||
await this.chatBot.train(this)
|
|
||||||
|
|
||||||
this.once('ready', client => {
|
|
||||||
function setStatus() {
|
|
||||||
client.user.setActivity({
|
|
||||||
type: ActivityType.Custom,
|
|
||||||
name: 'ㅅ살려주세요..!',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
setStatus()
|
|
||||||
setInterval(() => setStatus(), 600000)
|
|
||||||
|
|
||||||
console.log(`먹힐 준ㅂ비 완료`)
|
|
||||||
}).on('messageCreate', async msg => {
|
|
||||||
if (msg.author.bot) return
|
|
||||||
if (msg.content.startsWith(this.prefix)) {
|
|
||||||
const args = msg.content.slice(this.prefix.length).trim().split(/ +/g)
|
|
||||||
|
|
||||||
if (args[0].startsWith('dokdo') || args[0].startsWith('dok')) {
|
|
||||||
await this.dokdo.run(msg)
|
|
||||||
} else {
|
|
||||||
if (!this.stores.get('commands').get(args[0])) {
|
|
||||||
await msg.channel.sendTyping()
|
|
||||||
const response = await this.chatBot.getResponse(msg)
|
|
||||||
await msg.reply(response)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return super.login(config.bot.token)
|
return super.login(config.bot.token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
declare module 'discord.js' {
|
|
||||||
interface Client {
|
|
||||||
chatBot: ChatBot
|
|
||||||
prefix: string
|
|
||||||
dokdo: Dokdo
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
declare module '@sapphire/pieces' {
|
declare module '@sapphire/pieces' {
|
||||||
interface Container {
|
interface Container {
|
||||||
database: MaaDatabase
|
database: MaaDatabase
|
||||||
|
|
7
src/listeners/debug.ts
Normal file
7
src/listeners/debug.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { Listener } from '@sapphire/framework'
|
||||||
|
|
||||||
|
export default class extends Listener {
|
||||||
|
public async run(debug: string) {
|
||||||
|
this.container.logger.debug(debug)
|
||||||
|
}
|
||||||
|
}
|
32
src/listeners/messageCreate.ts
Normal file
32
src/listeners/messageCreate.ts
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import { Listener } from '@sapphire/framework'
|
||||||
|
import { type Message } from 'discord.js'
|
||||||
|
import { noPerm } from '../modules'
|
||||||
|
import Dokdo from 'dokdo'
|
||||||
|
|
||||||
|
export default class extends Listener {
|
||||||
|
public async run(msg: Message) {
|
||||||
|
const dokdo = new Dokdo(this.container.client, {
|
||||||
|
aliases: ['dokdo', 'dok'],
|
||||||
|
owners: [this.container.config.bot.owner_ID],
|
||||||
|
noPerm,
|
||||||
|
prefix: this.container.prefix,
|
||||||
|
})
|
||||||
|
if (msg.author.bot) return
|
||||||
|
if (msg.content.startsWith(this.container.prefix)) {
|
||||||
|
const args = msg.content
|
||||||
|
.slice(this.container.prefix.length)
|
||||||
|
.trim()
|
||||||
|
.split(/ +/g)
|
||||||
|
|
||||||
|
if (args[0].startsWith('dokdo') || args[0].startsWith('dok')) {
|
||||||
|
await dokdo.run(msg)
|
||||||
|
} else {
|
||||||
|
if (!this.container.stores.get('commands').get(args[0])) {
|
||||||
|
await msg.channel.sendTyping()
|
||||||
|
const response = await this.container.chatBot.getResponse(msg)
|
||||||
|
await msg.reply(response)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
src/listeners/ready.ts
Normal file
20
src/listeners/ready.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import { ApplyOptions } from '@sapphire/decorators'
|
||||||
|
import { Listener } from '@sapphire/framework'
|
||||||
|
import { ActivityType, Client } from 'discord.js'
|
||||||
|
|
||||||
|
@ApplyOptions<Listener.Options>({ once: true })
|
||||||
|
export default class extends Listener {
|
||||||
|
public async run(client: Client<true>) {
|
||||||
|
function setStatus() {
|
||||||
|
client.user.setActivity({
|
||||||
|
type: ActivityType.Custom,
|
||||||
|
name: 'ㅅ살려주세요..!',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
setStatus()
|
||||||
|
setInterval(() => setStatus(), 600000)
|
||||||
|
|
||||||
|
this.container.logger.info(`[MuffinBotClient] 먹힐 준ㅂ비 완료`)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
import type { Client, Message, TextChannel } from 'discord.js'
|
import type { Client, Message, TextChannel } from 'discord.js'
|
||||||
|
import { container } from '@sapphire/framework'
|
||||||
import type { MaaDatabase } from './database'
|
import type { MaaDatabase } from './database'
|
||||||
import config from '../../config.json'
|
|
||||||
import { NODE_ENV } from './env'
|
|
||||||
|
|
||||||
export default class ChatBot {
|
export default class ChatBot {
|
||||||
public constructor(public db: MaaDatabase) {
|
public constructor(public db: MaaDatabase) {
|
||||||
|
@ -11,20 +10,19 @@ export default class ChatBot {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getResponse(msg: Message): Promise<string> {
|
public async getResponse(msg: Message): Promise<string> {
|
||||||
const prefix = msg.client.prefix
|
const prefix = container.prefix
|
||||||
const data = await this.db.statement.all()
|
const data = await this.db.statement.all()
|
||||||
const args = msg.content.slice(prefix.length).trim().split(/ +/g).join(' ')
|
const args = msg.content.slice(prefix.length).trim().split(/ +/g).join(' ')
|
||||||
const learn = await this.db.learn.findOne(args)
|
const learn = await this.db.learn.findOne(args)
|
||||||
const learnData = learn[Math.floor(Math.random() * learn.length)]
|
const learnData = learn[Math.floor(Math.random() * learn.length)]
|
||||||
const randomNumber = Math.round(Math.random() * (2 - 1) + 1)
|
const randomNumber = Math.round(Math.random() * (2 - 1) + 1)
|
||||||
|
|
||||||
if (NODE_ENV === 'development') {
|
container.logger.debug(`[ChatBot] command: ${args}`)
|
||||||
console.log(randomNumber)
|
|
||||||
console.log(learnData)
|
|
||||||
console.log(args)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (randomNumber === 1 && learnData && args.startsWith(learnData.command)) {
|
if (randomNumber === 1 && learnData && args.startsWith(learnData.command)) {
|
||||||
|
container.logger.debug(
|
||||||
|
`[ChatBot] response: (learnData) ${learnData.result}`,
|
||||||
|
)
|
||||||
return `${learnData.result}\n\`${
|
return `${learnData.result}\n\`${
|
||||||
(await msg.client.users.fetch(learnData.user_id)).username
|
(await msg.client.users.fetch(learnData.user_id)).username
|
||||||
}님이 알려주셨어요.\``
|
}님이 알려주셨어요.\``
|
||||||
|
@ -38,15 +36,19 @@ export default class ChatBot {
|
||||||
} else {
|
} else {
|
||||||
response = data[Math.floor(Math.random() * data.length)].text
|
response = data[Math.floor(Math.random() * data.length)].text
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response) response = '살ㄹ려주세요'
|
if (!response) response = '살ㄹ려주세요'
|
||||||
|
|
||||||
|
container.logger.debug(`[ChatBot] response: ${response}`)
|
||||||
|
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
public async train(client: Client): Promise<ChatBot> {
|
public async train(client: Client): Promise<ChatBot> {
|
||||||
const prefix = client.prefix
|
const prefix = container.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 === container.config.train.user_ID) {
|
||||||
const response = await this.getResponse(msg)
|
const response = await this.getResponse(msg)
|
||||||
await this.db.statement.insert({
|
await this.db.statement.insert({
|
||||||
text: msg.content,
|
text: msg.content,
|
||||||
|
|
|
@ -1,14 +1,20 @@
|
||||||
import { createPool } from 'mysql2/promise'
|
|
||||||
import { LearnTable, NSFWContentTable, StatementTable } from './model'
|
import { LearnTable, NSFWContentTable, StatementTable } from './model'
|
||||||
import config from '../../../config.json'
|
import { container } from '@sapphire/framework'
|
||||||
|
import { createPool } from 'mysql2/promise'
|
||||||
import run from './run'
|
import run from './run'
|
||||||
|
|
||||||
export class MaaDatabase {
|
export class MaaDatabase {
|
||||||
private _database = createPool({
|
private _database = createPool({
|
||||||
...config.mysql,
|
...container.config.mysql,
|
||||||
keepAliveInitialDelay: 10000,
|
keepAliveInitialDelay: 10000,
|
||||||
enableKeepAlive: true,
|
enableKeepAlive: true,
|
||||||
})
|
})
|
||||||
|
.on('release', conn => {
|
||||||
|
container.logger.debug(`[MaaDatabase] ${conn.threadId} Released.`)
|
||||||
|
})
|
||||||
|
.on('connection', conn => {
|
||||||
|
container.logger.debug(`[MaaDatabase] ${conn.threadId} Connected.`)
|
||||||
|
})
|
||||||
public statement = new StatementTable(this._database)
|
public statement = new StatementTable(this._database)
|
||||||
public nsfwContent = new NSFWContentTable(this._database)
|
public nsfwContent = new NSFWContentTable(this._database)
|
||||||
public learn = new LearnTable(this._database)
|
public learn = new LearnTable(this._database)
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { defineConfig } from 'tsup'
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
clean: true,
|
clean: true,
|
||||||
format: ['cjs'],
|
format: ['cjs'],
|
||||||
entry: ['src/index.ts', 'src/Commands/*.ts'],
|
entry: ['src/index.ts', 'src/commands/*.ts', 'src/listeners/*.ts'],
|
||||||
// minify: true,
|
// minify: true,
|
||||||
// splitting: true,
|
// splitting: true,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue