database.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. const {defaultSettings} = require('../util/default.json');
  2. const sqlite3 = require('sqlite3').verbose();
  3. var db = new sqlite3.Database( './wikibot.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, dberror => {
  4. if ( dberror ) {
  5. console.log( '- ' + shardId + ': Error while connecting to the database: ' + dberror );
  6. return dberror;
  7. }
  8. console.log( '- ' + shardId + ': Connected to the database.' );
  9. getSettings();
  10. } );
  11. /**
  12. * Fill the patreon list.
  13. * @param {Number} [trysettings] - The amount of tries.
  14. */
  15. function getSettings(trysettings = 1) {
  16. db.each( 'SELECT guild, prefix FROM discord WHERE patreon IS NOT NULL', [], (dberror, row) => {
  17. if ( dberror ) {
  18. console.log( '- ' + shardId + ': ' + trysettings + '. Error while getting the patreon: ' + dberror );
  19. if ( trysettings < 10 ) {
  20. trysettings++;
  21. getSettings(trysettings);
  22. }
  23. return dberror;
  24. }
  25. patreons[row.guild] = row.prefix;
  26. }, (dberror) => {
  27. if ( dberror ) {
  28. console.log( '- ' + trysettings + '. Error while getting the patreons: ' + dberror );
  29. if ( dberror.message === 'SQLITE_ERROR: no such table: discord' ) db.serialize( () => {
  30. db.run( 'CREATE TABLE IF NOT EXISTS patreons(patreon TEXT PRIMARY KEY UNIQUE NOT NULL, count INTEGER NOT NULL)', [], function (error) {
  31. if ( error ) {
  32. console.log( '- ' + shardId + ': Error while creating the patreons table: ' + error );
  33. return error;
  34. }
  35. console.log( '- Created the patreons table.' );
  36. db.run( 'CREATE INDEX idx_patreons_patreon ON patreons(patreon)', [], function (idxerror) {
  37. if ( idxerror ) {
  38. console.log( '- ' + shardId + ': Error while creating the patreons index: ' + idxerror );
  39. return idxerror;
  40. }
  41. console.log( '- ' + shardId + ': Created the patreons index.' );
  42. } );
  43. } );
  44. db.run( 'CREATE TABLE IF NOT EXISTS discord(guild TEXT NOT NULL, channel TEXT, lang TEXT NOT NULL DEFAULT ?, wiki TEXT NOT NULL DEFAULT ?, prefix TEXT NOT NULL DEFAULT ?, patreon TEXT, voice INTEGER, inline INTEGER, UNIQUE(guild, channel), FOREIGN KEY(patreon) REFERENCES patreons(patreon) ON DELETE SET NULL)', [defaultSettings.lang, defaultSettings.wiki, process.env.prefix], function (error) {
  45. if ( error ) {
  46. console.log( '- ' + shardId + ': Error while creating the discord table: ' + error );
  47. return error;
  48. }
  49. console.log( '- Created the discord table.' );
  50. db.run( 'CREATE TRIGGER unique_discord_guild BEFORE INSERT ON discord WHEN NEW.channel IS NULL BEGIN SELECT CASE WHEN (SELECT 1 FROM discord WHERE guild = NEW.guild AND channel IS NULL) IS NOT NULL THEN RAISE(ABORT, "UNIQUE constraint failed: discord.guild, discord.channel") END; END;', [], function (tgerror) {
  51. if ( tgerror ) {
  52. console.log( '- ' + shardId + ': Error while creating the discord guild trigger: ' + tgerror );
  53. return tgerror;
  54. }
  55. console.log( '- ' + shardId + ': Created the discord guild trigger.' );
  56. } );
  57. db.run( 'CREATE INDEX idx_discord_patreon ON discord(patreon) WHERE patreon IS NOT NULL', [], function (idxerror) {
  58. if ( idxerror ) {
  59. console.log( '- ' + shardId + ': Error while creating the discord patreon index: ' + idxerror );
  60. return idxerror;
  61. }
  62. console.log( '- ' + shardId + ': Created the discord patreon index.' );
  63. } );
  64. db.run( 'CREATE INDEX idx_discord_voice ON discord(voice) WHERE voice IS NOT NULL', [], function (idxerror) {
  65. if ( idxerror ) {
  66. console.log( '- ' + shardId + ': Error while creating the discord voice index: ' + idxerror );
  67. return idxerror;
  68. }
  69. console.log( '- ' + shardId + ': Created the discord voice index.' );
  70. } );
  71. db.run( 'CREATE INDEX idx_discord_channel ON discord(guild, channel DESC)', [], function (idxerror) {
  72. if ( idxerror ) {
  73. console.log( '- ' + shardId + ': Error while creating the discord channel index: ' + idxerror );
  74. return idxerror;
  75. }
  76. console.log( '- ' + shardId + ': Created the discord channel index.' );
  77. } );
  78. db.run( 'PRAGMA foreign_keys = ON;', [], function (fkerror) {
  79. if ( fkerror ) {
  80. console.log( '- ' + shardId + ': Error while enabling the foreign key constraint: ' + fkerror );
  81. return fkerror;
  82. }
  83. console.log( '- ' + shardId + ': Enabled the foreign key constraint.' );
  84. } );
  85. if ( trysettings < 10 ) {
  86. trysettings++;
  87. getSettings(trysettings);
  88. }
  89. } );
  90. db.run( 'CREATE TABLE IF NOT EXISTS verification(guild TEXT NOT NULL, configid INTEGER NOT NULL, channel TEXT NOT NULL, role TEXT NOT NULL, editcount INTEGER NOT NULL DEFAULT ?, usergroup TEXT NOT NULL DEFAULT ?, accountage INTEGER NOT NULL DEFAULT ?, rename INTEGER NOT NULL DEFAULT ?, UNIQUE(guild, configid))', [0, 'user', 0, 0], function (error) {
  91. if ( error ) {
  92. console.log( '- ' + shardId + ': Error while creating the verification table: ' + error );
  93. return error;
  94. }
  95. console.log( '- ' + shardId + ': Created the verification table.' );
  96. db.run( 'CREATE INDEX idx_verification_config ON verification(guild, configid ASC, channel)', [], function (idxerror) {
  97. if ( idxerror ) {
  98. console.log( '- ' + shardId + ': Error while creating the verification index: ' + idxerror );
  99. return idxerror;
  100. }
  101. console.log( '- ' + shardId + ': Created the verification index.' );
  102. } );
  103. } );
  104. db.run( 'CREATE TABLE IF NOT EXISTS rcgcdw(guild TEXT NOT NULL, configid INTEGER NOT NULL, webhook TEXT NOT NULL UNIQUE, wiki TEXT NOT NULL, lang TEXT NOT NULL DEFAULT ?, display INTEGER NOT NULL DEFAULT ?, wikiid INTEGER, rcid INTEGER, postid TEXT, UNIQUE(guild, configid))', [defaultSettings.lang, 1], function (error) {
  105. if ( error ) {
  106. console.log( '- ' + shardId + ': Error while creating the rcgcdw table: ' + error );
  107. return error;
  108. }
  109. console.log( '- ' + shardId + ': Created the rcgcdw table.' );
  110. db.run( 'CREATE INDEX idx_rcgcdw_wiki ON rcgcdw(wiki)', [], function (idxerror) {
  111. if ( idxerror ) {
  112. console.log( '- ' + shardId + ': Error while creating the rcgcdw wiki index: ' + idxerror );
  113. return idxerror;
  114. }
  115. console.log( '- ' + shardId + ': Created the rcgcdw wiki index.' );
  116. } );
  117. db.run( 'CREATE INDEX idx_rcgcdw_webhook ON rcgcdw(webhook)', [], function (idxerror) {
  118. if ( idxerror ) {
  119. console.log( '- ' + shardId + ': Error while creating the rcgcdw webhook index: ' + idxerror );
  120. return idxerror;
  121. }
  122. console.log( '- ' + shardId + ': Created the rcgcdw webhook index.' );
  123. } );
  124. db.run( 'CREATE INDEX idx_rcgcdw_config ON rcgcdw(guild, configid ASC)', [], function (idxerror) {
  125. if ( idxerror ) {
  126. console.log( '- ' + shardId + ': Error while creating the rcgcdw config index: ' + idxerror );
  127. return idxerror;
  128. }
  129. console.log( '- ' + shardId + ': Created the rcgcdw config index.' );
  130. } );
  131. } );
  132. } );
  133. else {
  134. if ( trysettings < 10 ) {
  135. trysettings++;
  136. getSettings(trysettings);
  137. }
  138. }
  139. return dberror;
  140. }
  141. console.log( '- ' + shardId + ': Patreons successfully loaded.' );
  142. getVoice();
  143. } );
  144. }
  145. /**
  146. * Fill the voice list.
  147. * @param {Number} [trysettings] - The amount of tries.
  148. */
  149. function getVoice(trysettings = 1) {
  150. db.each( 'SELECT guild, lang FROM discord WHERE voice IS NOT NULL', [], (dberror, row) => {
  151. if ( dberror ) {
  152. console.log( '- ' + shardId + ': ' + trysettings + '. Error while getting the voice channel: ' + dberror );
  153. if ( trysettings < 10 ) {
  154. trysettings++;
  155. getVoice(trysettings);
  156. }
  157. return dberror;
  158. }
  159. voice[row.guild] = row.lang;
  160. }, (dberror) => {
  161. if ( dberror ) {
  162. console.log( '- ' + shardId + ': ' + trysettings + '. Error while getting the voice channels: ' + dberror );
  163. if ( trysettings < 10 ) {
  164. trysettings++;
  165. getVoice(trysettings);
  166. }
  167. return dberror;
  168. }
  169. console.log( '- ' + shardId + ': Voice channels successfully loaded.' );
  170. } );
  171. }
  172. module.exports = db;