/[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 256 by rakin, Mon Jul 29 17:29:14 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 37  export default class SnippetManager { Line 41  export default class SnippetManager {
41      }      }
42    
43      set(guildID: string, name: string, content: string, files: string[] = []): void {      set(guildID: string, name: string, content: string, files: string[] = []): void {
44            if (!this.snippets[guildID]) {
45                this.snippets[guildID] = [];
46            }
47    
48          this.snippets[guildID].push({          this.snippets[guildID].push({
49              name,              name,
50              content,              content,
# Line 45  export default class SnippetManager { Line 53  export default class SnippetManager {
53      }      }
54    
55      get(guildID: string, name: string): Snippet | null {      get(guildID: string, name: string): Snippet | null {
56            if (!this.snippets[guildID]) {
57                return null;
58            }
59            
60          for (const s of this.snippets[guildID]) {          for (const s of this.snippets[guildID]) {
61              if (s.name === name) {              if (s.name === name) {
62                  return s;                  return s;
# Line 53  export default class SnippetManager { Line 65  export default class SnippetManager {
65    
66          return null;          return null;
67      }      }
68        
69        getParsed(guildID: string, name: string): Snippet | null {
70            const snippet = this.get(guildID, name);
71    
72            if (!snippet) {
73                return null;
74            }
75    
76            return this.parseEmbeds(snippet) ?? snippet;
77        }
78    
79        parseEmbeds(snippet: Snippet) {        
80            try {
81                const { embeds, content } = parseEmbedsInString(snippet.content);
82    
83                console.log(content);            
84    
85                return <Snippet> {
86                    ...snippet,
87                    content,
88                    embeds,
89                };
90            }
91            catch (e) {
92                console.log(e);
93            }
94        }
95    
96      async delete(guildID: string, name: string): Promise<void> {      async delete(guildID: string, name: string): Promise<void> {
97          for (const i in this.snippets[guildID]) {          for (const i in this.snippets[guildID]) {

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26