/[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 51 by rakin, Mon Jul 29 17:28:23 2024 UTC revision 283 by rakin, Mon Jul 29 17:29:21 2024 UTC
# Line 1  Line 1 
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 { deleteFile } from "../utils/util";  import MessageEmbed from "../client/MessageEmbed";
7    import Service from "../utils/structures/Service";
8    import { deleteFile, parseEmbedsInString } 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 = {
18      [guildID: string]: Snippet[];      [guildID: string]: Snippet[];
19  };  };
20    
21  export default class SnippetManager {  export default class SnippetManager extends Service {
22      snippets: SnippetContainer = {};      snippets: SnippetContainer = {};
     client: DiscordClient;  
23    
24      constructor(client: DiscordClient) {      constructor(client: DiscordClient) {
25          this.client = client;          super(client);
26          this.load();          this.load();
27      }      }
28    
# Line 45  export default class SnippetManager { Line 49  export default class SnippetManager {
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 { Line 61  export default class SnippetManager {
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]) {
# Line 81  export default class SnippetManager { Line 116  export default class SnippetManager {
116              }              }
117          }          }
118      }      }
 }  
119    }

Legend:
Removed from v.51  
changed lines
  Added in v.283

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26