@zoodogood/utils 0.5.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +3 -0
- package/package.json +3 -2
- package/packages/discordjs/mod.js +122 -0
- package/retype.yml +11 -0
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zoodogood/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.6.2",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"homepage": "https://zoodogood.github.io/utils",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
},
|
|
12
12
|
"exports": {
|
|
13
13
|
"./primitives": "./packages/primitives/mod.js",
|
|
14
|
-
"./objectives": "./packages/objectives/mod.js"
|
|
14
|
+
"./objectives": "./packages/objectives/mod.js",
|
|
15
|
+
"./discordjs": "./packages/discordjs/mod.js"
|
|
15
16
|
},
|
|
16
17
|
"repository": {
|
|
17
18
|
"type": "git",
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { EmbedBuilder, ModalBuilder, resolveColor } from 'discord.js';
|
|
2
|
+
import { ComponentType } from 'discord-api-types/v10';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
function isEmptyEmbed(embed){
|
|
7
|
+
|
|
8
|
+
const DEFAULT_PROPERTIES = {
|
|
9
|
+
title: null,
|
|
10
|
+
author: null,
|
|
11
|
+
thumbnail: null,
|
|
12
|
+
description: null,
|
|
13
|
+
fields: [],
|
|
14
|
+
image: null,
|
|
15
|
+
footer: null,
|
|
16
|
+
timestamp: Number.NaN,
|
|
17
|
+
video: null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
// in isEmptyEmbed context is a good compare
|
|
22
|
+
const isEqualDefault = (key) => String(embed[key]) === String(DEFAULT_PROPERTIES[key]);
|
|
23
|
+
|
|
24
|
+
const isEveryPropertyDefault = Object.keys(DEFAULT_PROPERTIES)
|
|
25
|
+
.every(isEqualDefault);
|
|
26
|
+
|
|
27
|
+
return isEveryPropertyDefault;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
function CreateMessage({
|
|
32
|
+
content,
|
|
33
|
+
title, url, author, thumbnail, description, color, fields, image, video, footer, timestamp,
|
|
34
|
+
ephemeral, fetchReply,
|
|
35
|
+
components, files
|
|
36
|
+
|
|
37
|
+
}){
|
|
38
|
+
const message = {};
|
|
39
|
+
|
|
40
|
+
thumbnail &&= { url: thumbnail };
|
|
41
|
+
image &&= { url: image };
|
|
42
|
+
video &&= { url: video };
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
color = resolveColor(color ?? "Random");
|
|
46
|
+
|
|
47
|
+
const embed = new EmbedBuilder({
|
|
48
|
+
title, url, author, thumbnail,
|
|
49
|
+
description, color, fields,
|
|
50
|
+
image, video, footer, timestamp
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
if (!isEmptyEmbed(embed)){
|
|
54
|
+
message.embeds = [embed];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (files){
|
|
58
|
+
message.files = files;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
message.components = components ? SimplifyComponents(components) : null;
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
message.content = content;
|
|
65
|
+
message.ephemeral = ephemeral;
|
|
66
|
+
message.fetchReply = fetchReply;
|
|
67
|
+
|
|
68
|
+
return message;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function CreateModal({title, customId, components}){
|
|
72
|
+
components = SimplifyComponents(components);
|
|
73
|
+
return new ModalBuilder({title, customId, components});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
function SimplifyComponents(data){
|
|
79
|
+
|
|
80
|
+
if (data instanceof Array && data.length === 0)
|
|
81
|
+
return [];
|
|
82
|
+
|
|
83
|
+
const isComponent = (component) => "type" in component;
|
|
84
|
+
const isActionRow = (component) => component instanceof Array && isComponent(component.at(0)) || component instanceof MessageActionRow;
|
|
85
|
+
const isComponents = (component) => component.length && isActionRow(component.at(0));
|
|
86
|
+
|
|
87
|
+
const argumentType = [
|
|
88
|
+
isComponent(data),
|
|
89
|
+
isActionRow(data),
|
|
90
|
+
isComponents(data)
|
|
91
|
+
].findIndex(Boolean);
|
|
92
|
+
|
|
93
|
+
if (argumentType === -1)
|
|
94
|
+
throw new TypeError("expected component");
|
|
95
|
+
|
|
96
|
+
const inArray = (component) => [component];
|
|
97
|
+
const arrayToActionRow = (componentsArray) => {
|
|
98
|
+
if (componentsArray.type === ComponentType.ActionRow)
|
|
99
|
+
return componentsArray;
|
|
100
|
+
|
|
101
|
+
return { type: ComponentType.ActionRow, components: componentsArray };
|
|
102
|
+
}
|
|
103
|
+
const actionRowInArray = (actionRow) => [actionRow];
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
if (argumentType <= 0)
|
|
107
|
+
data = inArray(data);
|
|
108
|
+
|
|
109
|
+
if (argumentType <= 1)
|
|
110
|
+
data = actionRowInArray(data)
|
|
111
|
+
|
|
112
|
+
data = data.map(arrayToActionRow);
|
|
113
|
+
return data;
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export {
|
|
118
|
+
CreateMessage,
|
|
119
|
+
CreateModal,
|
|
120
|
+
SimplifyComponents,
|
|
121
|
+
isEmptyEmbed
|
|
122
|
+
};
|
package/retype.yml
ADDED