feat: Listener
This commit is contained in:
parent
9c897a266d
commit
b918324c5f
8 changed files with 91 additions and 70 deletions
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "muffinbot",
|
||||
"version": "3.0.0-cake.d240622a",
|
||||
"version": "3.0.0-cake.d240622b",
|
||||
"main": "dist/index.js",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
|
@ -25,7 +25,7 @@
|
|||
},
|
||||
"scripts": {
|
||||
"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"
|
||||
},
|
||||
"packageManager": "yarn@4.2.2"
|
||||
|
|
|
@ -1,25 +1,14 @@
|
|||
import { noPerm, ChatBot, NODE_ENV, MaaDatabase } from './modules'
|
||||
import { SapphireClient, container } from '@sapphire/framework'
|
||||
import { ActivityType, GatewayIntentBits, Snowflake } from 'discord.js'
|
||||
import { SapphireClient, container, LogLevel } from '@sapphire/framework'
|
||||
import { GatewayIntentBits, type Snowflake } from 'discord.js'
|
||||
import { ChatBot, NODE_ENV, MaaDatabase } from './modules'
|
||||
import config from '../config.json'
|
||||
import Dokdo from 'dokdo'
|
||||
|
||||
container.config = config
|
||||
container.prefix = '머핀아 '
|
||||
container.database = new MaaDatabase()
|
||||
container.chatBot = new ChatBot(container.database)
|
||||
container.config = config
|
||||
|
||||
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() {
|
||||
super({
|
||||
intents: [
|
||||
|
@ -29,53 +18,18 @@ export default class MuffinBot extends SapphireClient {
|
|||
],
|
||||
loadMessageCommandListeners: true,
|
||||
defaultPrefix: container.prefix,
|
||||
logger: {
|
||||
level: NODE_ENV === 'development' ? LogLevel.Debug : LogLevel.Info,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
public override async login(): Promise<string> {
|
||||
if (NODE_ENV === 'development') this.on('debug', console.info)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
await container.chatBot.train(this)
|
||||
return super.login(config.bot.token)
|
||||
}
|
||||
}
|
||||
|
||||
declare module 'discord.js' {
|
||||
interface Client {
|
||||
chatBot: ChatBot
|
||||
prefix: string
|
||||
dokdo: Dokdo
|
||||
}
|
||||
}
|
||||
|
||||
declare module '@sapphire/pieces' {
|
||||
interface Container {
|
||||
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 { container } from '@sapphire/framework'
|
||||
import type { MaaDatabase } from './database'
|
||||
import config from '../../config.json'
|
||||
import { NODE_ENV } from './env'
|
||||
|
||||
export default class ChatBot {
|
||||
public constructor(public db: MaaDatabase) {
|
||||
|
@ -11,20 +10,19 @@ export default class ChatBot {
|
|||
}
|
||||
|
||||
public async getResponse(msg: Message): Promise<string> {
|
||||
const prefix = msg.client.prefix
|
||||
const prefix = container.prefix
|
||||
const data = await this.db.statement.all()
|
||||
const args = msg.content.slice(prefix.length).trim().split(/ +/g).join(' ')
|
||||
const learn = await this.db.learn.findOne(args)
|
||||
const learnData = learn[Math.floor(Math.random() * learn.length)]
|
||||
const randomNumber = Math.round(Math.random() * (2 - 1) + 1)
|
||||
|
||||
if (NODE_ENV === 'development') {
|
||||
console.log(randomNumber)
|
||||
console.log(learnData)
|
||||
console.log(args)
|
||||
}
|
||||
container.logger.debug(`[ChatBot] command: ${args}`)
|
||||
|
||||
if (randomNumber === 1 && learnData && args.startsWith(learnData.command)) {
|
||||
container.logger.debug(
|
||||
`[ChatBot] response: (learnData) ${learnData.result}`,
|
||||
)
|
||||
return `${learnData.result}\n\`${
|
||||
(await msg.client.users.fetch(learnData.user_id)).username
|
||||
}님이 알려주셨어요.\``
|
||||
|
@ -38,15 +36,19 @@ export default class ChatBot {
|
|||
} else {
|
||||
response = data[Math.floor(Math.random() * data.length)].text
|
||||
}
|
||||
|
||||
if (!response) response = '살ㄹ려주세요'
|
||||
|
||||
container.logger.debug(`[ChatBot] response: ${response}`)
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
public async train(client: Client): Promise<ChatBot> {
|
||||
const prefix = client.prefix
|
||||
const prefix = container.prefix
|
||||
client.on('messageCreate', async msg => {
|
||||
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)
|
||||
await this.db.statement.insert({
|
||||
text: msg.content,
|
||||
|
|
|
@ -1,14 +1,20 @@
|
|||
import { createPool } from 'mysql2/promise'
|
||||
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'
|
||||
|
||||
export class MaaDatabase {
|
||||
private _database = createPool({
|
||||
...config.mysql,
|
||||
...container.config.mysql,
|
||||
keepAliveInitialDelay: 10000,
|
||||
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 nsfwContent = new NSFWContentTable(this._database)
|
||||
public learn = new LearnTable(this._database)
|
||||
|
|
|
@ -3,7 +3,7 @@ import { defineConfig } from 'tsup'
|
|||
export default defineConfig({
|
||||
clean: true,
|
||||
format: ['cjs'],
|
||||
entry: ['src/index.ts', 'src/Commands/*.ts'],
|
||||
entry: ['src/index.ts', 'src/commands/*.ts', 'src/listeners/*.ts'],
|
||||
// minify: true,
|
||||
// splitting: true,
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue