bot/src/Client.ts

105 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-02-02 22:17:58 +09:00
import {
ActivityType,
Client,
Collection,
GatewayIntentBits,
TextChannel,
} from 'discord.js'
2024-05-30 18:50:14 +09:00
import { type Command, noPerm, ChatBot, NODE_ENV } from './modules'
2023-01-30 17:54:08 +09:00
import { readdirSync } from 'node:fs'
import { join } from 'node:path'
2023-06-05 14:06:03 +09:00
import config from '../config.json'
2024-05-30 20:34:15 +09:00
import Dokdo from 'dokdo'
2023-01-23 22:53:02 +09:00
2024-06-08 22:51:02 +09:00
const prefix = '머핀아 '
2023-01-25 20:23:01 +09:00
2024-05-30 22:15:23 +09:00
export default class MuffinBot extends Client {
2023-02-11 15:03:25 +09:00
get chatBot() {
2024-05-30 20:34:15 +09:00
return new ChatBot()
}
get dokdo() {
return new Dokdo(this, {
aliases: ['dokdo', 'dok'],
owners: [config.bot.owner_ID],
noPerm,
prefix,
})
2023-02-11 15:03:25 +09:00
}
2024-05-30 22:15:23 +09:00
get modules(): Collection<string, Command> {
return this.#modules
}
2023-02-02 22:17:58 +09:00
#modules: Collection<string, Command> = new Collection()
2023-01-23 22:53:02 +09:00
public constructor() {
super({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
})
}
public override login(): Promise<string> {
2023-02-08 21:00:37 +09:00
if (NODE_ENV === 'development') this.on('debug', console.info)
2023-01-25 20:23:01 +09:00
this.chatBot.train(this)
2023-01-30 17:54:08 +09:00
readdirSync(join(__dirname, 'Commands')).forEach(file => {
const a = require(join(__dirname, 'Commands', file))
const b: Command = new a.default()
2024-05-30 22:15:23 +09:00
this.modules.set(b.name, b)
2024-05-30 18:50:14 +09:00
if (NODE_ENV === 'development') console.log(`${b.name}가 로ㄷ드됨`)
2023-01-30 17:54:08 +09:00
})
2023-11-25 13:04:29 +09:00
this.once('ready', client => {
function setStatus() {
client.user.setActivity({
type: ActivityType.Custom,
name: 'ㅅ살려주세요..!',
})
}
setStatus()
setInterval(() => setStatus(), 600000)
2024-06-06 12:00:09 +09:00
console.log(`먹힐 준ㅂ비 완료`)
2023-01-30 17:54:08 +09:00
}).on('messageCreate', async msg => {
2023-12-02 16:22:48 +09:00
const args: string[] = msg.content
.slice(prefix.length)
.trim()
.split(/ +/g)
if (NODE_ENV === 'development') console.log(args)
2023-01-23 22:53:02 +09:00
if (msg.author.bot) return
2024-05-30 18:50:14 +09:00
if (msg.content.startsWith(prefix)) {
2024-05-30 20:34:15 +09:00
if (args[0].startsWith('dokdo') || args[0].startsWith('dok')) {
await this.dokdo.run(msg)
} else {
if (msg.channel instanceof TextChannel) {
await msg.channel.sendTyping()
2024-05-30 22:15:23 +09:00
const command = this.modules.get(args.shift()!.toLowerCase())
2023-12-02 16:22:48 +09:00
2024-05-30 20:34:15 +09:00
if (command) {
if (command.noPerm && msg.author.id !== config.bot.owner_ID)
return await noPerm(msg)
2023-12-02 16:22:48 +09:00
2024-05-30 20:34:15 +09:00
await command.execute(msg, args)
} else {
const response = await this.chatBot.getResponse(msg)
await msg.reply(response)
}
2023-12-02 16:22:48 +09:00
}
2023-02-02 22:17:58 +09:00
}
2023-01-30 17:54:08 +09:00
}
2023-01-23 22:53:02 +09:00
})
2023-06-05 14:06:03 +09:00
return super.login(config.bot.token)
2023-01-23 22:53:02 +09:00
}
}
2023-01-30 17:54:08 +09:00
declare module 'discord.js' {
interface Client {
2023-02-11 15:03:25 +09:00
get chatBot(): ChatBot
2024-05-30 22:15:23 +09:00
get modules(): Collection<string, Command>
2023-01-30 17:54:08 +09:00
}
}