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