1
0

util.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 {import('cheerio')} $ - The cheerio static
  86. * @param {String} notice - The notice to create
  87. * @param {String[]} [args] - The arguments for the notice
  88. * @returns {import('cheerio')}
  89. */
  90. function createNotice($, notice, args) {
  91. if ( !notice ) return;
  92. var type = 'info';
  93. var title = $('<b>');
  94. var text = $('<div>');
  95. var note;
  96. switch (notice) {
  97. case 'unauthorized':
  98. type = 'info';
  99. title.text('Not logged in!');
  100. text.text('Please login before you can change any settings.');
  101. break;
  102. case 'save':
  103. type = 'success';
  104. title.text('Settings saved!');
  105. text.text('The settings have been updated successfully.');
  106. break;
  107. case 'logout':
  108. type = 'success';
  109. title.text('Successfully logged out!');
  110. text.text('You have been successfully logged out. To change any settings you need to login again.');
  111. break;
  112. case 'refresh':
  113. type = 'success';
  114. title.text('Refresh successful!');
  115. text.text('Your server list has been successfully refeshed.');
  116. break;
  117. case 'loginfail':
  118. type = 'error';
  119. title.text('Login failed!');
  120. text.text('An error occurred while logging you in, please try again.');
  121. break;
  122. case 'sysmessage':
  123. type = 'info';
  124. title.text('System message does not match!');
  125. text.text(`The page "MediaWiki:Custom-RcGcDw" need to be the server id "${args[0]}".`);
  126. note = $('<a target="_blank">').text(args[1]).attr('href', args[1]);
  127. break;
  128. case 'mwversion':
  129. type = 'error';
  130. title.text('Outdated MediaWiki version!');
  131. text.text(`Requires at least MediaWiki 1.30, found ${args[0]} on ${args[1]}.`);
  132. note = $('<a target="_blank">').text('https://www.mediawiki.org/wiki/MediaWiki_1.30').attr('href', 'https://www.mediawiki.org/wiki/MediaWiki_1.30');
  133. break;
  134. case 'nochange':
  135. type = 'info';
  136. title.text('Save failed!');
  137. text.text('The settings matched the current default settings.');
  138. break;
  139. case 'invalidusergroup':
  140. type = 'error';
  141. title.text('Invalid user group!');
  142. text.text('The user group name was too long or you provided too many.');
  143. break;
  144. case 'wikiblocked':
  145. type = 'error';
  146. title.text('Wiki is blocked!');
  147. text.text(`${args[0]} has been blocked from being added as a recent changes webhook.`);
  148. if ( args[1] ) note = $('<div>').text(`Reason: ${args[1]}`);
  149. break;
  150. case 'savefail':
  151. type = 'error';
  152. title.text('Save failed!');
  153. text.text('The settings could not be saved, please try again.');
  154. break;
  155. case 'movefail':
  156. type = 'info';
  157. title.text('Settings partially saved!');
  158. text.text('The settings have only been partially updated.');
  159. note = $('<div>').text('The webhook channel could not be changed!');
  160. break;
  161. case 'refreshfail':
  162. type = 'error';
  163. title.text('Refresh failed!');
  164. text.text('You server list could not be refreshed, please try again.');
  165. break;
  166. case 'readonly':
  167. type = 'info';
  168. title.text('Read-only database!');
  169. text.text('You can currently only view your settings, but not change them.');
  170. break;
  171. default:
  172. return;
  173. }
  174. return $(`<div class="notice notice-${type}">`).append(
  175. title,
  176. text,
  177. note
  178. ).appendTo('#text #notices');
  179. }
  180. const permissions = {
  181. ADMINISTRATOR: 1 << 3,
  182. MANAGE_CHANNELS: 1 << 4,
  183. MANAGE_GUILD: 1 << 5,
  184. ADD_REACTIONS: 1 << 6,
  185. VIEW_CHANNEL: 1 << 10,
  186. SEND_MESSAGES: 1 << 11,
  187. MANAGE_MESSAGES: 1 << 13,
  188. EMBED_LINKS: 1 << 14,
  189. ATTACH_FILES: 1 << 15,
  190. READ_MESSAGE_HISTORY: 1 << 16,
  191. USE_EXTERNAL_EMOJIS: 1 << 18,
  192. MANAGE_NICKNAMES: 1 << 27,
  193. MANAGE_ROLES: 1 << 28,
  194. MANAGE_WEBHOOKS: 1 << 29
  195. }
  196. /**
  197. * Check if a permission is included in the BitField
  198. * @param {String|Number} all - BitField of multiple permissions
  199. * @param {String[]} permission - Name of the permission to check for
  200. * @returns {Boolean}
  201. */
  202. function hasPerm(all = 0, ...permission) {
  203. if ( (all & permissions.ADMINISTRATOR) === permissions.ADMINISTRATOR ) return true;
  204. return permission.map( perm => {
  205. let bit = permissions[perm];
  206. return ( (all & bit) === bit );
  207. } ).every( perm => perm );
  208. }
  209. module.exports = {got, db, settingsData, sendMsg, createNotice, hasPerm};