11const Discord = require ( 'discord.js' ) ;
2+ const bot = new Discord . Client ( ) ;
23const request = require ( 'request' ) ;
3- const jsonfile = require ( 'jsonfile' ) ;
4- const fs = require ( 'fs' ) ;
4+ const fs = require ( 'fs-extra-promise' ) ;
55const config = require ( './config.json' ) ;
6+ const dbfile = './db.json' ;
67
7- const bot = new Discord . Client ( ) ;
8+ let trulyReady = false ;
9+ let db ;
810
9- const dbfile = './db.json' ;
11+ try {
12+ db = require ( dbfile ) ;
13+ } catch ( err ) {
14+ db = { } ;
15+ }
16+
17+ bot . on ( 'ready' , ( ) => {
18+ bot . guilds . forEach ( guild => {
19+ if ( ! db . hasOwnProperty ( guild . id ) ) db [ guild . id ] = { } ;
20+ guild . settings = db [ guild . id ] ;
21+ } ) ;
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 ) ;
26+ } ) ;
1027
1128bot . on ( 'message' , ( msg ) => {
12- if ( msg . author . bot ) return ;
29+ if ( msg . author . bot || ! msg . guild || ! trulyReady ) return ;
1330
1431 if ( msg . content . startsWith ( config . prefix ) ) {
15- const command = msg . content . slice ( config . prefix . length ) . split ( ' ' ) . shift ( ) ;
16- if ( commands . hasOwnProperty ( command ) ) commands [ command ] ( msg ) ;
17- } else {
32+ const args = msg . content . slice ( config . prefix . length ) . split ( ' ' ) ;
33+ const command = args . shift ( ) ;
34+ if ( commands . hasOwnProperty ( command ) ) commands [ command ] ( msg , args ) ;
35+ } else if ( / \[ \[ ( [ ^ \] | ] + ) (?: | [ ^ \] ] + ) ? \] \] / g. test ( msg . cleanContent ) || / \{ \{ ( [ ^ } | ] + ) (?: | [ ^ } ] + ) ? \} \} / g. test ( msg . cleanContent ) || / - - ( [ ^ \- | ] + ) (?: | [ ^ - ] + ) ? - - / g. test ( msg . cleanContent ) ) {
36+ if ( ! msg . guild . settings . wiki ) {
37+ // eslint-disable-next-line consistant-return
38+ return msg . channel . sendMessage ( [
39+ 'This server has not set a default wiki yet.' ,
40+ 'Users with the "Administrator" permission can do this using %setWiki <wikiname>.'
41+ ] ) ;
42+ }
1843 const mps = [ '**Wiki links detected:**' ] ;
1944 const removeCodeblocks = msg . cleanContent . replace ( / ` { 3 } [ \S \s ] * ?` { 3 } / gm, '' ) ;
2045 const removeInlineCode = removeCodeblocks . replace ( / ` [ \S \s ] * ?` / gm, '' ) ;
@@ -26,8 +51,7 @@ bot.on('message', (msg) => {
2651 const unique = new Set ( allLinks ) ;
2752
2853 unique . forEach ( ( item ) => {
29- var toPush = dbCheck ( msg , item . trim ( ) , false ) ;
30- if ( toPush !== null ) mps . push ( toPush ) ;
54+ mps . push ( reqAPI ( msg . guild . settings . wiki , item . trim ( ) ) . catch ( console . error ) ) ;
3155 } ) ;
3256 }
3357
@@ -37,8 +61,7 @@ bot.on('message', (msg) => {
3761 const unique = new Set ( allLinks ) ;
3862
3963 unique . forEach ( ( item ) => {
40- var toPush = dbCheck ( msg , `Template:${ item . trim ( ) } ` , false ) ;
41- if ( toPush !== null ) mps . push ( toPush ) ;
64+ mps . push ( reqAPI ( msg . guild . settings . wiki , `Template:${ item . trim ( ) } ` ) . catch ( console . error ) ) ;
4265 } ) ;
4366 }
4467
@@ -48,8 +71,7 @@ bot.on('message', (msg) => {
4871 const unique = new Set ( allLinks ) ;
4972
5073 unique . forEach ( ( item ) => {
51- var toPush = dbCheck ( msg , item . trim ( ) . replace ( / \s / g, '_' ) , true ) ;
52- if ( toPush !== null ) mps . push ( toPush ) ;
74+ mps . push ( `<http://${ msg . guild . settings . wiki } .wikia.com/wiki/${ item . trim ( ) . replace ( / \s / g, '_' ) } >` ) ;
5375 } ) ;
5476 }
5577
@@ -70,29 +92,6 @@ bot.on('message', (msg) => {
7092 bot.login(config.token);
7193}); */
7294
73- const dbCheck = ( msg , requestname , raw ) => {
74- jsonfile . readFile ( dbfile , ( err , obj ) => {
75- if ( err ) {
76- console . error ( err ) ;
77- return null ;
78- }
79- if ( ! msg . guild ) {
80- msg . reply ( "please don't PM me :(" ) ;
81- return null ;
82- }
83- if ( obj [ msg . guild . id ] === null || obj [ msg . guild . id ] === undefined ) {
84- msg . channel . sendMessage ( 'This server has not set a default wiki yet.\nUsers with the "Administrator" permission can do this using %setWiki <wikiname>.' ) ;
85- return null ;
86- }
87- const wiki = obj [ msg . guild . id ] ;
88- if ( raw ) {
89- return `<http://${ wiki } .wikia.com/wiki/${ requestname . trim ( ) . replace ( / \s / g, '_' ) } >` ;
90- } else {
91- return reqAPI ( wiki , requestname ) ;
92- }
93- } ) ;
94- } ;
95-
9695const commands = {
9796 help : ( msg ) => {
9897 msg . channel . sendMessage ( 'Syntax and commands: <https://github.com/ThePsionic/RSWikiLinker#syntax>' ) ;
@@ -104,26 +103,14 @@ const commands = {
104103 process . exit ( 1 ) ;
105104 } ) ;
106105 } ,
107- setWiki : ( msg ) => {
106+ setWiki : ( msg , [ wiki ] ) => {
108107 if ( msg . author . id !== config . admin_snowflake || ! msg . member . hasPermission ( 'ADMINISTRATOR' ) ) {
109- return msg . reply ( 'you are not allowed to change the default wiki of this server.' ) ;
110- }
111- if ( ! msg . guild ) {
112- return msg . reply ( 'you can\'t set a default wiki privately as of right now.' ) ;
108+ return msg . reply ( 'You are not allowed to change the default wiki of this server.' ) ;
113109 }
114- return msg . reply ( jsonfile . readFile ( dbfile , ( err , obj ) => {
115- if ( err ) {
116- console . error ( err ) ;
117- return `error while setting wiki - sorry!` ;
118- }
119- var wiki = msg . cleanContent . split ( / \s / g) [ 1 ] ;
120- console . log ( `wiki = ${ wiki } ` ) ;
121- obj [ msg . guild . id ] = wiki ;
122- jsonfile . writeFile ( dbfile , obj , ( innererr ) => {
123- console . error ( innererr ) ;
124- } ) ;
125- return `you have set the default wiki of this server to ${ wiki } .` ;
126- } ) ) ;
110+ db [ msg . guild . id ] . wiki = wiki ;
111+ return saveDB ( ) . then ( ( ) => {
112+ msg . reply ( `Wiki is now set to: ${ wiki } .` ) ;
113+ } ) . catch ( console . error ) ;
127114 }
128115} ;
129116
@@ -144,18 +131,11 @@ const reqAPI = (wiki, requestname) => new Promise((resolve, reject) => {
144131 } ) ;
145132} ) ;
146133
134+ const saveDB = ( ) => fs . writeFileAsync ( dbfile , JSON . stringify ( db , null , 2 ) ) ;
135+
147136if ( config . admin_snowflake === '' ) {
148137 console . log ( 'Admin snowflake empty. Startup disallowed.' ) ;
149138 process . exit ( 1 ) ;
150139} else {
151- fs . stat ( dbfile , ( err ) => {
152- if ( err === null ) {
153- console . log ( 'File exists' ) ;
154- } else if ( err . code === 'ENOENT' ) {
155- fs . writeFile ( dbfile , '{}' ) ;
156- } else {
157- console . error ( 'Bad file error: ' , err . code ) ;
158- }
159- } ) ;
160140 bot . login ( config . token ) ;
161141}
0 commit comments