bot/src/Client.ts

105 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-02-02 13:17:58 +00:00
import {
ActivityType,
Client,
Collection,
GatewayIntentBits,
TextChannel,
} from 'discord.js'
2023-02-08 12:00:37 +00:00
import { Command, noPerm, ChatBot, NODE_ENV } from './modules'
2023-01-30 08:54:08 +00:00
import { readdirSync } from 'node:fs'
import { join } from 'node:path'
2023-02-17 09:14:28 +00:00
import { execSync } from 'node:child_process'
2023-06-05 05:06:03 +00:00
import config from '../config.json'
2023-01-23 13:53:02 +00:00
2023-01-30 08:54:08 +00:00
const prefix = '멒힌아 '
2023-01-25 11:23:01 +00:00
2023-01-23 13:53:02 +00:00
export default class MuffinAI extends Client {
2023-02-11 06:03:25 +00:00
get chatBot() {
return this.#chatBot
}
#chatBot = new ChatBot()
2023-02-02 13:17:58 +00:00
#modules: Collection<string, Command> = new Collection()
2023-01-23 13:53:02 +00:00
public constructor() {
super({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
})
}
public override login(): Promise<string> {
2023-02-08 12:00:37 +00:00
if (NODE_ENV === 'development') this.on('debug', console.info)
2023-01-25 11:23:01 +00:00
this.chatBot.train(this)
2023-01-30 08:54:08 +00:00
2023-10-04 12:35:14 +00:00
setInterval(async () => {
const db = await this.chatBot.db.getConnection()
await db.beginTransaction()
await db.ping()
await db.commit()
db.release()
}, 600000)
2023-01-30 08:54:08 +00:00
readdirSync(join(__dirname, 'Commands')).forEach(file => {
const a = require(join(__dirname, 'Commands', file))
const b: Command = new a.default()
2023-02-02 13:17:58 +00:00
this.#modules.set(b.name, b)
2023-02-08 12:00:37 +00:00
if (NODE_ENV === 'development') console.log(b.name)
2023-01-30 08:54:08 +00:00
})
2023-11-25 04:04:29 +00:00
this.once('ready', client => {
2023-02-17 09:14:28 +00:00
console.log(
2023-09-23 03:51:15 +00:00
`Build Number: ${execSync('git rev-parse --short HEAD').toString()}`,
2023-02-17 09:14:28 +00:00
)
2023-11-25 04:04:29 +00:00
function setStatus() {
client.user.setActivity({
type: ActivityType.Custom,
name: 'ㅅ살려주세요..!',
})
}
setStatus()
setInterval(() => setStatus(), 600000)
2023-01-23 14:00:35 +00:00
console.log(`먹힐 준비 완료`)
2023-01-30 08:54:08 +00:00
}).on('messageCreate', async msg => {
2023-01-23 13:53:02 +00:00
if (msg.author.bot) return
2023-02-01 05:43:14 +00:00
if (msg.content.startsWith('머핀아 ')) {
2023-02-02 13:17:58 +00:00
if (msg.channel instanceof TextChannel) {
await msg.channel.sendTyping()
2023-02-08 12:00:37 +00:00
this.chatBot //
.getResponse(msg)
.then(response => {
msg.channel.send(response)
})
2023-02-02 13:17:58 +00:00
}
2023-02-01 05:43:14 +00:00
} else if (msg.content.startsWith(prefix)) {
2023-02-04 10:47:48 +00:00
if (msg.channel instanceof TextChannel) if (msg.channel.nsfw) return
2023-01-30 08:54:08 +00:00
const args: string[] = msg.content
.slice(prefix.length)
.trim()
2023-02-08 12:00:37 +00:00
.split(/ +/g)
if (NODE_ENV === 'development') console.log(args)
const command = this.#modules.get(args.shift()!.toLowerCase())
2023-01-30 08:54:08 +00:00
if (!command) return
2023-06-05 05:06:03 +00:00
if (command.noPerm && msg.author.id !== config.bot.owner_ID)
2023-01-30 08:54:08 +00:00
return await noPerm(msg)
2023-02-08 12:00:37 +00:00
2023-01-30 08:54:08 +00:00
command.execute(msg, args)
}
2023-01-23 13:53:02 +00:00
})
2023-06-05 05:06:03 +00:00
return super.login(config.bot.token)
2023-01-23 13:53:02 +00:00
}
}
2023-01-30 08:54:08 +00:00
declare module 'discord.js' {
interface Client {
2023-02-11 06:03:25 +00:00
get chatBot(): ChatBot
2023-01-30 08:54:08 +00:00
}
}