1
1
const fs = require ( 'fs' )
2
+ const config = require ( './config' )
2
3
3
4
function ensureDatabase ( ) {
4
5
const exists = fs . existsSync ( './databases/tags.json' )
@@ -22,6 +23,17 @@ function getDatabase() {
22
23
return JSON . parse ( fs . readFileSync ( './databases/tags.json' ) )
23
24
}
24
25
26
+ function generateEmbed ( name , content , image ) {
27
+ return {
28
+ title : name ,
29
+ description : content ,
30
+ color : config . getColor ( 'accent' ) ,
31
+ image : {
32
+ url : image
33
+ }
34
+ }
35
+ }
36
+
25
37
module . exports = {
26
38
add : async ( name , content , image ) => {
27
39
ensureDatabase ( )
@@ -40,14 +52,15 @@ module.exports = {
40
52
delete database [ name ]
41
53
writeDatabase ( database )
42
54
} ,
43
- modify : ( name , content , image ) => {
55
+ modify : ( name , content , image , faqitem ) => {
44
56
ensureDatabase ( )
45
57
const database = getDatabase ( )
46
58
if ( ! database [ name ] ) database [ name ] = { }
47
59
const item = database [ name ]
48
60
item . name = name || item . name
49
61
item . content = content || item . content
50
62
item . image = image || item . image
63
+ item . faqitem = faqitem ?? item . faqitem
51
64
writeDatabase ( database )
52
65
} ,
53
66
get : name => {
@@ -57,5 +70,23 @@ module.exports = {
57
70
getAll : ( ) => {
58
71
ensureDatabase ( )
59
72
return getDatabase ( )
73
+ } ,
74
+ updateFAQList : async client => {
75
+ if ( ! config . get ( ) . channels . faq ) return
76
+ ensureDatabase ( )
77
+ const database = getDatabase ( )
78
+ const guild = await client . guilds . fetch ( config . get ( ) . guildId )
79
+ const channel = await guild . channels . fetch ( config . get ( ) . channels . faq )
80
+ const messages = await channel . messages . fetch ( { limit : 100 } )
81
+ const botMessages = messages . filter ( m => m . author . id === client . user . id ) . toJSON ( )
82
+ await channel . bulkDelete ( botMessages )
83
+
84
+ const embeds = Object . values ( database )
85
+ . filter ( t => t . faqitem === true )
86
+ . map ( ( { name, content, image } ) => generateEmbed ( name , content , image ) )
87
+ if ( embeds . length === 0 ) return
88
+ const chunks = [ ]
89
+ for ( let i = 0 ; i < embeds . length ; i += 10 ) chunks . push ( embeds . slice ( i , i + 10 ) )
90
+ chunks . forEach ( embeds => channel . send ( { embeds } ) )
60
91
}
61
92
}
0 commit comments