forked from stoatchat/javascript-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageEmbed.ts
More file actions
227 lines (202 loc) · 5.75 KB
/
Copy pathMessageEmbed.ts
File metadata and controls
227 lines (202 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import type { Embed, ImageSize, Special } from "stoat-api";
import type { Client } from "../Client.js";
import { File } from "./File.js";
/**
* Message Embed
*/
export abstract class MessageEmbed {
protected client?: Client;
readonly type: Embed["type"];
/**
* Construct Embed
* @param client Client
* @param type Type
*/
constructor(client?: Client, type: Embed["type"] = "None") {
this.client = client;
this.type = type;
}
/**
* Create an Embed from an API Embed
* @param client Client
* @param embed Data
* @returns Embed
*/
static from(client: Client, embed: Embed): MessageEmbed {
switch (embed.type) {
case "Image":
return new ImageEmbed(client, embed);
case "Video":
return new VideoEmbed(client, embed);
case "Website":
return new WebsiteEmbed(client, embed);
case "Text":
return new TextEmbed(client, embed);
default:
return new UnknownEmbed(client);
}
}
}
/**
* Embed of unknown type
*/
export class UnknownEmbed extends MessageEmbed {}
/**
* Image Embed
*/
export class ImageEmbed extends MessageEmbed {
readonly url: string;
readonly width: number;
readonly height: number;
readonly size: ImageSize;
/**
* Construct Image Embed
* @param client Client
* @param embed Embed
*/
constructor(client: Client, embed: Omit<Embed & { type: "Image" }, "type">) {
super(client, "Image");
this.url = embed.url;
this.width = embed.width;
this.height = embed.height;
this.size = embed.size;
}
/**
* Proxied image URL
*/
get proxiedURL(): string | undefined {
return this.client?.proxyFile(this.url);
}
}
/**
* Video Embed
*/
export class VideoEmbed extends MessageEmbed {
readonly url: string;
readonly width: number;
readonly height: number;
/**
* Construct Video Embed
* @param client Client
* @param embed Embed
*/
constructor(client: Client, embed: Omit<Embed & { type: "Video" }, "type">) {
super(client, "Video");
this.url = embed.url;
this.width = embed.width;
this.height = embed.height;
}
/**
* Proxied video URL
*/
get proxiedURL(): string | undefined {
return this.client?.proxyFile(this.url);
}
}
/**
* Website Embed
*/
export class WebsiteEmbed extends MessageEmbed {
readonly url?: string;
readonly originalUrl?: string;
readonly specialContent?: Special;
readonly title?: string;
readonly description?: string;
readonly image?: ImageEmbed;
readonly video?: VideoEmbed;
readonly siteName?: string;
readonly iconUrl?: string;
readonly colour?: string;
/**
* Construct Video Embed
* @param client Client
* @param embed Embed
*/
constructor(
client: Client,
embed: Omit<Embed & { type: "Website" }, "type">,
) {
super(client, "Website");
this.url = embed.url!;
this.originalUrl = embed.original_url!;
this.specialContent = embed.special!;
this.title = embed.title!;
this.description = embed.description!;
this.image = embed.image ? new ImageEmbed(client, embed.image) : undefined;
this.video = embed.video ? new VideoEmbed(client, embed.video) : undefined;
this.siteName = embed.site_name!;
this.iconUrl = embed.icon_url!;
this.colour = embed.colour!;
}
/**
* Proxied icon URL
*/
get proxiedIconURL(): string | undefined {
return this.iconUrl ? this.client?.proxyFile(this.iconUrl) : undefined;
}
/**
* If special content is present, generate the embed URL
*/
get embedURL(): string | undefined {
switch (this.specialContent?.type) {
case "YouTube": {
let timestamp = "";
if (this.specialContent.timestamp) {
timestamp = `&start=${this.specialContent.timestamp}`;
}
return `https://www.youtube-nocookie.com/embed/${this.specialContent.id}?modestbranding=1${timestamp}`;
}
case "Twitch":
return `https://player.twitch.tv/?${this.specialContent.content_type.toLowerCase()}=${
this.specialContent.id
}&parent=${(window ?? {})?.location?.hostname}&autoplay=false`;
case "Lightspeed":
return `https://new.lightspeed.tv/embed/${this.specialContent.id}/stream`;
case "Spotify":
return `https://open.spotify.com/embed/${this.specialContent.content_type}/${this.specialContent.id}`;
case "Soundcloud":
return `https://w.soundcloud.com/player/?url=${encodeURIComponent(
this.url!,
)}&color=%23FF7F50&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true`;
case "Bandcamp": {
return `https://bandcamp.com/EmbeddedPlayer/${this.specialContent.content_type.toLowerCase()}=${
this.specialContent.id
}/size=large/bgcol=181a1b/linkcol=056cc4/tracklist=false/transparent=true/`;
}
case "Streamable": {
return `https://streamable.com/e/${this.specialContent.id}?loop=0`;
}
}
}
}
/**
* Text Embed
*/
export class TextEmbed extends MessageEmbed {
readonly iconUrl?: string;
readonly url?: string;
readonly title?: string;
readonly description?: string;
readonly media?: File;
readonly colour?: string;
/**
* Construct Video Embed
* @param client Client
* @param embed Embed
*/
constructor(client: Client, embed: Omit<Embed & { type: "Text" }, "type">) {
super(client, "Text");
this.iconUrl = embed.icon_url!;
this.url = embed.url!;
this.title = embed.title!;
this.description = embed.description!;
this.media = embed.media ? new File(client, embed.media) : undefined;
this.colour = embed.colour!;
}
/**
* Proxied icon URL
*/
get proxiedIconURL(): string | undefined {
return this.iconUrl ? this.client?.proxyFile(this.iconUrl) : undefined;
}
}