Skip to content

Commit bc2a139

Browse files
committed
feat: add defineEmbed and defineAttachment
1 parent fff1b3a commit bc2a139

File tree

4 files changed

+81
-24
lines changed

4 files changed

+81
-24
lines changed

src/define.ts

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import {
22
ActionRowBuilder,
33
type ModalActionRowComponentBuilder,
44
ModalBuilder,
5-
TextInputBuilder
5+
TextInputBuilder,
6+
EmbedBuilder,
7+
AttachmentBuilder,
8+
type BufferResolvable
69
} from 'discord.js'
10+
import type { Stream } from 'node:stream'
711
import type {
812
ArgsDef,
913
CommandExecute,
@@ -15,6 +19,8 @@ import type {
1519
DefineEvent,
1620
DefineEventWithOptions,
1721
DefinePrecondition,
22+
EmbedOptions,
23+
EmbedSetters,
1824
EventCallback,
1925
EventOptions,
2026
HarmonixCommand,
@@ -98,6 +104,12 @@ export const defineContextMenu: DefineContextMenu &
98104
}
99105
}
100106

107+
export const definePrecondition: DefinePrecondition = (
108+
callback: PreconditionCallback
109+
) => {
110+
return { options: {}, callback }
111+
}
112+
101113
export const defineModal = (options: ModalOptions): ModalBuilder => {
102114
const builder = new ModalBuilder()
103115
.setCustomId(options.id)
@@ -137,8 +149,30 @@ export const defineModal = (options: ModalOptions): ModalBuilder => {
137149
return builder
138150
}
139151

140-
export const definePrecondition: DefinePrecondition = (
141-
callback: PreconditionCallback
142-
) => {
143-
return { options: {}, callback }
152+
export const defineEmbed = (options: EmbedOptions) => {
153+
const builder = new EmbedBuilder()
154+
const setters: EmbedSetters = {
155+
color: (value) => builder.setColor(value ?? null),
156+
title: (value) => builder.setTitle(value ?? null),
157+
url: (value) => builder.setURL(value ?? null),
158+
author: (value) => builder.setAuthor(value ?? null),
159+
description: (value) => builder.setDescription(value ?? null),
160+
thumbnail: (value) => builder.setThumbnail(value ?? null),
161+
image: (value) => builder.setImage(value ?? null),
162+
timestamp: () => builder.setTimestamp(),
163+
footer: (value) => builder.setFooter(value ?? null),
164+
fields: (value) => builder.addFields(...(value ?? []))
165+
}
166+
167+
Object.entries(options).forEach(([key, value]) => {
168+
const _key = key as keyof EmbedOptions
169+
170+
setters[_key]!(value)
171+
})
172+
173+
return builder
174+
}
175+
176+
export const defineAttachment = (args: BufferResolvable | Stream) => {
177+
return new AttachmentBuilder(args)
144178
}

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export type * from './commands'
22
export type * from './events'
33
export type * from './contextMenus'
44
export type * from './preconditions'
5-
export type * from './modals'
5+
export type * from './rich'
66
export type * from './harmonix'

src/types/modals.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/types/rich.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
type ColorResolvable,
3+
type EmbedAuthorOptions,
4+
type EmbedFooterOptions,
5+
type APIEmbedField,
6+
type TextInputStyle
7+
} from 'discord.js'
8+
9+
interface TextInput {
10+
id: string
11+
label: string
12+
style: TextInputStyle
13+
maxLength?: number
14+
minLength?: number
15+
placeholder?: string
16+
value?: string
17+
required?: boolean
18+
}
19+
20+
export interface ModalOptions {
21+
id: string
22+
title: string
23+
textInputs?: TextInput[]
24+
}
25+
26+
export interface EmbedOptions {
27+
color?: ColorResolvable
28+
title?: string
29+
url?: string
30+
author?: EmbedAuthorOptions
31+
description?: string
32+
thumbnail?: string
33+
image?: string
34+
timestamp?: number | Date | null
35+
footer?: EmbedFooterOptions
36+
fields?: APIEmbedField[]
37+
}
38+
39+
export type EmbedSetters = {
40+
[K in keyof EmbedOptions]: (value: EmbedOptions[K]) => void
41+
}

0 commit comments

Comments
 (0)