util.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. if ( message === 'toggleDebug' ) global.isDebug = !global.isDebug;
  75. console.log( '- [Dashboard]: Message received!', message );
  76. } );
  77. /**
  78. * Send messages to the manager.
  79. * @param {Object} [message] - The message.
  80. * @returns {Promise<Object>}
  81. */
  82. function sendMsg(message) {
  83. var id = messageId++;
  84. var promise = new Promise( (resolve, reject) => {
  85. messages.set(id, {resolve, reject});
  86. process.send( {id, data: message} );
  87. } );
  88. return promise;
  89. }
  90. /**
  91. * Create a red notice
  92. * @param {import('cheerio')} $ - The cheerio static
  93. * @param {String} notice - The notice to create
  94. * @param {import('./i18n.js')} dashboardLang - The user language
  95. * @param {String[]} [args] - The arguments for the notice
  96. * @returns {import('cheerio')}
  97. */
  98. function createNotice($, notice, dashboardLang, args = []) {
  99. if ( !notice ) return;
  100. var type = 'info';
  101. var title = $('<b>');
  102. var text = $('<div>');
  103. var note;
  104. switch (notice) {
  105. case 'unauthorized':
  106. type = 'info';
  107. title.text(dashboardLang.get('notice.unauthorized.title'));
  108. text.text(dashboardLang.get('notice.unauthorized.text'));
  109. break;
  110. case 'save':
  111. type = 'success';
  112. title.text(dashboardLang.get('notice.save.title'));
  113. text.text(dashboardLang.get('notice.save.text'));
  114. break;
  115. case 'nosettings':
  116. type = 'info';
  117. title.text(dashboardLang.get('notice.nosettings.title'));
  118. text.text(dashboardLang.get('notice.nosettings.text'));
  119. note = $('<a>').text(dashboardLang.get('notice.nosettings.note')).attr('href', `/guild/${args[0]}/settings`);
  120. break;
  121. case 'logout':
  122. type = 'success';
  123. title.text(dashboardLang.get('notice.logout.title'));
  124. text.text(dashboardLang.get('notice.logout.text'));
  125. break;
  126. case 'refresh':
  127. type = 'success';
  128. title.text(dashboardLang.get('notice.refresh.title'));
  129. text.text(dashboardLang.get('notice.refresh.text'));
  130. break;
  131. case 'missingperm':
  132. type = 'error';
  133. title.text(dashboardLang.get('notice.missingperm.title'));
  134. text.html(dashboardLang.get('notice.missingperm.text', true, $('<code>').text(args[0])));
  135. break;
  136. case 'loginfail':
  137. type = 'error';
  138. title.text(dashboardLang.get('notice.loginfail.title'));
  139. text.text(dashboardLang.get('notice.loginfail.text'));
  140. break;
  141. case 'sysmessage':
  142. type = 'info';
  143. title.text(dashboardLang.get('notice.sysmessage.title'));
  144. text.html(dashboardLang.get('notice.sysmessage.text', true, $('<a target="_blank">').append(
  145. $('<code>').text('MediaWiki:Custom-RcGcDw')
  146. ).attr('href', args[1]), $('<code class="user-select">').text(args[0])));
  147. note = $('<a target="_blank">').text(args[1]).attr('href', args[1]);
  148. break;
  149. case 'mwversion':
  150. type = 'error';
  151. title.text(dashboardLang.get('notice.mwversion.title'));
  152. text.text(dashboardLang.get('notice.mwversion.text', false, args[0], args[1]));
  153. note = $('<a target="_blank">').text('https://www.mediawiki.org/wiki/MediaWiki_1.30').attr('href', 'https://www.mediawiki.org/wiki/MediaWiki_1.30');
  154. break;
  155. case 'nochange':
  156. type = 'info';
  157. title.text(dashboardLang.get('notice.nochange.title'));
  158. text.text(dashboardLang.get('notice.nochange.text'));
  159. break;
  160. case 'invalidusergroup':
  161. type = 'error';
  162. title.text(dashboardLang.get('notice.invalidusergroup.title'));
  163. text.text(dashboardLang.get('notice.invalidusergroup.text'));
  164. break;
  165. case 'wikiblocked':
  166. type = 'error';
  167. title.text(dashboardLang.get('notice.wikiblocked.title'));
  168. text.text(dashboardLang.get('notice.wikiblocked.text', false, args[0]));
  169. if ( args[1] ) note = $('<div>').append(
  170. dashboardLang.get('notice.wikiblocked.note', true) + ' ',
  171. $('<code>').text(args[1])
  172. );
  173. break;
  174. case 'savefail':
  175. type = 'error';
  176. title.text(dashboardLang.get('notice.savefail.title'));
  177. text.text(dashboardLang.get('notice.savefail.text'));
  178. if ( typeof args[0] === 'string' ) {
  179. note = $('<div>').text(dashboardLang.get('notice.savefail.note_' + args[0]));
  180. }
  181. break;
  182. case 'movefail':
  183. type = 'info';
  184. title.text(dashboardLang.get('notice.movefail.title'));
  185. text.text(dashboardLang.get('notice.movefail.text'));
  186. note = $('<div>').text(dashboardLang.get('notice.movefail.note'));
  187. break;
  188. case 'refreshfail':
  189. type = 'error';
  190. title.text(dashboardLang.get('notice.refreshfail.title'));
  191. text.text(dashboardLang.get('notice.refreshfail.text'));
  192. break;
  193. case 'error':
  194. type = 'error';
  195. title.text(dashboardLang.get('notice.error.title'));
  196. text.text(dashboardLang.get('notice.error.text'));
  197. break;
  198. case 'readonly':
  199. type = 'info';
  200. title.text(dashboardLang.get('notice.readonly.title'));
  201. text.text(dashboardLang.get('notice.readonly.text'));
  202. break;
  203. default:
  204. return;
  205. }
  206. return $(`<div class="notice notice-${type}">`).append(
  207. title,
  208. text,
  209. note
  210. ).appendTo('#text #notices');
  211. }
  212. /**
  213. * HTML escape text
  214. * @param {String} text - The text to escape
  215. * @returns {String}
  216. */
  217. function escapeText(text) {
  218. return text.replace( /&/g, '&amp;' ).replace( /</g, '&lt;' ).replace( />/g, '&gt;' );
  219. }
  220. const permissions = {
  221. ADMINISTRATOR: 1 << 3,
  222. MANAGE_CHANNELS: 1 << 4,
  223. MANAGE_GUILD: 1 << 5,
  224. ADD_REACTIONS: 1 << 6,
  225. VIEW_CHANNEL: 1 << 10,
  226. SEND_MESSAGES: 1 << 11,
  227. MANAGE_MESSAGES: 1 << 13,
  228. EMBED_LINKS: 1 << 14,
  229. ATTACH_FILES: 1 << 15,
  230. READ_MESSAGE_HISTORY: 1 << 16,
  231. MENTION_EVERYONE: 1 << 17,
  232. USE_EXTERNAL_EMOJIS: 1 << 18,
  233. MANAGE_NICKNAMES: 1 << 27,
  234. MANAGE_ROLES: 1 << 28,
  235. MANAGE_WEBHOOKS: 1 << 29
  236. }
  237. /**
  238. * Check if a permission is included in the BitField
  239. * @param {String|Number} all - BitField of multiple permissions
  240. * @param {String[]} permission - Name of the permission to check for
  241. * @returns {Boolean}
  242. */
  243. function hasPerm(all = 0, ...permission) {
  244. if ( (all & permissions.ADMINISTRATOR) === permissions.ADMINISTRATOR ) return true;
  245. return permission.map( perm => {
  246. let bit = permissions[perm];
  247. return ( (all & bit) === bit );
  248. } ).every( perm => perm );
  249. }
  250. module.exports = {got, db, settingsData, sendMsg, createNotice, escapeText, hasPerm};