util.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 {Pool} = require('pg');
  10. const db = new Pool();
  11. db.on( 'error', dberror => {
  12. console.log( '- Dashboard: Error while connecting to the database: ' + dberror );
  13. } );
  14. /**
  15. * @typedef Settings
  16. * @property {String} state
  17. * @property {String} access_token
  18. * @property {User} user
  19. * @property {Object} guilds
  20. * @property {Number} guilds.count
  21. * @property {Map<String, Guild>} guilds.isMember
  22. * @property {Map<String, Guild>} guilds.notMember
  23. */
  24. /**
  25. * @typedef User
  26. * @property {String} id
  27. * @property {String} username
  28. * @property {String} discriminator
  29. * @property {String} avatar
  30. * @property {String} locale
  31. */
  32. /**
  33. * @typedef Guild
  34. * @property {String} id
  35. * @property {String} name
  36. * @property {String} acronym
  37. * @property {String} [icon]
  38. * @property {String} userPermissions
  39. * @property {Boolean} [patreon]
  40. * @property {String} [botPermissions]
  41. * @property {Channel[]} [channels]
  42. * @property {Role[]} [roles]
  43. * @property {String} [locale]
  44. */
  45. /**
  46. * @typedef Channel
  47. * @property {String} id
  48. * @property {String} name
  49. * @property {Boolean} isCategory
  50. * @property {Number} userPermissions
  51. * @property {Number} botPermissions
  52. */
  53. /**
  54. * @typedef Role
  55. * @property {String} id
  56. * @property {String} name
  57. * @property {Boolean} lower
  58. */
  59. /**
  60. * @type {Map<String, Settings>}
  61. */
  62. const settingsData = new Map();
  63. /**
  64. * @type {Map<Number, PromiseConstructor>}
  65. */
  66. const messages = new Map();
  67. var messageId = 1;
  68. process.on( 'message', message => {
  69. if ( message.id ) {
  70. if ( message.data.error ) messages.get(message.id).reject(message.data.error);
  71. else messages.get(message.id).resolve(message.data.response);
  72. return messages.delete(message.id);
  73. }
  74. console.log( '- [Dashboard]: Message received!', message );
  75. } );
  76. /**
  77. * Send messages to the manager.
  78. * @param {Object} [message] - The message.
  79. * @returns {Promise<Object>}
  80. */
  81. function sendMsg(message) {
  82. var id = messageId++;
  83. var promise = new Promise( (resolve, reject) => {
  84. messages.set(id, {resolve, reject});
  85. process.send( {id, data: message} );
  86. } );
  87. return promise;
  88. }
  89. /**
  90. * Create a red notice
  91. * @param {import('cheerio')} $ - The cheerio static
  92. * @param {String} notice - The notice to create
  93. * @param {import('./i18n.js')} dashboardLang - The user language
  94. * @param {String[]} [args] - The arguments for the notice
  95. * @returns {import('cheerio')}
  96. */
  97. function createNotice($, notice, dashboardLang, args = []) {
  98. if ( !notice ) return;
  99. var type = 'info';
  100. var title = $('<b>');
  101. var text = $('<div>');
  102. var note;
  103. switch (notice) {
  104. case 'unauthorized':
  105. type = 'info';
  106. title.text(dashboardLang.get('notice.unauthorized.title'));
  107. text.text(dashboardLang.get('notice.unauthorized.text'));
  108. break;
  109. case 'save':
  110. type = 'success';
  111. title.text(dashboardLang.get('notice.save.title'));
  112. text.text(dashboardLang.get('notice.save.text'));
  113. break;
  114. case 'nosettings':
  115. type = 'info';
  116. title.text(dashboardLang.get('notice.nosettings.title'));
  117. text.text(dashboardLang.get('notice.nosettings.text'));
  118. note = $('<a>').text(dashboardLang.get('notice.nosettings.note')).attr('href', `/guild/${args[0]}/settings`);
  119. break;
  120. case 'logout':
  121. type = 'success';
  122. title.text(dashboardLang.get('notice.logout.title'));
  123. text.text(dashboardLang.get('notice.logout.text'));
  124. break;
  125. case 'refresh':
  126. type = 'success';
  127. title.text(dashboardLang.get('notice.refresh.title'));
  128. text.text(dashboardLang.get('notice.refresh.text'));
  129. break;
  130. case 'missingperm':
  131. type = 'error';
  132. title.text(dashboardLang.get('notice.missingperm.title'));
  133. text.html(dashboardLang.get('notice.missingperm.text', true, $('<code>').text(args[0])));
  134. break;
  135. case 'loginfail':
  136. type = 'error';
  137. title.text(dashboardLang.get('notice.loginfail.title'));
  138. text.text(dashboardLang.get('notice.loginfail.text'));
  139. break;
  140. case 'sysmessage':
  141. type = 'info';
  142. title.text(dashboardLang.get('notice.sysmessage.title'));
  143. text.html(dashboardLang.get('notice.sysmessage.text', true, $('<a target="_blank">').append(
  144. $('<code>').text('MediaWiki:Custom-RcGcDw')
  145. ).attr('href', args[1]), $('<code class="user-select">').text(args[0])));
  146. note = $('<a target="_blank">').text(args[1]).attr('href', args[1]);
  147. break;
  148. case 'mwversion':
  149. type = 'error';
  150. title.text(dashboardLang.get('notice.mwversion.title'));
  151. text.text(dashboardLang.get('notice.mwversion.text', false, args[0], args[1]));
  152. note = $('<a target="_blank">').text('https://www.mediawiki.org/wiki/MediaWiki_1.30').attr('href', 'https://www.mediawiki.org/wiki/MediaWiki_1.30');
  153. break;
  154. case 'nochange':
  155. type = 'info';
  156. title.text(dashboardLang.get('notice.nochange.title'));
  157. text.text(dashboardLang.get('notice.nochange.text'));
  158. break;
  159. case 'invalidusergroup':
  160. type = 'error';
  161. title.text(dashboardLang.get('notice.invalidusergroup.title'));
  162. text.text(dashboardLang.get('notice.invalidusergroup.text'));
  163. break;
  164. case 'wikiblocked':
  165. type = 'error';
  166. title.text(dashboardLang.get('notice.wikiblocked.title'));
  167. text.text(dashboardLang.get('notice.wikiblocked.text', false, args[0]));
  168. if ( args[1] ) note = $('<div>').append(
  169. dashboardLang.get('notice.wikiblocked.note', true) + ' ',
  170. $('<code>').text(args[1])
  171. );
  172. break;
  173. case 'savefail':
  174. type = 'error';
  175. title.text(dashboardLang.get('notice.savefail.title'));
  176. text.text(dashboardLang.get('notice.savefail.text'));
  177. if ( typeof args[0] === 'string' ) {
  178. note = $('<div>').text(dashboardLang.get('notice.savefail.note_' + args[0]));
  179. }
  180. break;
  181. case 'movefail':
  182. type = 'info';
  183. title.text(dashboardLang.get('notice.movefail.title'));
  184. text.text(dashboardLang.get('notice.movefail.text'));
  185. note = $('<div>').text(dashboardLang.get('notice.movefail.note'));
  186. break;
  187. case 'refreshfail':
  188. type = 'error';
  189. title.text(dashboardLang.get('notice.refreshfail.title'));
  190. text.text(dashboardLang.get('notice.refreshfail.text'));
  191. break;
  192. case 'error':
  193. type = 'error';
  194. title.text(dashboardLang.get('notice.error.title'));
  195. text.text(dashboardLang.get('notice.error.text'));
  196. break;
  197. case 'readonly':
  198. type = 'info';
  199. title.text(dashboardLang.get('notice.readonly.title'));
  200. text.text(dashboardLang.get('notice.readonly.text'));
  201. break;
  202. default:
  203. return;
  204. }
  205. return $(`<div class="notice notice-${type}">`).append(
  206. title,
  207. text,
  208. note
  209. ).appendTo('#text #notices');
  210. }
  211. /**
  212. * HTML escape text
  213. * @param {String} text - The text to escape
  214. * @returns {String}
  215. */
  216. function escapeText(text) {
  217. return text.replace( /&/g, '&amp;' ).replace( /</g, '&lt;' ).replace( />/g, '&gt;' );
  218. }
  219. const permissions = {
  220. ADMINISTRATOR: 1 << 3,
  221. MANAGE_CHANNELS: 1 << 4,
  222. MANAGE_GUILD: 1 << 5,
  223. ADD_REACTIONS: 1 << 6,
  224. VIEW_CHANNEL: 1 << 10,
  225. SEND_MESSAGES: 1 << 11,
  226. MANAGE_MESSAGES: 1 << 13,
  227. EMBED_LINKS: 1 << 14,
  228. ATTACH_FILES: 1 << 15,
  229. READ_MESSAGE_HISTORY: 1 << 16,
  230. MENTION_EVERYONE: 1 << 17,
  231. USE_EXTERNAL_EMOJIS: 1 << 18,
  232. MANAGE_NICKNAMES: 1 << 27,
  233. MANAGE_ROLES: 1 << 28,
  234. MANAGE_WEBHOOKS: 1 << 29
  235. }
  236. /**
  237. * Check if a permission is included in the BitField
  238. * @param {String|Number} all - BitField of multiple permissions
  239. * @param {String[]} permission - Name of the permission to check for
  240. * @returns {Boolean}
  241. */
  242. function hasPerm(all = 0, ...permission) {
  243. if ( (all & permissions.ADMINISTRATOR) === permissions.ADMINISTRATOR ) return true;
  244. return permission.map( perm => {
  245. let bit = permissions[perm];
  246. return ( (all & bit) === bit );
  247. } ).every( perm => perm );
  248. }
  249. module.exports = {got, db, settingsData, sendMsg, createNotice, escapeText, hasPerm};