feat: Manually load pieces

reason: Some environment can't load pieces.
This commit is contained in:
Siwoo Jeon 2024-08-03 19:21:06 +09:00
parent 19cee7d44a
commit 13762b85f6
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
14 changed files with 92 additions and 27 deletions

View file

@ -5,6 +5,10 @@ import { version } from '../package.json'
import config from '../config.json' import config from '../config.json'
import semver from 'semver' import semver from 'semver'
import './interaction-handlers/_load'
import './listeners/_load'
import './Commands/_load'
container.config = config container.config = config
container.prefix = config.bot.prefix container.prefix = config.bot.prefix
container.version = version container.version = version
@ -43,6 +47,7 @@ export default class MuffinBot extends SapphireClient {
repliedUser: true, repliedUser: true,
}, },
partials: [Partials.Message, Partials.ThreadMember], partials: [Partials.Message, Partials.ThreadMember],
baseUserDirectory: null,
}) })
} }

5
src/Commands/_load.ts Normal file
View file

@ -0,0 +1,5 @@
import './learning_data'
import './deleteLearn'
import './learn'
import './help'
import './list'

View file

@ -1,3 +1,9 @@
import {
Args,
Command,
container,
DetailedDescriptionCommandObject,
} from '@sapphire/framework'
import { import {
type SelectMenuComponentOptionData, type SelectMenuComponentOptionData,
type Message, type Message,
@ -5,11 +11,6 @@ import {
codeBlock, codeBlock,
} from 'discord.js' } from 'discord.js'
import { ApplyOptions } from '@sapphire/decorators' import { ApplyOptions } from '@sapphire/decorators'
import {
Args,
Command,
DetailedDescriptionCommandObject,
} from '@sapphire/framework'
import { type LearnData } from '../modules' import { type LearnData } from '../modules'
@ApplyOptions<Command.Options>({ @ApplyOptions<Command.Options>({
@ -21,7 +22,7 @@ import { type LearnData } from '../modules'
examples: ['머핀아 삭제 머핀'], examples: ['머핀아 삭제 머핀'],
}, },
}) })
export default class extends Command { class DeleteLearnCommand extends Command {
public async messageRun(msg: Message, args: Args) { public async messageRun(msg: Message, args: Args) {
const command = await args.rest('string').catch(() => null) const command = await args.rest('string').catch(() => null)
const options: SelectMenuComponentOptionData[] = [] const options: SelectMenuComponentOptionData[] = []
@ -77,3 +78,9 @@ export default class extends Command {
}) })
} }
} }
void container.stores.loadPiece({
piece: DeleteLearnCommand,
name: 'delete',
store: 'commands',
})

View file

