util.js 3.8 KB

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