1
0

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. if ( trysettings < 10 ) {
  80. trysettings++;
  81. getSettings(trysettings);
  82. }
  83. } );
  84. 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) {
  85. if ( error ) {
  86. console.log( '- ' + shardId + ': Error while creating the verification table: ' + error );
  87. return error;
  88. }
  89. console.log( '- ' + shardId + ': Created the verification table.' );
  90. db.run( 'CREATE INDEX idx_verification_config ON verification(guild, configid ASC, channel)', [], function (idxerror) {
  91. if ( idxerror ) {
  92. console.log( '- ' + shardId + ': Error while creating the verification index: ' + idxerror );
  93. return idxerror;
  94. }
  95. console.log( '- ' + shardId + ': Created the verification index.' );
  96. } );
  97. } );
  98. 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) {
  99. if ( error ) {
  100. console.log( '- ' + shardId + ': Error while creating the rcgcdw table: ' + error );
  101. return error;
  102. }
  103. console.log( '- ' + shardId + ': Created the rcgcdw table.' );
  104. db.run( 'CREATE INDEX idx_rcgcdw_wiki ON rcgcdw(wiki)', [], function (idxerror) {
  105. if ( idxerror ) {
  106. console.log( '- ' + shardId + ': Error while creating the rcgcdw wiki index: ' + idxerror );
  107. return idxerror;
  108. }
  109. console.log( '- ' + shardId + ': Created the rcgcdw wiki index.' );
  110. } );
  111. db.run( 'CREATE INDEX idx_rcgcdw_webhook ON rcgcdw(webhook)', [], function (idxerror) {
  112. if ( idxerror ) {
  113. console.log( '- ' + shardId + ': Error while creating the rcgcdw webhook index: ' + idxerror );
  114. return idxerror;
  115. }
  116. console.log( '- ' + shardId + ': Created the rcgcdw webhook index.' );
  117. } );
  118. db.run( 'CREATE INDEX idx_rcgcdw_config ON rcgcdw(guild, configid ASC)', [], function (idxerror) {
  119. if ( idxerror ) {
  120. console.log( '- ' + shardId + ': Error while creating the rcgcdw config index: ' + idxerror );
  121. return idxerror;
  122. }
  123. console.log( '- ' + shardId + ': Created the rcgcdw config index.' );
  124. } );
  125. } );
  126. db.run( 'CREATE TABLE IF NOT EXISTS blocklist(wiki TEXT UNIQUE NOT NULL, reason TEXT)', [], function (error) {
  127. if ( error ) {
  128. console.log( '- ' + shardId + ': Error while creating the blocklist table: ' + error );
  129. return error;
  130. }
  131. console.log( '- ' + shardId + ': Created the blocklist table.' );
  132. db.run( 'CREATE INDEX idx_blocklist_wiki ON blocklist(wiki)', [], function (idxerror) {
  133. if ( idxerror ) {
  134. console.log( '- ' + shardId + ': Error while creating the blocklist wiki index: ' + idxerror );
  135. return idxerror;
  136. }
  137. console.log( '- ' + shardId + ': Created the blocklist wiki index.' );
  138. } );
  139. } );
  140. } );
  141. else {
  142. if ( trysettings < 10 ) {
  143. trysettings++;
  144. getSettings(trysettings);
  145. }
  146. }
  147. return dberror;
  148. }
  149. db.run( 'PRAGMA foreign_keys = ON;', [], function (fkerror) {
  150. if ( fkerror ) {
  151. console.log( '- ' + shardId + ': Error while enabling the foreign key constraint: ' + fkerror );
  152. return fkerror;
  153. }
  154. console.log( '- ' + shardId + ': Enabled the foreign key constraint.' );
  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;