util.js 3.6 KB

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