Skip to content

Commit 260da9f

Browse files
committed
feat: define of buttons, modals and select menus are now defined in separate folder
1 parent d3abf2e commit 260da9f

16 files changed

+595
-363
lines changed

src/define.ts

+15-207
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
import {
22
ActionRowBuilder,
3-
ModalBuilder,
4-
TextInputBuilder,
53
EmbedBuilder,
64
AttachmentBuilder,
75
type BufferResolvable,
8-
ButtonBuilder,
9-
ButtonStyle,
106
type AnyComponentBuilder,
117
type RestOrArray,
12-
StringSelectMenuBuilder,
13-
UserSelectMenuBuilder,
14-
ChannelSelectMenuBuilder,
15-
RoleSelectMenuBuilder,
16-
StringSelectMenuOptionBuilder,
17-
MentionableSelectMenuBuilder
8+
ClientEvents
189
} from 'discord.js'
1910
import type { Stream } from 'node:stream'
2011
import type {
21-
ButtonOptions,
22-
ChannelSelectMenuOptions,
2312
CommandConfig,
2413
CommandExecute,
2514
ContextMenuCallback,
@@ -28,6 +17,7 @@ import type {
2817
DefineContextMenuWithOptions,
2918
DefineEvent,
3019
DefineEventWithOptions,
20+
DefineModal,
3121
DefinePrecondition,
3222
DefinePreconditionWithName,
3323
EmbedOptions,
@@ -38,31 +28,25 @@ import type {
3828
HarmonixConfig,
3929
HarmonixContextMenu,
4030
HarmonixEvent,
41-
HarmonixEvents,
42-
MentionableSelectMenuOptions,
43-
ModalOptions,
4431
OptionsDef,
45-
PreconditionCallback,
46-
RoleSelectMenuOptions,
47-
SelectMenuOptions,
48-
StringSelectMenuOptions,
49-
UserSelectMenuOptions
32+
PreconditionCallback
5033
} from './types'
51-
import { createError } from './harmonix'
34+
import { DefineButton } from './types'
35+
import { DefineSelectMenu } from './types/select-menus'
5236

5337
export const defineHarmonixConfig = (config: HarmonixConfig) => {
5438
return config
5539
}
5640

5741
export const defineEvent: DefineEvent & DefineEventWithOptions = <
58-
Event extends keyof HarmonixEvents = any
42+
Event extends keyof ClientEvents = any
5943
>(
6044
...args: [EventOptions | EventCallback<Event>, EventCallback<Event>?]
6145
): HarmonixEvent => {
6246
let options: EventOptions = {}
6347

6448
if (args.length === 1) {
65-
const [callback] = args as [EventCallback<keyof HarmonixEvents>]
49+
const [callback] = args as [EventCallback<keyof ClientEvents>]
6650

6751
return {
6852
options,
@@ -71,7 +55,7 @@ export const defineEvent: DefineEvent & DefineEventWithOptions = <
7155
} else {
7256
const [opts, callback] = args as [
7357
EventOptions,
74-
EventCallback<keyof HarmonixEvents>
58+
EventCallback<keyof ClientEvents>
7559
]
7660

7761
options = opts
@@ -150,42 +134,6 @@ export const defineActionRow = <
150134
return builder
151135
}
152136

153-
export const defineModal = (options: ModalOptions): ModalBuilder => {
154-
const builder = new ModalBuilder()
155-
.setCustomId(options.id)
156-
.setTitle(options.title)
157-
158-
if (options.textInputs) {
159-
for (const input of options.textInputs) {
160-
const inputBuilder = new TextInputBuilder()
161-
.setCustomId(input.id)
162-
.setLabel(input.label)
163-
.setStyle(input.style)
164-
165-
if (input.maxLength) {
166-
inputBuilder.setMaxLength(input.maxLength)
167-
}
168-
if (input.minLength) {
169-
inputBuilder.setMinLength(input.minLength)
170-
}
171-
if (input.placeholder) {
172-
inputBuilder.setPlaceholder(input.placeholder)
173-
}
174-
if (input.value) {
175-
inputBuilder.setValue(input.value)
176-
}
177-
if (input.required) {
178-
inputBuilder.setRequired(input.required)
179-
}
180-
const row = defineActionRow(inputBuilder)
181-
182-
builder.addComponents(row)
183-
}
184-
}
185-
186-
return builder
187-
}
188-
189137
export const defineEmbed = (options: EmbedOptions) => {
190138
const builder = new EmbedBuilder()
191139
const setters: EmbedSetters = {
@@ -214,154 +162,14 @@ export const defineAttachment = (args: BufferResolvable | Stream) => {
214162
return new AttachmentBuilder(args)
215163
}
216164

217-
export const defineButton = (options: ButtonOptions) => {
218-
const builder = new ButtonBuilder()
219-
.setCustomId(options.id)
220-
.setStyle(options.style ? ButtonStyle[options.style] : ButtonStyle.Primary)
221-
222-
if (options.label) {
223-
builder.setLabel(options.label)
224-
}
225-
if (options.emoji) {
226-
builder.setEmoji(options.emoji)
227-
}
228-
if (options.url) {
229-
builder.setURL(options.url)
230-
}
231-
if (options.disabled) {
232-
builder.setDisabled(options.disabled)
233-
}
234-
235-
return builder
165+
export const defineButton: DefineButton = (config, callback) => {
166+
return { config, callback }
236167
}
237168

238-
export const defineSelectMenu = (options: SelectMenuOptions) => {
239-
const { id, placeholder, type, disabled, minValues, maxValues } = options
240-
241-
switch (type) {
242-
case 'String': {
243-
const stringOptions = options as StringSelectMenuOptions
244-
const selectMenu = new StringSelectMenuBuilder()
245-
.setCustomId(id)
246-
.setPlaceholder(placeholder)
247-
248-
if (disabled) {
249-
selectMenu.setDisabled(disabled)
250-
}
251-
if (minValues) {
252-
selectMenu.setMinValues(minValues)
253-
}
254-
if (maxValues) {
255-
selectMenu.setMaxValues(maxValues)
256-
}
257-
stringOptions.options.forEach((option) => {
258-
const optionBuilder = new StringSelectMenuOptionBuilder()
259-
.setLabel(option.label)
260-
.setValue(option.value)
261-
262-
if (option.description) {
263-
optionBuilder.setDescription(option.description)
264-
}
265-
if (option.emoji) {
266-
optionBuilder.setEmoji(option.emoji)
267-
}
268-
if (option.default) {
269-
optionBuilder.setDefault(true)
270-
}
271-
272-
selectMenu.addOptions(optionBuilder)
273-
})
274-
275-
return selectMenu
276-
}
277-
case 'User': {
278-
const userOptions = options as UserSelectMenuOptions
279-
const selectMenu = new UserSelectMenuBuilder()
280-
.setCustomId(id)
281-
.setPlaceholder(placeholder)
282-
283-
if (disabled) {
284-
selectMenu.setDisabled(disabled)
285-
}
286-
if (minValues) {
287-
selectMenu.setMinValues(minValues)
288-
}
289-
if (maxValues) {
290-
selectMenu.setMaxValues(maxValues)
291-
}
292-
if (userOptions.defaultUsers) {
293-
selectMenu.setDefaultUsers(userOptions.defaultUsers)
294-
}
295-
296-
return selectMenu
297-
}
298-
case 'Channel': {
299-
const channelOptions = options as ChannelSelectMenuOptions
300-
const selectMenu = new ChannelSelectMenuBuilder()
301-
.setCustomId(id)
302-
.setPlaceholder(placeholder)
303-
304-
if (disabled) {
305-
selectMenu.setDisabled(disabled)
306-
}
307-
if (minValues) {
308-
selectMenu.setMinValues(minValues)
309-
}
310-
if (maxValues) {
311-
selectMenu.setMaxValues(maxValues)
312-
}
313-
if (channelOptions.channelTypes) {
314-
selectMenu.addChannelTypes(...channelOptions.channelTypes)
315-
}
316-
if (channelOptions.defaultChannels) {
317-
selectMenu.setDefaultChannels(channelOptions.defaultChannels)
318-
}
319-
320-
return selectMenu
321-
}
322-
case 'Role': {
323-
const roleOptions = options as RoleSelectMenuOptions
324-
const selectMenu = new RoleSelectMenuBuilder()
325-
.setCustomId(id)
326-
.setPlaceholder(placeholder)
327-
328-
if (disabled) {
329-
selectMenu.setDisabled(disabled)
330-
}
331-
if (minValues) {
332-
selectMenu.setMinValues(minValues)
333-
}
334-
if (maxValues) {
335-
selectMenu.setMaxValues(maxValues)
336-
}
337-
if (roleOptions.defaultRoles) {
338-
selectMenu.setDefaultRoles(roleOptions.defaultRoles)
339-
}
340-
341-
return selectMenu
342-
}
343-
case 'Mentionable': {
344-
const mentionableOptions = options as MentionableSelectMenuOptions
345-
const selectMenu = new MentionableSelectMenuBuilder()
346-
.setCustomId(id)
347-
.setPlaceholder(placeholder)
348-
349-
if (disabled) {
350-
selectMenu.setDisabled(disabled)
351-
}
352-
if (minValues) {
353-
selectMenu.setMinValues(minValues)
354-
}
355-
if (maxValues) {
356-
selectMenu.setMaxValues(maxValues)
357-
}
358-
if (mentionableOptions.defaultValues) {
359-
selectMenu.setDefaultValues(mentionableOptions.defaultValues)
360-
}
169+
export const defineModal: DefineModal = (config, callback) => {
170+
return { config, callback }
171+
}
361172

362-
return selectMenu
363-
}
364-
default:
365-
throw createError('Invalid select menu type')
366-
}
173+
export const defineSelectMenu: DefineSelectMenu = (config, callback) => {
174+
return { config, callback }
367175
}

0 commit comments

Comments
 (0)