Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new bases to follow #6

Merged
merged 8 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import "dotenv/config";
import Discord from "discord.js";
import 'dotenv/config'
import MiduClient from './lib/MiduClient'

const client = new Discord.Client({
intents: [Number(process.env.DISCORD_INTENTS)],
});
// eslint-disable-next-line no-unused-vars
const client = new MiduClient()

client.once("ready", () => {
console.log("🥳 Bot is ready!");
});
client.on('messageCreate', async (message) => {
if (message.author.bot) return
const prefix = `${process.env.DISCORD_PREFIX}`
if (!message.content.startsWith(prefix)) return
const args = message.content.slice(prefix.length).trim().split(/ +/g)
const commandName = args?.shift()?.toLowerCase()
const command = client.commandHandler.modules.find(m => m.aliases.includes(`${commandName}`))
console.log('Command', command)
console.log('CommandName', commandName)
if (!command) return

client.login(process.env.DISCORD_TOKEN);
try {
command.run(message, args)
} catch (error) {
console.error(error)
}
})
82 changes: 82 additions & 0 deletions src/lib/BaseHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Collection } from 'discord.js'
import EventEmitter from 'events'
import { readdirSync, statSync } from 'fs'
import path from 'path'
import BaseModule from './BaseModule'
import MiduBot from './MiduClient'
export interface BaseHandlerData {
path: string
}
class BaseHandler extends EventEmitter {
client: MiduBot
modules: Collection<string, BaseModule>
path: string
constructor (client: MiduBot, { path }: BaseHandlerData) {
super()
this.client = client
this.path = path
this.modules = new Collection()
}

register (mod: BaseModule, filepath: string) {
mod.filepath = filepath
this.modules.set(mod.id, mod)

if (mod.categoryID !== 'default') return
const dirs = path.dirname(filepath).split(path.sep)
mod.categoryID = dirs[dirs.length - 1]
}

deregister (mod: BaseModule) {
if (mod.filepath) delete require.cache[require.resolve(mod.filepath)]
this.modules.delete(mod.id)
}

load (filepath: string, isReload: boolean = false) {
console.log('path', filepath)
let mod = require(filepath)
// eslint-disable-next-line new-cap
mod = new mod()

this.register(mod, filepath)
return mod
}

reload (id: string | BaseModule) {
const mod = this.modules.get(id.toString())
if (!mod) return

this.deregister(mod)

const filepath = mod.filepath
const newMod = this.load(filepath, true)
return newMod
}

remove (id: string | BaseModule) {
const mod = this.modules.get(id.toString())
if (!mod) return
this.deregister(mod)
return mod
}

loadAll () {
const filepaths = BaseHandler.readdirRecursive(this.path)
for (let filepath of filepaths) {
filepath = path.resolve(filepath)
this.load(filepath)
}
}

static readdirRecursive (directory: string): string[] {
const result: string[] = []
const files = readdirSync(directory)
for (const file of files) {
const filepath = path.join(directory, file)
if (statSync(filepath).isDirectory()) this.readdirRecursive(filepath)
else result.push(filepath)
}
return result
}
}
export default BaseHandler
31 changes: 31 additions & 0 deletions src/lib/BaseModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import BaseHandler from './BaseHandler'
import MiduClient from './MiduClient'
export interface BaseModuleData {
category?: string
}
class BaseModule {
id!: string
filepath!: string
category!: string
client!: MiduClient
handler!: BaseHandler
categoryID: string
constructor (id: string, {
category = 'default'
}: BaseModuleData) {
this.id = id
this.categoryID = category
}

reload () {
this.handler.reload(this.id)
}

remove () {
this.handler.remove(this.id)
}

toString () { return this.id }
}

export default BaseModule
29 changes: 29 additions & 0 deletions src/lib/MiduClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import CommandHandler from './commands/CommandHandler'

import { Client } from 'discord.js'

class MiduClient extends Client {
commandHandler: CommandHandler
constructor () {
super({
intents: [Number(process.env.DISCORD_INTENTS)]
})
this.once('ready', () => {
console.log('🥳 Bot is ready!')
})
this.commandHandler = new CommandHandler(this)
this.start()
}

async start () {
try {
this.commandHandler.loadAll()
await this.login()
} catch (error) {
console.error(error)
process.exit(1)
}
}
}

export default MiduClient
34 changes: 34 additions & 0 deletions src/lib/commands/Command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Message } from 'discord.js'
import BaseModule from '../BaseModule'
interface CommandData {
aliases: string[]
description?: string
usage?: string
onlyOwner?: boolean
__filepath?: string
category?: string
}
abstract class Command extends BaseModule {
aliases: string[]
description: string
usage: string
onlyOwner: boolean
__filepath: string
category: string

constructor (id: string, { aliases, description, usage, onlyOwner, category }: CommandData) {
super(id, {
category
})
this.aliases = []
this.description = ''
this.usage = ''
this.onlyOwner = false

this.__filepath = ''
this.category = ''
}

abstract run (message: Message, args: string[]): void
}
export default Command
19 changes: 19 additions & 0 deletions src/lib/commands/CommandHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Collection } from 'discord.js'
import src from '../../utils/src'
import BaseHandler from '../BaseHandler'
import MiduBot from '../MiduClient'
import Command from './Command'

class CommandHandler extends BaseHandler {
client: MiduBot
modules: Collection<string, Command>
constructor (client: MiduBot) {
super(client, {
path: src('modules', 'commands')
})
this.client = client
this.modules = new Collection()
}
}

export default CommandHandler
16 changes: 16 additions & 0 deletions src/modules/commands/test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Message } from 'discord.js'
import Command from '../../../lib/commands/Command'

class TestCommand extends Command {
constructor () {
super('test', {
aliases: ['test']
})
}

async run (message: Message, args: string[]) {
message.channel.send('Test command ran!')
}
}

export default TestCommand
5 changes: 5 additions & 0 deletions src/utils/src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import path from 'path'

export default function src (...dir: string[]) {
return path.join(__dirname, '..', ...dir)
}