|
import { APIEmbed } from "discord-api-types/v9"; |
|
|
import { MessageEmbedOptions } from "discord.js"; |
|
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 MessageEmbed from "../client/MessageEmbed"; |
import MessageEmbed from "../client/MessageEmbed"; |
5 |
import Service from "../utils/structures/Service"; |
import Service from "../utils/structures/Service"; |
6 |
import { deleteFile } from "../utils/util"; |
import { deleteFile, parseEmbedsInString } from "../utils/util"; |
7 |
|
|
8 |
export type Snippet = { |
export type Snippet = { |
9 |
name: string; |
name: string; |
18 |
|
|
19 |
export default class SnippetManager extends Service { |
export default class SnippetManager extends Service { |
20 |
snippets: SnippetContainer = {}; |
snippets: SnippetContainer = {}; |
21 |
|
filePath: string; |
22 |
|
|
23 |
constructor(client: DiscordClient) { |
constructor(client: DiscordClient) { |
24 |
super(client); |
super(client); |
25 |
|
this.filePath = path.resolve(process.env.SUDO_PREFIX ?? path.join(__dirname, '../..'), 'config/snippets.json'); |
26 |
this.load(); |
this.load(); |
27 |
} |
} |
28 |
|
|
29 |
load() { |
load() { |
30 |
readFile(path.resolve(__dirname, '../..', 'config/snippets.json'), (err, data) => { |
readFile(this.filePath, (err, data) => { |
31 |
if (err) { |
if (err) { |
32 |
console.log(err); |
console.log(err); |
33 |
} |
} |
37 |
} |
} |
38 |
|
|
39 |
write() { |
write() { |
40 |
writeFile(path.resolve(__dirname, '../..', 'config/snippets.json'), JSON.stringify(this.snippets), () => null); |
writeFile(this.filePath, JSON.stringify(this.snippets), () => null); |
41 |
} |
} |
42 |
|
|
43 |
set(guildID: string, name: string, content: string, files: string[] = []): void { |
set(guildID: string, name: string, content: string, files: string[] = []): void { |
69 |
return null; |
return null; |
70 |
} |
} |
71 |
|
|
72 |
snippet.embeds = this.parseEmbeds(snippet).embeds; |
return this.parseEmbeds(snippet) ?? snippet; |
|
|
|
|
return snippet; |
|
73 |
} |
} |
74 |
|
|
75 |
parseEmbeds(snippet: Snippet) {; |
parseEmbeds(snippet: Snippet) { |
76 |
const embedExpressions = snippet.content.matchAll(/embed\:(\{[^\n]+\})/g); |
try { |
77 |
snippet.content = snippet.content.replace(/embed\:(\{[^\n]+\})/g, ''); |
const { embeds, content } = parseEmbedsInString(snippet.content); |
|
let embeds: typeof snippet.embeds = []; |
|
78 |
|
|
79 |
for (const expr of [...embedExpressions]) { |
console.log(content); |
|
const parsed = JSON.parse(expr[1]); |
|
80 |
|
|
81 |
try { |
return <Snippet> { |
82 |
embeds.push(new MessageEmbed(parsed).setColor(parsed.color)); |
...snippet, |
83 |
} |
content, |
84 |
catch (e) { |
embeds, |
85 |
console.log(e); |
}; |
86 |
} |
} |
87 |
|
catch (e) { |
88 |
|
console.log(e); |
89 |
} |
} |
|
|
|
|
snippet.embeds = embeds; |
|
|
|
|
|
return snippet; |
|
90 |
} |
} |
91 |
|
|
92 |
async delete(guildID: string, name: string): Promise<void> { |
async delete(guildID: string, name: string): Promise<void> { |
94 |
if (this.snippets[guildID][i].name === name) { |
if (this.snippets[guildID][i].name === name) { |
95 |
for await (const file of this.snippets[guildID][i].files) { |
for await (const file of this.snippets[guildID][i].files) { |
96 |
try { |
try { |
97 |
await deleteFile(path.resolve(__dirname, '../../storage', file)); |
await deleteFile(path.resolve(process.env.SUDO_PREFIX ?? path.join(__dirname, '../..'), 'storage', file)); |
98 |
} |
} |
99 |
catch (e) { |
catch (e) { |
100 |
console.log(e); |
console.log(e); |
116 |
} |
} |
117 |
} |
} |
118 |
} |
} |
|
} |
|
119 |
|
} |