util.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const sqlite3 = require('sqlite3').verbose();
  2. const mode = ( process.env.READONLY ? sqlite3.OPEN_READONLY : sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE );
  3. const db = new sqlite3.Database( './wikibot.db', mode, dberror => {
  4. if ( dberror ) {
  5. console.log( '- Dashboard: Error while connecting to the database: ' + dberror );
  6. return dberror;
  7. }
  8. console.log( '- Dashboard: Connected to the database.' );
  9. } );
  10. /**
  11. * @typedef Settings
  12. * @property {String} state
  13. * @property {String} access_token
  14. * @property {User} user
  15. * @property {Object} guilds
  16. * @property {Number} guilds.count
  17. * @property {Map<String, Guild>} guilds.isMember
  18. * @property {Map<String, Guild>} guilds.notMember
  19. */
  20. /**
  21. * @typedef User
  22. * @property {String} id
  23. * @property {String} username
  24. * @property {String} discriminator
  25. * @property {String} avatar
  26. * @property {String} locale
  27. */
  28. /**
  29. * @typedef Guild
  30. * @property {String} id
  31. * @property {String} name
  32. * @property {String} acronym
  33. * @property {String} [icon]
  34. * @property {String} permissions
  35. */
  36. /**
  37. * @type {Map<String, Settings>}
  38. */
  39. const settingsData = new Map();
  40. /**
  41. * @type {Map<Number, PromiseConstructor>}
  42. */
  43. const messages = new Map();
  44. var messageId = 1;
  45. process.on( 'message', message => {
  46. if ( message.id ) {
  47. if ( message.data.error ) messages.get(message.id).reject(message.data.error);
  48. else messages.get(message.id).resolve(message.data.response);
  49. return messages.delete(message.id);
  50. }
  51. console.log( '- [Dashboard]: Message received!', message );
  52. } );
  53. /**
  54. * Send messages to the manager.
  55. * @param {Object} [message] - The message.
  56. * @returns {Promise<Object>}
  57. */
  58. function sendMsg(message) {
  59. var id = messageId++;
  60. var promise = new Promise( (resolve, reject) => {
  61. messages.set(id, {resolve, reject});
  62. process.send( {id, data: message} );
  63. } );
  64. return promise;
  65. }
  66. /**
  67. * Create a red notice
  68. * @param {CheerioStatic} $ - The cheerio static
  69. * @param {Object} notice - The notices to create
  70. * @param {String} notice.title - The title of the notice
  71. * @param {String} notice.text - The text of the notice
  72. * @returns {Cheerio}
  73. */
  74. function createNotice($, notice) {
  75. return $('<div class="notice">').append(
  76. $('<b>').text(notice.title),
  77. $('<div>').text(notice.text)
  78. );
  79. }
  80. const permissions = {
  81. ADMINISTRATOR: 1 << 3,
  82. MANAGE_CHANNELS: 1 << 4,
  83. MANAGE_GUILD: 1 << 5,
  84. MANAGE_MESSAGES: 1 << 13,
  85. MENTION_EVERYONE: 1 << 17,
  86. MANAGE_NICKNAMES: 1 << 27,
  87. MANAGE_ROLES: 1 << 28,
  88. MANAGE_WEBHOOKS: 1 << 29,
  89. MANAGE_EMOJIS: 1 << 30
  90. }
  91. /**
  92. * Check if a permission is included in the BitField
  93. * @param {String|Number} all - BitField of multiple permissions
  94. * @param {String} permission - Name of the permission to check for
  95. * @param {Boolean} [admin] - If administrator permission can overwrite
  96. * @returns {Boolean}
  97. */
  98. function hasPerm(all, permission, admin = true) {
  99. var bit = permissions[permission];
  100. var adminOverwrite = ( admin && (all & permissions.ADMINISTRATOR) === permissions.ADMINISTRATOR );
  101. return ( adminOverwrite || (all & bit) === bit )
  102. }
  103. module.exports = {db, settingsData, sendMsg, createNotice, hasPerm};