11const Discord = require ( 'discord.js' ) ;
22const bot = new Discord . Client ( ) ;
33const request = require ( 'request' ) ;
4- const fs = require ( 'fs-extra-promise' ) ;
54const config = require ( './config.json' ) ;
6- const dbfile = './db.json' ;
75
8- let trulyReady = false ;
9- let db ;
6+ const sql = require ( 'sqlite' ) ;
7+ sql . open ( './db.sqlite' ) ;
108
11- try {
12- db = require ( dbfile ) ;
13- } catch ( err ) {
14- db = { } ;
15- }
9+ let trulyReady = false ;
1610
1711bot . once ( 'ready' , ( ) => {
1812 bot . guilds . forEach ( guild => {
19- if ( ! db . hasOwnProperty ( guild . id ) ) db [ guild . id ] = { } ;
20- guild . settings = db [ guild . id ] ;
13+ sql . get ( `SELECT * FROM guilds WHERE id="${ guild . id } "` ) . then ( row => {
14+ if ( ! row ) {
15+ sql . run ( 'INSERT INTO guilds (id) VALUES (?)' , [ guild . id ] ) ;
16+ }
17+ } ) . catch ( ( ) => {
18+ sql . run ( 'CREATE TABLE IF NOT EXISTS guilds (id TEXT, mainWiki TEXT, broadcastChannel TEXT)' ) . then ( ( ) => {
19+ sql . run ( 'CREATE TABLE IF NOT EXISTS overrides (guildID TEXT, channelID TEXT, wiki TEXT)' ) . then ( ( ) => {
20+ sql . run ( 'INSERT INTO guilds (id) VALUES (?)' , [ guild . id ] ) ;
21+ } ) ;
22+ } ) ;
23+ } ) ;
2124 } ) ;
22- saveDB ( ) . then ( ( ) => {
23- trulyReady = true ;
24- console . log ( `Ready: serving ${ bot . guilds . size } guilds, in ${ bot . channels . size } channels, for ${ bot . users . size } users.` ) ;
25- } ) . catch ( console . error ) ;
25+ trulyReady = true ;
26+ console . log ( `Ready: serving ${ bot . guilds . size } guilds, in ${ bot . channels . size } channels, for ${ bot . users . size } users.` ) ;
2627} ) ;
2728
2829bot . on ( 'guildCreate' , guild => {
29- if ( ! db . hasOwnProperty ( guild . id ) ) db [ guild . id ] = { } ;
30- guild . settings = db [ guild . id ] ;
31- saveDB ( ) . then ( ( ) => {
32- console . log ( `New Guild: ${ guild . name } ` ) ;
33- } ) . catch ( console . error ) ;
30+ sql . get ( `SELECT * FROM guilds WHERE id="${ guild . id } "` ) . then ( row => {
31+ if ( ! row ) {
32+ sql . run ( 'INSERT INTO guilds (id) VALUES (?)' , [ guild . id ] ) ;
33+ }
34+ } ) . catch ( ( ) => {
35+ sql . run ( 'CREATE TABLE IF NOT EXISTS guilds (id TEXT, mainWiki TEXT, broadcastChannel TEXT)' ) . then ( ( ) => {
36+ sql . run ( 'INSERT INTO guilds (id) VALUES (?)' , [ guild . id ] ) ;
37+ } ) ;
38+ } ) ;
3439} ) ;
3540
3641bot . on ( 'message' , ( msg ) => {
@@ -41,59 +46,68 @@ bot.on('message', (msg) => {
4146 const command = args . shift ( ) ;
4247 if ( commands . hasOwnProperty ( command ) ) commands [ command ] ( msg , args ) ;
4348 } else if ( / \[ \[ ( [ ^ \] | ] + ) (?: | [ ^ \] ] + ) ? \] \] / g. test ( msg . cleanContent ) || / \{ \{ ( [ ^ } | ] + ) (?: | [ ^ } ] + ) ? \} \} / g. test ( msg . cleanContent ) || / - - ( [ ^ \- | ] + ) (?: | [ ^ - ] + ) ? - - / g. test ( msg . cleanContent ) ) {
44- if ( ! msg . guild . settings . wiki ) {
45- // eslint-disable-next-line consistent-return
46- return msg . channel . send ( [
47- 'This server has not set a default wiki yet.' ,
48- 'Users with the "Administrator" permission can do this using wl~swiki <wikiname>.'
49- ] ) ;
50- } else if ( ! msg . guild . settings . broadcastChannel ) {
51- // eslint-disable-next-line consistent-return
52- return msg . channel . send ( [
53- 'This server has not set a broadcast channel yet.' ,
54- 'Users with the "Administrator" permission can do this using wl~broadcastchan <channel mention>.'
55- ] ) ;
56- }
49+ // eslint-disable-next-line consistent-return
50+ sql . get ( `SELECT * FROM guilds WHERE id="${ msg . guild . id } "` ) . then ( row => {
51+ if ( ! row . mainWiki ) {
52+ return msg . channel . send ( [
53+ 'This server has not set a default wiki yet.' ,
54+ 'Users with the "Administrator" permission can do this using wl~swiki <wikiname>.'
55+ ] ) ;
56+ } else if ( ! row . broadcastChannel ) {
57+ return msg . channel . send ( [
58+ 'This server has not set a default wiki yet.' ,
59+ 'Users with the "Administrator" permission can do this using wl~swiki <wikiname>.'
60+ ] ) ;
61+ }
62+ } ) ;
5763
58- let wiki = msg . guild . settings . wiki ;
59- if ( msg . guild . settings . channelOverrides ) wiki = msg . guild . settings . channelOverrides [ msg . channel . id ] || msg . guild . settings . wiki ;
64+ sql . get ( `SELECT mainWiki FROM guilds WHERE id=" ${ msg . guild . id } "` ) . then ( row => {
65+ let wiki = row . mainWiki ;
6066
61- const mps = [ '**Wiki links detected:**' ] ;
62- const removeCodeblocks = msg . cleanContent . replace ( / ` { 3 } [ \S \s ] * ?` { 3 } / gm, '' ) ;
63- const removeInlineCode = removeCodeblocks . replace ( / ` [ \S \s ] * ?` / gm, '' ) ;
64- const cleaned = removeInlineCode . replace ( / \u200B / g, '' ) ;
67+ sql . all ( `SELECT * FROM overrides WHERE guildID="${ msg . guild . id } "` ) . then ( rows => {
68+ if ( rows . length !== 0 ) {
69+ for ( let i = 0 ; i < rows . length ; i ++ ) {
70+ if ( rows [ i ] . channelID === msg . channel . id ) {
71+ wiki = rows [ i ] . wiki ;
72+ }
73+ }
74+ }
75+ const mps = [ '**Wiki links detected:**' ] ;
76+ const removeCodeblocks = msg . cleanContent . replace ( / ` { 3 } [ \S \s ] * ?` { 3 } / gm, '' ) ;
77+ const removeInlineCode = removeCodeblocks . replace ( / ` [ \S \s ] * ?` / gm, '' ) ;
78+ const cleaned = removeInlineCode . replace ( / \u200B / g, '' ) ;
6579
66- if ( / \[ \[ ( [ ^ \] | ] + ) (?: | [ ^ \] ] + ) ? \] \] / g. test ( cleaned ) ) {
67- const name = cleaned . replace ( / .* ?\[ \[ ( [ ^ \] | ] + ) (?: | [ ^ \] ] + ) ? \] \] / g, '$1\u200B' ) ;
68- const allLinks = name . split ( '\u200B' ) . slice ( 0 , - 1 ) ;
69- const unique = new Set ( allLinks ) ;
80+ if ( / \[ \[ ( [ ^ \] | ] + ) (?: | [ ^ \] ] + ) ? \] \] / g. test ( cleaned ) ) {
81+ const name = cleaned . replace ( / .* ?\[ \[ ( [ ^ \] | ] + ) (?: | [ ^ \] ] + ) ? \] \] / g, '$1\u200B' ) ;
82+ const allLinks = name . split ( '\u200B' ) . slice ( 0 , - 1 ) ;
83+ const unique = new Set ( allLinks ) ;
7084
71- unique . forEach ( ( item ) => {
72- mps . push ( reqAPI ( wiki , item . trim ( ) ) . catch ( console . error ) ) ;
73- } ) ;
74- }
85+ unique . forEach ( ( item ) => {
86+ mps . push ( reqAPI ( wiki , item . trim ( ) ) . catch ( console . error ) ) ;
87+ } ) ;
88+ }
7589
76- if ( / \{ \{ ( [ ^ } | ] + ) (?: | [ ^ } ] + ) ? \} \} / g. test ( cleaned ) ) {
77- const name = cleaned . replace ( / .* ?\{ \{ ( [ ^ } | ] + ) (?: | [ ^ } ] + ) ? \} \} / g, '$1\u200B' ) ;
78- const allLinks = name . split ( '\u200B' ) . slice ( 0 , - 1 ) ;
79- const unique = new Set ( allLinks ) ;
90+ if ( / \{ \{ ( [ ^ } | ] + ) (?: | [ ^ } ] + ) ? \} \} / g. test ( cleaned ) ) {
91+ const name = cleaned . replace ( / .* ?\{ \{ ( [ ^ } | ] + ) (?: | [ ^ } ] + ) ? \} \} / g, '$1\u200B' ) ;
92+ const allLinks = name . split ( '\u200B' ) . slice ( 0 , - 1 ) ;
93+ const unique = new Set ( allLinks ) ;
8094
81- unique . forEach ( ( item ) => {
82- mps . push ( reqAPI ( wiki , `Template:${ item . trim ( ) } ` ) . catch ( console . error ) ) ;
83- } ) ;
84- }
95+ unique . forEach ( ( item ) => {
96+ mps . push ( reqAPI ( wiki , `Template:${ item . trim ( ) } ` ) . catch ( console . error ) ) ;
97+ } ) ;
98+ }
8599
86- if ( / - - ( [ ^ \- | ] + ) (?: | [ ^ - ] + ) ? - - / g. test ( cleaned ) ) {
87- const name = cleaned . replace ( / .* ?- - ( [ ^ \- | ] + ) (?: | [ ^ - ] + ) ? - - / g, '$1\u200B' ) ;
88- const allLinks = name . split ( '\u200B' ) . slice ( 0 , - 1 ) ;
89- const unique = new Set ( allLinks ) ;
100+ if ( / - - ( [ ^ \- | ] + ) (?: | [ ^ - ] + ) ? - - / g. test ( cleaned ) ) {
101+ const name = cleaned . replace ( / .* ?- - ( [ ^ \- | ] + ) (?: | [ ^ - ] + ) ? - - / g, '$1\u200B' ) ;
102+ const allLinks = name . split ( '\u200B' ) . slice ( 0 , - 1 ) ;
103+ const unique = new Set ( allLinks ) ;
90104
91- unique . forEach ( ( item ) => {
92- mps . push ( `<http://${ wiki } .wikia.com/wiki/${ item . trim ( ) . replace ( / \s / g, '_' ) } >` ) ;
93- } ) ;
94- }
105+ unique . forEach ( ( item ) => {
106+ mps . push ( `<http://${ wiki } .wikia.com/wiki/${ item . trim ( ) . replace ( / \s / g, '_' ) } >` ) ;
107+ } ) ;
108+ }
95109
96- Promise . all ( mps )
110+ Promise . all ( mps )
97111 . then ( preparedSend => {
98112 preparedSend = preparedSend . filter ( item => item !== undefined ) ;
99113 if ( preparedSend . length > 1 ) {
@@ -102,6 +116,8 @@ bot.on('message', (msg) => {
102116 }
103117 } )
104118 . catch ( console . error ) ;
119+ } ) ;
120+ } ) ;
105121 }
106122} ) ;
107123
@@ -121,73 +137,103 @@ const commands = {
121137 process . exit ( 1 ) ;
122138 } ) ;
123139 } ,
124- // eslint-disable-next-line consistent-return
125- broadcast : ( msg , [ globalMessage ] ) => {
126- if ( msg . author . id !== config . admin_snowflake ) return msg . reply ( "you don't get to yell at everyone!" ) ;
127- var joinedGuilds = Array . from ( bot . guilds . keys ( ) ) ;
140+ bc : ( msg , [ globalMessage ] ) => {
141+ if ( msg . author . id !== config . admin_snowflake ) {
142+ msg . reply ( "you don't get to yell at everyone!" ) ;
143+ return ;
144+ }
145+ /* var joinedGuilds = Array.from(bot.guilds.keys());
128146 for (var i = 0; i < joinedGuilds.length; i++) {
129147 var guildID = joinedGuilds[i];
130148 if (db[guildID].broadcastChannel) {
131149 return bot.channels.get(db[guildID].broadcastChannel).send(globalMessage);
132150 }
133- }
151+ }*/
152+
153+ sql . each ( `SELECT * FROM guilds` , ( err , row ) => {
154+ if ( row . broadcastChannel && ! err ) {
155+ bot . channels . get ( row . broadcastChannel ) . send ( globalMessage ) ;
156+ }
157+ } ) ;
134158 } ,
135159 swiki : ( msg , [ wiki ] ) => {
136160 if ( msg . author . id !== config . admin_snowflake || ! msg . member . hasPermission ( 'ADMINISTRATOR' ) ) {
137- return msg . reply ( 'You are not allowed to change the default wiki of this server.' ) ;
161+ msg . reply ( 'You are not allowed to change the default wiki of this server.' ) ;
162+ return ;
138163 }
139- db [ msg . guild . id ] . wiki = wiki . split ( ' ' ) [ 0 ] ;
140- return saveDB ( ) . then ( ( ) => {
141- msg . reply ( `Wiki is now set to: ${ wiki } .` ) ;
142- } ) . catch ( console . error ) ;
164+ wiki = wiki . split ( ' ' ) [ 0 ] ;
165+ sql . get ( `SELECT * FROM guilds WHERE id=${ msg . guild . id } ` ) . then ( row => {
166+ if ( ! row ) {
167+ sql . run ( 'INSERT INTO guilds (mainWiki) VALUES (?)' , [ wiki ] ) . then ( ( ) =>
168+ msg . reply ( `Wiki is now set to: ${ wiki } ` )
169+ ) . catch ( ( ) => msg . reply ( 'Database error - please contact the developer!' ) ) ;
170+ } else {
171+ sql . run ( `UPDATE guilds SET mainWiki="${ wiki } " WHERE id="${ msg . guild . id } "` ) . then ( ( ) =>
172+ msg . reply ( `Wiki is now set to: ${ wiki } ` ) ) ;
173+ }
174+ } ) ;
143175 } ,
144176 cwiki : ( msg , [ wiki ] ) => {
145177 if ( msg . author . id !== config . admin_snowflake || ! msg . member . hasPermission ( 'ADMINISTRATOR' ) ) {
146- return msg . reply ( 'You are not allowed to override the wiki of this channel.' ) ;
178+ msg . reply ( 'You are not allowed to override the wiki of this channel.' ) ; return ;
147179 } else if ( msg . channel . id === msg . guild . id ) {
148- return msg . reply ( 'You can\'t override the default channel of a server.' ) ;
180+ msg . reply ( 'You can\'t override the default channel of a server.' ) ; return ;
149181 }
150- if ( ! db [ msg . guild . id ] . channelOverrides ) db [ msg . guild . id ] . channelOverrides = { } ;
151- db [ msg . guild . id ] . channelOverrides [ msg . channel . id ] = wiki . split ( ' ' ) [ 0 ] ;
152- return saveDB ( ) . then ( ( ) => {
153- msg . reply ( `Wiki in this channel is now set to: ${ wiki } .` ) ;
154- } ) . catch ( console . error ) ;
182+ console . log ( wiki ) ;
183+ wiki = wiki . split ( ' ' ) [ 0 ] ;
184+ sql . get ( `SELECT * FROM overrides WHERE guildID="${ msg . guild . id } " AND channelID="${ msg . channel . id } "` ) . then ( row => {
185+ if ( row ) {
186+ sql . run ( `UPDATE overrides SET wiki="${ wiki } " WHERE guildID="${ msg . guild . id } " AND channelID="${ msg . channel . id } "` ) ;
187+ } else {
188+ sql . run ( `INSERT INTO overrides (guildID, channelID, wiki) VALUES (?,?,?)` , [ msg . guild . id , msg . channel . id , wiki ] ) ;
189+ }
190+ } ) . then ( ( ) => msg . reply ( `The wiki override for channel ${ msg . channel . name } is now set to ${ wiki } ` ) ) ;
155191 } ,
156- broadcastchan : ( msg ) => {
192+ bchan : ( msg ) => {
157193 if ( ! msg . mentions . channels || msg . mentions . channels . size > 1 ) {
158- return msg . reply ( 'You need to mention exactly one channel to be set as broadcast channel.' ) ;
194+ msg . reply ( 'You need to mention exactly one channel to be set as broadcast channel.' ) ;
195+ return ;
159196 } else {
160197 var channel = msg . mentions . channels . first ( ) ;
161- db [ msg . guild . id ] . broadcastChannel = channel . id ;
198+ /* db[msg.guild.id].broadcastChannel = channel.id;
162199 return saveDB().then(() => {
163200 msg.reply(`The broadcast channel for this server is now set to: ${channel.name}.`);
201+ }); */
202+ sql . get ( `SELECT * FROM guilds WHERE id="${ msg . guild . id } "` ) . then ( row => {
203+ if ( row ) {
204+ sql . run ( `UPDATE guilds SET broadcastChannel="${ channel . id } " WHERE id="${ msg . guild . id } "` ) . then ( ( ) =>
205+ msg . reply ( `The broadcast channel for this server is now set to: ${ channel . name } .` )
206+ ) ;
207+ } else {
208+ msg . reply ( 'Database error - please contact the developer!' ) ;
209+ }
164210 } ) ;
165211 }
166212 } ,
167- serverinfo : ( msg ) => {
213+ sinfo : ( msg ) => {
168214 if ( ! msg . guild ) return ;
169- var totalMessage = `\`\`\`\nInfo for server: ${ msg . guild . name } ` ;
170- if ( ! msg . guild . settings . broadcastChannel ) {
171- totalMessage += '\nNo broadcast channel set' ;
172- } else {
173- totalMessage += `\nBroadcast channel: ${ msg . guild . channels . get ( msg . guild . settings . broadcastChannel ) . name } ` ;
174- }
175- if ( ! msg . guild . settings . wiki ) {
176- totalMessage += '\nNo main wiki set' ;
177- } else {
178- totalMessage += `\nMain wiki: ${ msg . guild . settings . wiki } ` ;
179- }
180- if ( ! msg . guild . settings . channelOverrides ) {
181- totalMessage += '\nNo channel overrides set ' ;
182- } else {
183- totalMessage += '\nChannel overrides:' ;
184- var channelIDs = Object . keys ( msg . guild . settings . channelOverrides ) ;
185- for ( var i = 0 ; i < channelIDs . length ; i ++ ) {
186- totalMessage += `\n Wiki ${ msg . guild . settings . channelOverrides [ channelIDs [ i ] ] } in channel ${ msg . guild . channels . get ( channelIDs [ i ] ) . name } ` ;
187- }
188- }
189- totalMessage += '\n```' ;
190- msg . channel . send ( totalMessage ) ;
215+ sql . get ( `SELECT * FROM guilds WHERE id=" ${ msg . guild . id } "` ) . then ( row => {
216+ var totalMessage = `\`\`\`\nInfo for server: ${ msg . guild . name } ` ;
217+ if ( ! row . broadcastChannel ) totalMessage += '\nNo broadcast channel set' ;
218+ else totalMessage += `\nBroadcast channel: ${ msg . guild . channels . get ( row . broadcastChannel ) . name } ` ;
219+
220+ if ( ! row . mainWiki ) totalMessage += '\nNo main wiki set' ;
221+ else totalMessage += `\nMain wiki: ${ row . mainWiki } ` ;
222+
223+ sql . all ( `SELECT * FROM overrides WHERE guildID=" ${ msg . guild . id } "` ) . then ( rows => {
224+ if ( rows . length === 0 ) {
225+ totalMessage += '\nNo channel overrides set' ;
226+ } else {
227+ totalMessage += '\nChannel overrides: ' ;
228+ for ( let i = 0 ; i < rows . length ; i ++ ) {
229+ totalMessage += `\n Wiki ${ rows [ i ] . wiki } in channel ${ msg . guild . channels . get ( rows [ i ] . channelID ) . name } ` ;
230+ }
231+ }
232+
233+ totalMessage += '\n```' ;
234+ msg . channel . send ( totalMessage ) ;
235+ } ) ;
236+ } ) ;
191237 }
192238} ;
193239
@@ -208,8 +254,6 @@ const reqAPI = (wiki, requestname) => new Promise((resolve, reject) => {
208254 } ) ;
209255} ) ;
210256
211- const saveDB = ( ) => fs . writeFileAsync ( dbfile , JSON . stringify ( db , null , 2 ) ) ;
212-
213257if ( config . admin_snowflake === '' ) {
214258 console . log ( 'Admin snowflake empty. Startup disallowed.' ) ;
215259 process . exit ( 1 ) ;
0 commit comments