1 |
|
const { default: axios } = require('axios'); |
2 |
const { readFile, writeFile } = require('fs'); |
const { readFile, writeFile } = require('fs'); |
3 |
|
const path = require('path'); |
4 |
|
const fs = require('fs'); |
5 |
|
const { download } = require('../commands/cat'); |
6 |
|
|
7 |
class SnippetManager { |
class SnippetManager { |
8 |
constructor() { |
constructor() { |
20 |
}); |
}); |
21 |
} |
} |
22 |
|
|
23 |
find(name) { |
find(guild, name) { |
24 |
return this.snippets.find(s => s.name === name); |
return this.snippets[guild].find(s => s.name === name); |
25 |
} |
} |
26 |
|
|
27 |
update() { |
update() { |
28 |
writeFile("config/snippets.json", JSON.stringify(this.snippets), () => {}); |
writeFile("config/snippets.json", JSON.stringify(this.snippets), () => {}); |
29 |
} |
} |
30 |
|
|
31 |
create(name, content) { |
async create(guild, name, content, files) { |
32 |
if (this.find(name) !== undefined) |
if (this.find(guild, name) !== undefined) |
33 |
return false; |
return false; |
34 |
|
|
35 |
this.snippets.push({name, content}); |
const fileList = []; |
36 |
this.update(); |
|
37 |
|
for (const i in files) { |
38 |
|
fileList[i] = Math.round((Math.random() * 10000000)) + "_" + files[i].name; |
39 |
|
await download(files[i].proxyURL, path.join(__dirname, '..', 'storage', fileList[i])); |
40 |
|
} |
41 |
|
|
42 |
|
await this.snippets[guild].push({name, content, files: fileList}); |
43 |
|
await this.update(); |
44 |
|
|
45 |
return true; |
return true; |
46 |
} |
} |
47 |
|
|
48 |
delete(name) { |
delete(guild, name) { |
49 |
for (let i in this.snippets) { |
for (let i in this.snippets[guild]) { |
50 |
if (this.snippets[i].name === name) { |
if (this.snippets[guild][i].name === name) { |
51 |
this.snippets.splice(i, 1); |
|
52 |
|
this.snippets[guild][i].files?.forEach(f => { |
53 |
|
fs.rmSync(path.resolve(__dirname, '..', 'storage', f)); |
54 |
|
}); |
55 |
|
|
56 |
|
this.snippets[guild].splice(i, 1); |
57 |
this.update(); |
this.update(); |
58 |
return true; |
return true; |
59 |
} |
} |