bot/src/Client.ts

93 lines
2.5 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
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-02-06 11:21:58 +00:00
this.once('ready', () => {
2023-02-17 09:14:28 +00:00
console.log(
`Build Number: ${execSync('git rev-parse --short HEAD').toString()}`
)
2023-02-06 11:21:58 +00:00
this.user!.setActivity({
2023-01-23 13:53:02 +00:00
type: ActivityType.Playing,
name: 'ㅅ살려주세요..!',
})
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-24 10:54:49 +00:00
public override destroy() {
2023-02-10 12:09:24 +00:00
this.chatBot.destroy()
2023-01-24 10:54:49 +00:00
super.destroy()
}
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
}
}