@gryt/core 0.26.0 → 0.27.0
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.
|
@@ -32,5 +32,7 @@ export declare function parseDiscordTime(v: unknown): number | null;
|
|
|
32
32
|
export declare function listDiscordDms(files: PackageFiles): Promise<DiscordPackageDm[]>;
|
|
33
33
|
/** One DM's messages, oldest first. */
|
|
34
34
|
export declare function readDiscordDm(files: PackageFiles, channelId: string): Promise<DiscordPackageMessage[]>;
|
|
35
|
+
/** Discord's own markup as text a person can read. A custom emoji can't come over, so it keeps its name. */
|
|
36
|
+
export declare function readableDiscordText(text: string, people: ReadonlyMap<string, string>, channels: Readonly<Record<string, unknown>>): string;
|
|
35
37
|
/** The earliest `ex` (hex Unix seconds) among signed CDN links, or null when none carry one. */
|
|
36
38
|
export declare function linksExpireAt(urls: string[]): number | null;
|
|
@@ -189,7 +189,41 @@ export async function listDiscordDms(files) {
|
|
|
189
189
|
export async function readDiscordDm(files, channelId) {
|
|
190
190
|
const idx = indexNames(files.names);
|
|
191
191
|
const dir = channelFolder(idx, channelId);
|
|
192
|
-
|
|
192
|
+
if (!dir)
|
|
193
|
+
return [];
|
|
194
|
+
const names = await peopleIn(files, idx, channelId);
|
|
195
|
+
const channels = ((await readJson(files, idx, "messages/index.json")) ?? {});
|
|
196
|
+
return (await messagesIn(files, idx, dir)).map((m) => ({ ...m, text: readableDiscordText(m.text, names, channels) }));
|
|
197
|
+
}
|
|
198
|
+
async function peopleIn(files, idx, channelId) {
|
|
199
|
+
const names = new Map();
|
|
200
|
+
const user = (await readJson(files, idx, "account/user.json"));
|
|
201
|
+
const add = (u) => {
|
|
202
|
+
const name = u?.global_name || u?.username;
|
|
203
|
+
if (typeof u?.id === "string" && typeof name === "string")
|
|
204
|
+
names.set(u.id, name);
|
|
205
|
+
};
|
|
206
|
+
add(user);
|
|
207
|
+
for (const rel of Array.isArray(user?.relationships) ? user.relationships : [])
|
|
208
|
+
add(rel?.user);
|
|
209
|
+
const channel = (await readJson(files, idx, `messages/c${channelId}/channel.json`));
|
|
210
|
+
const label = (await readJson(files, idx, "messages/index.json"))?.[channelId];
|
|
211
|
+
const peer = Array.isArray(channel?.recipients) ? channel.recipients.find((id) => id !== user?.id) : undefined;
|
|
212
|
+
if (typeof peer === "string" && typeof label === "string" && !names.has(peer))
|
|
213
|
+
names.set(peer, displayName(label));
|
|
214
|
+
return names;
|
|
215
|
+
}
|
|
216
|
+
/** Discord's own markup as text a person can read. A custom emoji can't come over, so it keeps its name. */
|
|
217
|
+
export function readableDiscordText(text, people, channels) {
|
|
218
|
+
return text
|
|
219
|
+
.replace(/<a?:(\w+):\d+>/g, ":$1:")
|
|
220
|
+
.replace(/<@!?(\d+)>/g, (_, id) => `@${people.get(id) ?? "someone"}`)
|
|
221
|
+
.replace(/<@&\d+>/g, "@role")
|
|
222
|
+
.replace(/<#(\d+)>/g, (_, id) => {
|
|
223
|
+
const label = channels[id];
|
|
224
|
+
const name = typeof label === "string" && !/^unknown/i.test(label) ? label.split(",")[0].trim() : "channel";
|
|
225
|
+
return `#${name}`;
|
|
226
|
+
});
|
|
193
227
|
}
|
|
194
228
|
/** The earliest `ex` (hex Unix seconds) among signed CDN links, or null when none carry one. */
|
|
195
229
|
export function linksExpireAt(urls) {
|
package/package.json
CHANGED