1 |
rakinar2 |
577 |
import { APIEmbed } from "discord-api-types/v9"; |
2 |
|
|
import { MessageEmbedOptions } from "discord.js"; |
3 |
|
|
import { readFile, writeFile } from "fs"; |
4 |
|
|
import path from "path"; |
5 |
|
|
import DiscordClient from "../client/Client"; |
6 |
|
|
import MessageEmbed from "../client/MessageEmbed"; |
7 |
|
|
import Service from "../utils/structures/Service"; |
8 |
|
|
import { deleteFile, parseEmbedsInString } from "../utils/util"; |
9 |
|
|
|
10 |
|
|
export type Snippet = { |
11 |
|
|
name: string; |
12 |
|
|
content: string; |
13 |
|
|
files: string[], |
14 |
|
|
embeds?: MessageEmbed[] |
15 |
|
|
}; |
16 |
|
|
|
17 |
|
|
export type SnippetContainer = { |
18 |
|
|
[guildID: string]: Snippet[]; |
19 |
|
|
}; |
20 |
|
|
|
21 |
|
|
export default class SnippetManager extends Service { |
22 |
|
|
snippets: SnippetContainer = {}; |
23 |
|
|
|
24 |
|
|
constructor(client: DiscordClient) { |
25 |
|
|
super(client); |
26 |
|
|
this.load(); |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
load() { |
30 |
|
|
readFile(path.resolve(__dirname, '../..', 'config/snippets.json'), (err, data) => { |
31 |
|
|
if (err) { |
32 |
|
|
console.log(err); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
this.snippets = JSON.parse(data.toString()); |
36 |
|
|
}); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
write() { |
40 |
|
|
writeFile(path.resolve(__dirname, '../..', 'config/snippets.json'), JSON.stringify(this.snippets), () => null); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
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({ |
49 |
|
|
name, |
50 |
|
|
content, |
51 |
|
|
files |
52 |
|
|
}); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
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]) { |
61 |
|
|
if (s.name === name) { |
62 |
|
|
return s; |
63 |
|
|
} |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
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> { |
97 |
|
|
for (const i in this.snippets[guildID]) { |
98 |
|
|
if (this.snippets[guildID][i].name === name) { |
99 |
|
|
for await (const file of this.snippets[guildID][i].files) { |
100 |
|
|
try { |
101 |
|
|
await deleteFile(path.resolve(__dirname, '../../storage', file)); |
102 |
|
|
} |
103 |
|
|
catch (e) { |
104 |
|
|
console.log(e); |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
await this.snippets[guildID].splice(parseInt(i), 1); |
109 |
|
|
|
110 |
|
|
return; |
111 |
|
|
} |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
async deleteData(guildID: string, name: string): Promise<void> { |
116 |
|
|
for (const i in this.snippets[guildID]) { |
117 |
|
|
if (this.snippets[guildID][i].name === name) { |
118 |
|
|
await this.snippets[guildID].splice(parseInt(i), 1); |
119 |
|
|
return; |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
} |
123 |
|
|
} |