@ -1,6 +1,6 @@
import { Args, Command, container } from '@sapphire/framework'
import { codeBlock, type Message } from 'discord.js' import { codeBlock, type Message } from 'discord.js'
import { ApplyOptions } from '@sapphire/decorators' import { ApplyOptions } from '@sapphire/decorators'
import { Args, Command } from '@sapphire/framework'
@ApplyOptions<Command.Options>({ @ApplyOptions<Command.Options>({
name: '도움말', name: '도움말',
@ -11,7 +11,7 @@ import { Args, Command } from '@sapphire/framework'
examples: ['머핀아 도움말', '머핀아 도움말 배워'], examples: ['머핀아 도움말', '머핀아 도움말 배워'],
}, },
}) })
export default class extends Command { class HelpCommand extends Command {
public async messageRun(msg: Message, args: Args) { public async messageRun(msg: Message, args: Args) {
const commandName = await args.pick('string').catch(() => null) const commandName = await args.pick('string').catch(() => null)
if ( if (
@ -87,3 +87,9 @@ export default class extends Command {
} }
} }
} }
void container.stores.loadPiece({
piece: HelpCommand,
name: 'help',
store: 'commands',
})

View file

@ -1,4 +1,4 @@
import { type Args, Command } from '@sapphire/framework' import { type Args, Command, container } from '@sapphire/framework'
import { codeBlock, type Message } from 'discord.js' import { codeBlock, type Message } from 'discord.js'
import { ApplyOptions } from '@sapphire/decorators' import { ApplyOptions } from '@sapphire/decorators'
@ -15,7 +15,7 @@ import { ApplyOptions } from '@sapphire/decorators'
], ],
}, },
}) })
export default class extends Command { class LearnCommand extends Command {
public async messageRun(msg: Message, args: Args) { public async messageRun(msg: Message, args: Args) {
if (typeof this.detailedDescription === 'string') return if (typeof this.detailedDescription === 'string') return
const config = this.container.config const config = this.container.config
@ -74,3 +74,9 @@ export default class extends Command {
await msg.reply(`${command}을/를 배웠ㅇ어요.`) await msg.reply(`${command}을/를 배웠ㅇ어요.`)
} }
} }
void container.stores.loadPiece({
piece: LearnCommand,
name: 'learn',
store: 'commands',
})

View file

@ -1,6 +1,6 @@
import { ApplyOptions } from '@sapphire/decorators' import { ApplyOptions } from '@sapphire/decorators'
import { type ResponseData } from '../modules' import { type ResponseData } from '../modules'
import { Command } from '@sapphire/framework' import { Command, container } from '@sapphire/framework'
import { type Message } from 'discord.js' import { type Message } from 'discord.js'
@ApplyOptions<Command.Options>({ @ApplyOptions<Command.Options>({
@ -11,7 +11,7 @@ import { type Message } from 'discord.js'
usage: '머핀아 학습데이터량', usage: '머핀아 학습데이터량',
}, },
}) })
export default class extends Command { class LearnDataCommand extends Command {
public async messageRun(msg: Message<true>) { public async messageRun(msg: Message<true>) {
const db = this.container.database const db = this.container.database
const data = await db.statement.all() const data = await db.statement.all()
@ -30,3 +30,9 @@ nsfw 데이터: ${nsfwData.length}개
${msg.author.username} 단어: ${userData.length}`) ${msg.author.username} 단어: ${userData.length}`)
} }
} }
void container.stores.loadPiece({
piece: LearnDataCommand,
name: 'learn_data',
store: 'commands',
})

View file

@ -1,6 +1,6 @@
import { ApplyOptions } from '@sapphire/decorators' import { ApplyOptions } from '@sapphire/decorators'
import { Message, codeBlock } from 'discord.js' import { Message, codeBlock } from 'discord.js'
import { Command } from '@sapphire/framework' import { Command, container } from '@sapphire/framework'
@ApplyOptions<Command.Options>({ @ApplyOptions<Command.Options>({
name: '리스트', name: '리스트',
@ -10,7 +10,7 @@ import { Command } from '@sapphire/framework'
usage: '머핀아 리스트', usage: '머핀아 리스트',
}, },
}) })
export default class extends Command { class ListCommand extends Command {
public async messageRun(msg: Message<boolean>) { public async messageRun(msg: Message<boolean>) {
const db = this.container.database const db = this.container.database
const data = await db.learn.findOneAnotherKey('user_id', msg.author.id) const data = await db.learn.findOneAnotherKey('user_id', msg.author.id)
@ -41,3 +41,9 @@ export default class extends Command {
}) })
} }
} }
void container.stores.loadPiece({
piece: ListCommand,
name: 'list',
store: 'commands',
})

View file

@ -0,0 +1 @@
import './deleteLearn'

View file

