1 |
|
import { APIEmbed } from "discord-api-types/v9"; |
2 |
|
import { MessageEmbedOptions } from "discord.js"; |
3 |
import { readFile, writeFile } from "fs"; |
import { readFile, writeFile } from "fs"; |
4 |
import path from "path"; |
import path from "path"; |
5 |
import DiscordClient from "../client/Client"; |
import DiscordClient from "../client/Client"; |
6 |
import { deleteFile } from "../utils/util"; |
import MessageEmbed from "../client/MessageEmbed"; |
7 |
|
import Service from "../utils/structures/Service"; |
8 |
|
import { deleteFile, parseEmbedsInString } from "../utils/util"; |
9 |
|
|
10 |
export type Snippet = { |
export type Snippet = { |
11 |
name: string; |
name: string; |
12 |
content: string; |
content: string; |
13 |
files: string[] |
files: string[], |
14 |
|
embeds?: MessageEmbed[] |
15 |
}; |
}; |
16 |
|
|
17 |
export type SnippetContainer = { |
export type SnippetContainer = { |
18 |
[guildID: string]: Snippet[]; |
[guildID: string]: Snippet[]; |
19 |
}; |
}; |
20 |
|
|
21 |
export default class SnippetManager { |
export default class SnippetManager extends Service { |
22 |
snippets: SnippetContainer = {}; |
snippets: SnippetContainer = {}; |
|
client: DiscordClient; |
|
23 |
|
|
24 |
constructor(client: DiscordClient) { |
constructor(client: DiscordClient) { |
25 |
this.client = client; |
super(client); |
26 |
this.load(); |
this.load(); |
27 |
} |
} |
28 |
|
|
41 |
} |
} |
42 |
|
|
43 |
set(guildID: string, name: string, content: string, files: string[] = []): void { |
set(guildID: string, name: string, content: string, files: string[] = []): void { |
44 |
|
if (!this.snippets[guildID]) { |
45 |
|
this.snippets[guildID] = []; |
46 |
|
} |
47 |
|
|
48 |
this.snippets[guildID].push({ |
this.snippets[guildID].push({ |
49 |
name, |
name, |
50 |
content, |
content, |
53 |
} |
} |
54 |
|
|
55 |
get(guildID: string, name: string): Snippet | null { |
get(guildID: string, name: string): Snippet | null { |
56 |
|
if (!this.snippets[guildID]) { |
57 |
|
return null; |
58 |
|
} |
59 |
|
|
60 |
for (const s of this.snippets[guildID]) { |
for (const s of this.snippets[guildID]) { |
61 |
if (s.name === name) { |
if (s.name === name) { |
62 |
return s; |
return s; |
65 |
|
|
66 |
return null; |
return null; |
67 |
} |
} |
68 |
|
|
69 |
|
getParsed(guildID: string, name: string): Snippet | null { |
70 |
|
const snippet = this.get(guildID, name); |
71 |
|
|
72 |
|
if (!snippet) { |
73 |
|
return null; |
74 |
|
} |
75 |
|
|
76 |
|
return this.parseEmbeds(snippet) ?? snippet; |
77 |
|
} |
78 |
|
|
79 |
|
parseEmbeds(snippet: Snippet) { |
80 |
|
try { |
81 |
|
const { embeds, content } = parseEmbedsInString(snippet.content); |
82 |
|
|
83 |
|
console.log(content); |
84 |
|
|
85 |
|
return <Snippet> { |
86 |
|
...snippet, |
87 |
|
content, |
88 |
|
embeds, |
89 |
|
}; |
90 |
|
} |
91 |
|
catch (e) { |
92 |
|
console.log(e); |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
async delete(guildID: string, name: string): Promise<void> { |
async delete(guildID: string, name: string): Promise<void> { |
97 |
for (const i in this.snippets[guildID]) { |
for (const i in this.snippets[guildID]) { |