rootspring revisó este gist . Ir a la revisión
1 file changed, 144 insertions
templater.ts(archivo creado)
@@ -0,0 +1,144 @@ | |||
1 | + | import { builderVersion, Embed, TemplateBuilderData, TemplateBuilderDataComment } from "./types"; | |
2 | + | ||
3 | + | export const parseString = (s: string): string => { | |
4 | + | // If it starts with $expr:, then it's a raw string | |
5 | + | if (s.startsWith("$expr")) { | |
6 | + | return s.substring(5).trim(); | |
7 | + | } | |
8 | + | ||
9 | + | return `"${s}"`; | |
10 | + | } | |
11 | + | ||
12 | + | const sha256 = async (message: string) => { | |
13 | + | // encode as UTF-8 | |
14 | + | const msgBuffer = new TextEncoder().encode(message); | |
15 | + | ||
16 | + | // hash the message | |
17 | + | const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer); | |
18 | + | ||
19 | + | // convert ArrayBuffer to Array | |
20 | + | const hashArray = Array.from(new Uint8Array(hashBuffer)); | |
21 | + | ||
22 | + | // convert bytes to hex string | |
23 | + | const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); | |
24 | + | return hashHex; | |
25 | + | } | |
26 | + | ||
27 | + | export const generateTemplateForTemplateBuilderData = async (tbd: TemplateBuilderData) => { | |
28 | + | let templateStr = ``; | |
29 | + | ||
30 | + | if (tbd.embeds.length > 0) { | |
31 | + | for (let embed of tbd.embeds) { | |
32 | + | let fragment = generateTemplateFragmentForEmbed(embed); | |
33 | + | ||
34 | + | if (fragment) { | |
35 | + | templateStr += `${fragment}table.insert(message, embed)\n\t`; | |
36 | + | } | |
37 | + | } | |
38 | + | } | |
39 | + | ||
40 | + | if (tbd.content) { | |
41 | + | templateStr += `message.content = ${parseString(tbd.content)}\n\t`; | |
42 | + | } | |
43 | + | ||
44 | + | if (templateStr) { | |
45 | + | templateStr = `function (args) { | |
46 | + | local message_plugin = require "@antiraid/message" | |
47 | + | local message = message_plugin.new_message() | |
48 | + | -- Create the message | |
49 | + | ${templateStr.trim()} | |
50 | + | }` | |
51 | + | ||
52 | + | // Get sha256 checksum of the template | |
53 | + | const checksum = await sha256(templateStr.trim()); | |
54 | + | ||
55 | + | let pragma: TemplatePragma = { | |
56 | + | lang: "lua", | |
57 | + | builderInfo: { | |
58 | + | ver: builderVersion, | |
59 | + | data: tbd, | |
60 | + | checksum | |
61 | + | } | |
62 | + | } | |
63 | + | ||
64 | + | templateStr = `@pragma ${JSON.stringify(pragma)}\n${templateStr}`; | |
65 | + | } | |
66 | + | ||
67 | + | return templateStr; | |
68 | + | } | |
69 | + | ||
70 | + | export interface ParsedTemplateBuilderComment { | |
71 | + | comment: TemplateBuilderDataComment; | |
72 | + | checksumOk: boolean; | |
73 | + | template: string; | |
74 | + | } | |
75 | + | ||
76 | + | export interface TemplatePragma { | |
77 | + | lang: string, | |
78 | + | builderInfo?: TemplateBuilderDataComment // Website specific field | |
79 | + | } | |
80 | + | ||
81 | + | export const parseTemplateBuilderDataCommentFromTemplate = async (template: string): Promise<ParsedTemplateBuilderComment | null> => { | |
82 | + | let templateFirstLine = template.split('\n')[0]; | |
83 | + | template = template.slice(1); // Rest of template is the actual template | |
84 | + | ||
85 | + | if (!templateFirstLine.startsWith("@pragma ")) { | |
86 | + | return null; | |
87 | + | } | |
88 | + | ||
89 | + | let pragma = templateFirstLine.substring(8).trim(); | |
90 | + | ||
91 | + | try { | |
92 | + | let pragmaObj: TemplatePragma = JSON.parse(pragma); | |
93 | + | ||
94 | + | if (!pragmaObj.builderInfo) { | |
95 | + | return null; | |
96 | + | } | |
97 | + | ||
98 | + | // Check if the checksum is correct | |
99 | + | let checksumOk = await sha256(template) == pragmaObj.builderInfo.checksum; | |
100 | + | ||
101 | + | return { | |
102 | + | comment: pragmaObj.builderInfo, | |
103 | + | checksumOk, | |
104 | + | template | |
105 | + | } | |
106 | + | } catch { | |
107 | + | return null; | |
108 | + | } | |
109 | + | } | |
110 | + | ||
111 | + | export const generateTemplateFragmentForEmbed = (embed: Embed) => { | |
112 | + | let baseFragment = ''; | |
113 | + | ||
114 | + | if (embed.title) { | |
115 | + | baseFragment += `embed.title = ${parseString(embed.title)}\n\t`; | |
116 | + | } | |
117 | + | ||
118 | + | if (embed.description) { | |
119 | + | baseFragment += `embed.description = ${parseString(embed.description)}\n\t`; | |
120 | + | } | |
121 | + | ||
122 | + | if (embed.fields.length > 0) { | |
123 | + | embed.fields.forEach((field, i) => { | |
124 | + | if (field.name == "" || field.value == "") { | |
125 | + | return | |
126 | + | } | |
127 | + | ||
128 | + | baseFragment += ` | |
129 | + | -- Field ${i + 1} | |
130 | + | local field = message_plugin.new_message_embed_field()\n | |
131 | + | field.name = ${parseString(field.name)}\n | |
132 | + | field.value = ${parseString(field.value)}\n | |
133 | + | field.inline = ${field.inline}\n | |
134 | + | table.insert(embed.fields, field)\n | |
135 | + | `; | |
136 | + | }); | |
137 | + | } | |
138 | + | ||
139 | + | if (baseFragment) { | |
140 | + | return `local embed = message_plugin.new_message_embed()\n\t${baseFragment}` | |
141 | + | } | |
142 | + | ||
143 | + | return ''; | |
144 | + | }; |
Siguiente
Anterior