Skip to content

Commit 4243263

Browse files
committed
refactor: useEmbed function
1 parent 26dc7d5 commit 4243263

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

src/types/rich.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ export interface EmbedOptions {
1313
description?: string
1414
thumbnail?: string
1515
image?: string
16-
timestamp?: number | Date | null
16+
timestamp?: number | Date
1717
footer?: EmbedFooterOptions
1818
fields?: APIEmbedField[]
1919
}
20-
21-
export type EmbedSetters = {
22-
[K in keyof EmbedOptions]: (value: EmbedOptions[K]) => void
23-
}

src/uses.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
EmbedBuilder,
77
type RestOrArray
88
} from 'discord.js'
9-
import { Stream } from 'node:stream'
10-
import { EmbedOptions, EmbedSetters } from './types'
9+
import type { Stream } from 'node:stream'
10+
import type { EmbedOptions } from './types'
1111

1212
export const useActionRow = <
1313
T extends AnyComponentBuilder = AnyComponentBuilder
@@ -21,24 +21,37 @@ export const useActionRow = <
2121

2222
export const useEmbed = (options: EmbedOptions) => {
2323
const builder = new EmbedBuilder()
24-
const setters: EmbedSetters = {
25-
color: (value) => builder.setColor(value ?? null),
26-
title: (value) => builder.setTitle(value ?? null),
27-
url: (value) => builder.setURL(value ?? null),
28-
author: (value) => builder.setAuthor(value ?? null),
29-
description: (value) => builder.setDescription(value ?? null),
30-
thumbnail: (value) => builder.setThumbnail(value ?? null),
31-
image: (value) => builder.setImage(value ?? null),
32-
timestamp: () => builder.setTimestamp(),
33-
footer: (value) => builder.setFooter(value ?? null),
34-
fields: (value) => builder.addFields(...(value ?? []))
35-
}
36-
37-
Object.entries(options).forEach(([key, value]) => {
38-
const _key = key as keyof EmbedOptions
3924

40-
setters[_key]!(value)
41-
})
25+
if (options.color) {
26+
builder.setColor(options.color)
27+
}
28+
if (options.title) {
29+
builder.setTitle(options.title)
30+
}
31+
if (options.url) {
32+
builder.setURL(options.url)
33+
}
34+
if (options.author) {
35+
builder.setAuthor(options.author)
36+
}
37+
if (options.description) {
38+
builder.setDescription(options.description)
39+
}
40+
if (options.thumbnail) {
41+
builder.setThumbnail(options.thumbnail)
42+
}
43+
if (options.image) {
44+
builder.setImage(options.image)
45+
}
46+
if (options.timestamp) {
47+
builder.setTimestamp(options.timestamp)
48+
}
49+
if (options.footer) {
50+
builder.setFooter(options.footer)
51+
}
52+
if (options.fields) {
53+
builder.addFields(...options.fields)
54+
}
4255

4356
return builder
4457
}

0 commit comments

Comments
 (0)