util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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} userPermissions
  35. * @property {Boolean} [patreon]
  36. * @property {String} [botPermissions]
  37. * @property {{id: String, name: String, permissions: Number}[]} [channels]
  38. * @property {{id: String, name: String, lower: Boolean}[]} [roles]
  39. */
  40. /**
  41. * @type {Map<String, Settings>}
  42. */
  43. const settingsData = new Map();
  44. /**
  45. * @type {Map<Number, PromiseConstructor>}
  46. */
  47. const messages = new Map();
  48. var messageId = 1;
  49. process.on( 'message', message => {
  50. if ( message.id ) {
  51. if ( message.data.error ) messages.get(message.id).reject(message.data.error);
  52. else messages.get(message.id).resolve(message.data.response);
  53. return messages.delete(message.id);
  54. }
  55. console.log( '- [Dashboard]: Message received!', message );
  56. } );
  57. /**
  58. * Send messages to the manager.
  59. * @param {Object} [message] - The message.
  60. * @returns {Promise<Object>}
  61. */
  62. function sendMsg(message) {
  63. var id = messageId++;
  64. var promise = new Promise( (resolve, reject) => {
  65. messages.set(id, {resolve, reject});
  66. process.send( {id, data: message} );
  67. } );
  68. return promise;
  69. }
  70. /**
  71. * Create a red notice
  72. * @param {CheerioStatic} $ - The cheerio static
  73. * @param {Object} notice - The notices to create
  74. * @param {String} notice.title - The title of the notice
  75. * @param {String} notice.text - The text of the notice
  76. * @param {String} [notice.type] - The type of the notice
  77. * @returns {Cheerio}
  78. */
  79. function createNotice($, notice) {
  80. var type = ( notice.type ? `notice-${notice.type}` : '' );
  81. return $('<div class="notice">').append(
  82. $('<b>').text(notice.title),
  83. $('<div>').text(notice.text)
  84. ).addClass(type);
  85. }
  86. const permissions = {
  87. ADMINISTRATOR: 1 << 3,
  88. MANAGE_CHANNELS: 1 << 4,
  89. MANAGE_GUILD: 1 << 5,
  90. ADD_REACTIONS: 1 << 6,
  91. VIEW_CHANNEL: 1 << 10,
  92. SEND_MESSAGES: 1 << 11,
  93. MANAGE_MESSAGES: 1 << 13,
  94. EMBED_LINKS: 1 << 14,
  95. ATTACH_FILES: 1 << 15,
  96. READ_MESSAGE_HISTORY: 1 << 16,
  97. USE_EXTERNAL_EMOJIS: 1 << 18,
  98. MANAGE_NICKNAMES: 1 << 27,
  99. MANAGE_ROLES: 1 << 28,
  100. MANAGE_WEBHOOKS: 1 << 29
  101. }
  102. /**
  103. * Check if a permission is included in the BitField
  104. * @param {String|Number} all - BitField of multiple permissions
  105. * @param {String[]} permission - Name of the permission to check for
  106. * @returns {Boolean}
  107. */
  108. function hasPerm(all, ...permission) {
  109. if ( (all & permissions.ADMINISTRATOR) === permissions.ADMINISTRATOR ) return true;
  110. return permission.map( perm => {
  111. let bit = permissions[perm];
  112. return ( (all & bit) === bit );
  113. } ).every( perm => perm );
  114. }
  115. module.exports = {db, settingsData, sendMsg, createNotice, hasPerm};