database.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 [' + defaultSettings.lang + '], wiki TEXT NOT NULL DEFAULT [' + defaultSettings.wiki + '], prefix TEXT NOT NULL DEFAULT [' + process.env.prefix + '], patreon TEXT, voice INTEGER, inline INTEGER, UNIQUE(guild, channel), FOREIGN KEY(patreon) REFERENCES patreons(patreon) ON DELETE SET NULL)', [], 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 [0], usergroup TEXT NOT NULL DEFAULT [user], accountage INTEGER NOT NULL DEFAULT [0], rename INTEGER NOT NULL DEFAULT [0], UNIQUE(guild, configid))', [], 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 [' + defaultSettings.lang + '], display INTEGER NOT NULL DEFAULT [1], wikiid INTEGER, rcid INTEGER, postid TEXT, UNIQUE(guild, configid))', [], 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. db.run( 'CREATE TABLE IF NOT EXISTS blocklist(wiki TEXT UNIQUE NOT NULL, reason TEXT)', [], function (error) {
  133. if ( error ) {
  134. console.log( '- ' + shardId + ': Error while creating the blocklist table: ' + error );
  135. return error;
  136. }
  137. console.log( '- ' + shardId + ': Created the blocklist table.' );
  138. db.run( 'CREATE INDEX idx_blocklist_wiki ON blocklist(wiki)', [], function (idxerror) {
  139. if ( idxerror ) {
  140. console.log( '- ' + shardId + ': Error while creating the blocklist wiki index: ' + idxerror );
  141. return idxerror;
  142. }
  143. console.log( '- ' + shardId + ': Created the blocklist wiki index.' );
  144. } );
  145. } );
  146. } );
  147. else {
  148. if ( trysettings < 10 ) {
  149. trysettings++;
  150. getSettings(trysettings);
  151. }
  152. }
  153. return dberror;
  154. }
  155. console.log( '- ' + shardId + ': Patreons successfully loaded.' );
  156. getVoice();
  157. } );
  158. }
  159. /**
  160. * Fill the voice list.
  161. * @param {Number} [trysettings] - The amount of tries.
  162. */
  163. function getVoice(trysettings = 1) {
  164. db.each( 'SELECT guild, lang FROM discord WHERE voice IS NOT NULL', [], (dberror, row) => {
  165. if ( dberror ) {
  166. console.log( '- ' + shardId + ': ' + trysettings + '. Error while getting the voice channel: ' + dberror );
  167. if ( trysettings < 10 ) {
  168. trysettings++;
  169. getVoice(trysettings);
  170. }
  171. return dberror;
  172. }
  173. voice[row.guild] = row.lang;
  174. }, (dberror) => {
  175. if ( dberror ) {
  176. console.log( '- ' + shardId + ': ' + trysettings + '. Error while getting the voice channels: ' + dberror );
  177. if ( trysettings < 10 ) {
  178. trysettings++;
  179. getVoice(trysettings);
  180. }
  181. return dberror;
  182. }
  183. console.log( '- ' + shardId + ': Voice channels successfully loaded.' );
  184. } );
  185. }
  186. module.exports = db;