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 MessageEmbed from "../client/MessageEmbed"; |
7 |
import Service from "../utils/structures/Service"; |
import Service from "../utils/structures/Service"; |
8 |
import { deleteFile } from "../utils/util"; |
import { deleteFile } 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 = { |
61 |
|
|
62 |
return null; |
return null; |
63 |
} |
} |
64 |
|
|
65 |
|
getParsed(guildID: string, name: string): Snippet | null { |
66 |
|
const snippet = this.get(guildID, name); |
67 |
|
|
68 |
|
if (!snippet) { |
69 |
|
return null; |
70 |
|
} |
71 |
|
|
72 |
|
snippet.embeds = this.parseEmbeds(snippet).embeds; |
73 |
|
|
74 |
|
return snippet; |
75 |
|
} |
76 |
|
|
77 |
|
parseEmbeds(snippet: Snippet) {; |
78 |
|
const embedExpressions = snippet.content.matchAll(/embed\:(\{[^\n]+\})/g); |
79 |
|
snippet.content = snippet.content.replace(/embed\:(\{[^\n]+\})/g, ''); |
80 |
|
let embeds: typeof snippet.embeds = []; |
81 |
|
|
82 |
|
for (const expr of [...embedExpressions]) { |
83 |
|
const parsed = JSON.parse(expr[1]); |
84 |
|
|
85 |
|
try { |
86 |
|
embeds.push(new MessageEmbed(parsed).setColor(parsed.color)); |
87 |
|
} |
88 |
|
catch (e) { |
89 |
|
console.log(e); |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
|
snippet.embeds = embeds; |
94 |
|
|
95 |
|
return snippet; |
96 |
|
} |
97 |
|
|
98 |
async delete(guildID: string, name: string): Promise<void> { |
async delete(guildID: string, name: string): Promise<void> { |
99 |
for (const i in this.snippets[guildID]) { |
for (const i in this.snippets[guildID]) { |