util.js 3.4 KB

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