1
0

util.js 3.0 KB

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