util.js 7.9 KB

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