1 |
rakin |
51 |
import { readFile, writeFile } from "fs"; |
2 |
|
|
import path from "path"; |
3 |
|
|
import DiscordClient from "../client/Client"; |
4 |
rakin |
255 |
import MessageEmbed from "../client/MessageEmbed"; |
5 |
rakin |
226 |
import Service from "../utils/structures/Service"; |
6 |
rakin |
256 |
import { deleteFile, parseEmbedsInString } from "../utils/util"; |
7 |
rakin |
51 |
|
8 |
|
|
export type Snippet = { |
9 |
|
|
name: string; |
10 |
|
|
content: string; |
11 |
rakin |
255 |
files: string[], |
12 |
|
|
embeds?: MessageEmbed[] |
13 |
rakin |
51 |
}; |
14 |
|
|
|
15 |
|
|
export type SnippetContainer = { |
16 |
|
|
[guildID: string]: Snippet[]; |
17 |
|
|
}; |
18 |
|
|
|
19 |
rakin |
226 |
export default class SnippetManager extends Service { |
20 |
rakin |
51 |
snippets: SnippetContainer = {}; |
21 |
|
|
|
22 |
|
|
constructor(client: DiscordClient) { |
23 |
rakin |
226 |
super(client); |
24 |
rakin |
51 |
this.load(); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
load() { |
28 |
|
|
readFile(path.resolve(__dirname, '../..', 'config/snippets.json'), (err, data) => { |
29 |
|
|
if (err) { |
30 |
|
|
console.log(err); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
this.snippets = JSON.parse(data.toString()); |
34 |
|
|
}); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
write() { |
38 |
|
|
writeFile(path.resolve(__dirname, '../..', 'config/snippets.json'), JSON.stringify(this.snippets), () => null); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
set(guildID: string, name: string, content: string, files: string[] = []): void { |
42 |
|
|
this.snippets[guildID].push({ |
43 |
|
|
name, |
44 |
|
|
content, |
45 |
|
|
files |
46 |
|
|
}); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
get(guildID: string, name: string): Snippet | null { |
50 |
rakin |
244 |
if (!this.snippets[guildID]) { |
51 |
|
|
return null; |
52 |
|
|
} |
53 |
|
|
|
54 |
rakin |
51 |
for (const s of this.snippets[guildID]) { |
55 |
|
|
if (s.name === name) { |
56 |
|
|
return s; |
57 |
|
|
} |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
return null; |
61 |
|
|
} |
62 |
rakin |
255 |
|
63 |
|
|
getParsed(guildID: string, name: string): Snippet | null { |
64 |
|
|
const snippet = this.get(guildID, name); |
65 |
rakin |
51 |
|
66 |
rakin |
255 |
if (!snippet) { |
67 |
|
|
return null; |
68 |
|
|
} |
69 |
|
|
|
70 |
rakin |
256 |
return this.parseEmbeds(snippet) ?? snippet; |
71 |
rakin |
255 |
} |
72 |
|
|
|
73 |
rakin |
256 |
parseEmbeds(snippet: Snippet) { |
74 |
|
|
try { |
75 |
|
|
const { embeds, content } = parseEmbedsInString(snippet.content); |
76 |
rakin |
255 |
|
77 |
rakin |
256 |
console.log(content); |
78 |
rakin |
255 |
|
79 |
rakin |
256 |
return <Snippet> { |
80 |
|
|
...snippet, |
81 |
|
|
content, |
82 |
|
|
embeds, |
83 |
|
|
}; |
84 |
rakin |
255 |
} |
85 |
rakin |
256 |
catch (e) { |
86 |
|
|
console.log(e); |
87 |
|
|
} |
88 |
rakin |
255 |
} |
89 |
|
|
|
90 |
rakin |
51 |
async delete(guildID: string, name: string): Promise<void> { |
91 |
|
|
for (const i in this.snippets[guildID]) { |
92 |
|
|
if (this.snippets[guildID][i].name === name) { |
93 |
|
|
for await (const file of this.snippets[guildID][i].files) { |
94 |
|
|
try { |
95 |
|
|
await deleteFile(path.resolve(__dirname, '../../storage', file)); |
96 |
|
|
} |
97 |
|
|
catch (e) { |
98 |
|
|
console.log(e); |
99 |
|
|
} |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
await this.snippets[guildID].splice(parseInt(i), 1); |
103 |
|
|
|
104 |
|
|
return; |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
async deleteData(guildID: string, name: string): Promise<void> { |
110 |
|
|
for (const i in this.snippets[guildID]) { |
111 |
|
|
if (this.snippets[guildID][i].name === name) { |
112 |
|
|
await this.snippets[guildID].splice(parseInt(i), 1); |
113 |
|
|
return; |
114 |
|
|
} |
115 |
|
|
} |
116 |
|
|
} |
117 |
rakin |
283 |
} |