@ -1,4 +1,5 @@
import { import {
container,
InteractionHandler, InteractionHandler,
InteractionHandlerTypes, InteractionHandlerTypes,
} from '@sapphire/framework' } from '@sapphire/framework'
@ -8,7 +9,7 @@ import { ApplyOptions } from '@sapphire/decorators'
@ApplyOptions<InteractionHandler.Options>({ @ApplyOptions<InteractionHandler.Options>({
interactionHandlerType: InteractionHandlerTypes.SelectMenu, interactionHandlerType: InteractionHandlerTypes.SelectMenu,
}) })
export default class extends InteractionHandler { class DeleteLearnHandler extends InteractionHandler {
public async parse(interaction: StringSelectMenuInteraction) { public async parse(interaction: StringSelectMenuInteraction) {
if (interaction.customId !== 'maa$deleteLearn') return this.none() if (interaction.customId !== 'maa$deleteLearn') return this.none()
return this.some() return this.some()
@ -34,3 +35,9 @@ export default class extends InteractionHandler {
}) })
} }
} }
void container.stores.loadPiece({
piece: DeleteLearnHandler,
name: 'deleteLearn',
store: 'interaction-handlers',
})

3
src/listeners/_load.ts Normal file
View file

@ -0,0 +1,3 @@
import './messageCreate'
import './debug'
import './ready'

View file

@ -1,7 +1,13 @@
import { Listener } from '@sapphire/framework' import { Listener, container } from '@sapphire/framework'
export default class extends Listener { class DebugListener extends Listener {
public async run(debug: string) { public async run(debug: string) {
this.container.logger.debug(`[MuffinBot] ${debug}`) this.container.logger.debug(`[MuffinBot] ${debug}`)
} }
} }
void container.stores.loadPiece({
piece: DebugListener,
name: 'debug',
store: 'listeners',
})

View file

@ -1,9 +1,9 @@
import { Listener } from '@sapphire/framework' import { Listener, container } from '@sapphire/framework'
import { type Message } from 'discord.js' import { type Message } from 'discord.js'
import { noPerm } from '../modules' import { noPerm } from '../modules'
import Dokdo from 'dokdo' import Dokdo from 'dokdo'
export default class extends Listener { class MessageCreateListener extends Listener {
public async run(msg: Message) { public async run(msg: Message) {
const prefix = this.container.prefix const prefix = this.container.prefix
const dokdo = new Dokdo(this.container.client, { const dokdo = new Dokdo(this.container.client, {
@ -46,3 +46,9 @@ export default class extends Listener {
} }
} }
} }
void container.stores.loadPiece({
piece: MessageCreateListener,
name: 'messageCreate',
store: 'listeners',
})

View file

@ -1,9 +1,9 @@
import { container, Listener } from '@sapphire/framework'
import { ApplyOptions } from '@sapphire/decorators' import { ApplyOptions } from '@sapphire/decorators'
import { Listener } from '@sapphire/framework'
import { ActivityType, Client } from 'discord.js' import { ActivityType, Client } from 'discord.js'
@ApplyOptions<Listener.Options>({ once: true }) @ApplyOptions<Listener.Options>({ once: true })
export default class extends Listener { class ClientReadyListener extends Listener {
public async run(client: Client<true>) { public async run(client: Client<true>) {
function setStatus() { function setStatus() {
client.user.setActivity({ client.user.setActivity({
@ -18,3 +18,9 @@ export default class extends Listener {
this.container.logger.info(`[MuffinBot] 먹힐 준ㅂ비 완료`) this.container.logger.info(`[MuffinBot] 먹힐 준ㅂ비 완료`)
} }
} }
void container.stores.loadPiece({
piece: ClientReadyListener,
name: 'ready',
store: 'listeners',
})

View file

@ -5,12 +5,7 @@ const sourcemap = process.env.NODE_ENV === 'development' ? true : false
export default defineConfig({ export default defineConfig({
clean: true, clean: true,
format: ['cjs'], format: ['cjs'],
entry: [ entry: ['src/index.ts'],
'src/index.ts',
'src/Commands/*.ts',
'src/listeners/*.ts',
'src/interaction-handlers/*.ts',
],
minify: true, minify: true,
sourcemap, sourcemap,
}) })