/[sudobot]/trunk/src/services/SnippetManager.ts
ViewVC logotype

Diff of /trunk/src/services/SnippetManager.ts

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 226 by rakin, Mon Jul 29 17:29:06 2024 UTC revision 360 by rakin, Mon Jul 29 17:29:46 2024 UTC
# Line 1  Line 1 
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";
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;
10      content: string;      content: string;
11      files: string[]      files: string[],
12        embeds?: MessageEmbed[]
13  };  };
14    
15  export type SnippetContainer = {  export type SnippetContainer = {
# Line 16  export type SnippetContainer = { Line 18  export type SnippetContainer = {
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              }              }
# Line 33  export default class SnippetManager exte Line 37  export default class SnippetManager exte
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 {
# Line 45  export default class SnippetManager exte Line 49  export default class SnippetManager exte
49      }      }
50    
51      get(guildID: string, name: string): Snippet | null {      get(guildID: string, name: string): Snippet | null {
52            if (!this.snippets[guildID]) {
53                return null;
54            }
55            
56          for (const s of this.snippets[guildID]) {          for (const s of this.snippets[guildID]) {
57              if (s.name === name) {              if (s.name === name) {
58                  return s;                  return s;
# Line 53  export default class SnippetManager exte Line 61  export default class SnippetManager exte
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            return this.parseEmbeds(snippet) ?? snippet;
73        }
74    
75        parseEmbeds(snippet: Snippet) {        
76            try {
77                const { embeds, content } = parseEmbedsInString(snippet.content);
78    
79                console.log(content);            
80    
81                return <Snippet> {
82                    ...snippet,
83                    content,
84                    embeds,
85                };
86            }
87            catch (e) {
88                console.log(e);
89            }
90        }
91    
92      async delete(guildID: string, name: string): Promise<void> {      async delete(guildID: string, name: string): Promise<void> {
93          for (const i in this.snippets[guildID]) {          for (const i in this.snippets[guildID]) {
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);                
# Line 81  export default class SnippetManager exte Line 116  export default class SnippetManager exte
116              }              }
117          }          }
118      }      }
 }  
119    }

Legend:
Removed from v.226  
changed lines
  Added in v.360

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26