database.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. function getSettings(trysettings = 1) {
  12. db.each( 'SELECT guild, prefix FROM discord WHERE patreon IS NOT NULL', [], (dberror, row) => {
  13. if ( dberror ) {
  14. console.log( '- ' + shardId + ': ' + trysettings + '. Error while getting the patreon: ' + dberror );
  15. if ( trysettings < 10 ) {
  16. trysettings++;
  17. getSettings(trysettings);
  18. }
  19. return dberror;
  20. }
  21. patreons[row.guild] = row.prefix;
  22. }, (dberror) => {
  23. if ( dberror ) {
  24. console.log( '- ' + trysettings + '. Error while getting the patreons: ' + dberror );
  25. if ( dberror.message === 'SQLITE_ERROR: no such table: discord' ) db.serialize( () => {
  26. db.run( 'CREATE TABLE IF NOT EXISTS patreons(patreon TEXT PRIMARY KEY UNIQUE NOT NULL, count INTEGER NOT NULL)', [], function (error) {
  27. if ( error ) {
  28. console.log( '- ' + shardId + ': Error while creating the patreons table: ' + error );
  29. return error;
  30. }
  31. console.log( '- Created the patreons table.' );
  32. db.run( 'CREATE INDEX idx_patreons_patreon ON patreons(patreon)', [], function (idxerror) {
  33. if ( idxerror ) {
  34. console.log( '- ' + shardId + ': Error while creating the patreons index: ' + idxerror );
  35. return error;
  36. }
  37. console.log( '- ' + shardId + ': Created the patreons index.' );
  38. } );
  39. } );
  40. 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) {
  41. if ( error ) {
  42. console.log( '- ' + shardId + ': Error while creating the discord table: ' + error );
  43. return error;
  44. }
  45. console.log( '- Created the discord table.' );
  46. 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 (idxerror) {
  47. if ( idxerror ) {
  48. console.log( '- ' + shardId + ': Error while creating the discord guild trigger: ' + idxerror );
  49. return error;
  50. }
  51. console.log( '- ' + shardId + ': Created the discord guild trigger.' );
  52. } );
  53. db.run( 'CREATE INDEX idx_discord_patreon ON discord(patreon) WHERE patreon IS NOT NULL', [], function (idxerror) {
  54. if ( idxerror ) {
  55. console.log( '- ' + shardId + ': Error while creating the discord patreon index: ' + idxerror );
  56. return error;
  57. }
  58. console.log( '- ' + shardId + ': Created the discord patreon index.' );
  59. } );
  60. db.run( 'CREATE INDEX idx_discord_voice ON discord(voice) WHERE voice IS NOT NULL', [], function (idxerror) {
  61. if ( idxerror ) {
  62. console.log( '- ' + shardId + ': Error while creating the discord voice index: ' + idxerror );
  63. return error;
  64. }
  65. console.log( '- ' + shardId + ': Created the discord voice index.' );
  66. } );
  67. db.run( 'CREATE INDEX idx_discord_channel ON discord(guild, channel DESC)', [], function (idxerror) {
  68. if ( idxerror ) {
  69. console.log( '- ' + shardId + ': Error while creating the discord channel index: ' + idxerror );
  70. return error;
  71. }
  72. console.log( '- ' + shardId + ': Created the discord channel index.' );
  73. } );
  74. if ( trysettings < 10 ) {
  75. trysettings++;
  76. getSettings(trysettings);
  77. }
  78. } );
  79. 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) {
  80. if ( error ) {
  81. console.log( '- ' + shardId + ': Error while creating the verification table: ' + error );
  82. return error;
  83. }
  84. console.log( '- ' + shardId + ': Created the verification table.' );
  85. db.run( 'CREATE INDEX idx_verification_config ON verification(guild, configid ASC, channel)', [], function (idxerror) {
  86. if ( idxerror ) {
  87. console.log( '- ' + shardId + ': Error while creating the verification index: ' + idxerror );
  88. return error;
  89. }
  90. console.log( '- ' + shardId + ': Created the verification index.' );
  91. } );
  92. } );
  93. } );
  94. else {
  95. if ( trysettings < 10 ) {
  96. trysettings++;
  97. getSettings(trysettings);
  98. }
  99. }
  100. return dberror;
  101. }
  102. console.log( '- ' + shardId + ': Patreons successfully loaded.' );
  103. getVoice();
  104. } );
  105. }
  106. function getVoice(trysettings = 1) {
  107. db.each( 'SELECT guild, lang FROM discord WHERE voice IS NOT NULL', [], (dberror, row) => {
  108. if ( dberror ) {
  109. console.log( '- ' + shardId + ': ' + trysettings + '. Error while getting the voice channel: ' + dberror );
  110. if ( trysettings < 10 ) {
  111. trysettings++;
  112. getVoice(trysettings);
  113. }
  114. return dberror;
  115. }
  116. voice[row.guild] = row.lang;
  117. }, (dberror) => {
  118. if ( dberror ) {
  119. console.log( '- ' + shardId + ': ' + trysettings + '. Error while getting the voice channels: ' + dberror );
  120. if ( trysettings < 10 ) {
  121. trysettings++;
  122. getVoice(trysettings);
  123. }
  124. return dberror;
  125. }
  126. console.log( '- ' + shardId + ': Voice channels successfully loaded.' );
  127. } );
  128. }
  129. module.exports = db;