database.js 8.6 KB

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