globals.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { inspect } from 'util';
  2. inspect.defaultOptions = {compact: false, breakLength: Infinity};
  3. /**
  4. * If debug logging is enabled.
  5. * @type {Boolean}
  6. * @global
  7. */
  8. globalThis.isDebug = ( process.argv[2] === 'debug' );
  9. /**
  10. * Prefix of guilds with patreon features enabled.
  11. * @type {Map<String, String>}
  12. * @global
  13. */
  14. globalThis.patreonGuildsPrefix = new Map();
  15. /**
  16. * Language code of guilds with voice channel role enabled.
  17. * @type {Map<String, String>}
  18. * @global
  19. */
  20. globalThis.voiceGuildsLang = new Map();
  21. /**
  22. * Guilds with pause activated.
  23. * @type {Set<String>}
  24. * @global
  25. */
  26. globalThis.pausedGuilds = new Set();
  27. /**
  28. * Logs an error.
  29. * @param {Error} error - The error.
  30. * @param {Boolean} isBig - If the error should get a big log.
  31. * @param {String} type - Type of the error.
  32. * @global
  33. */
  34. globalThis.log_error = function(error, isBig = false, type = '') {
  35. var time = new Date(Date.now()).toLocaleTimeString('de-DE', { timeZone: 'Europe/Berlin' });
  36. if ( isDebug ) {
  37. console.error( '--- ' + type + 'ERROR START ' + time + ' ---\n', error, '\n--- ' + type + 'ERROR END ' + time + ' ---' );
  38. } else {
  39. if ( isBig ) console.log( '--- ' + type + 'ERROR: ' + time + ' ---\n-', error );
  40. else console.log( '- ' + error.name + ': ' + error.message );
  41. }
  42. }
  43. const common_warnings = {
  44. main: [
  45. 'Unrecognized parameters: piprop, explaintext, exsectionformat, exlimit.',
  46. 'Unrecognized parameters: explaintext, exsectionformat, exlimit.',
  47. 'Unrecognized parameter: piprop.'
  48. ],
  49. query: [
  50. 'Unrecognized values for parameter "prop": pageimages, extracts.',
  51. 'Unrecognized values for parameter "prop": pageimages, extracts',
  52. 'Unrecognized value for parameter "prop": extracts.',
  53. 'Unrecognized value for parameter "prop": extracts',
  54. 'Unrecognized value for parameter "prop": pageimages.',
  55. 'Unrecognized value for parameter "prop": pageimages'
  56. ]
  57. }
  58. /**
  59. * Logs a warning.
  60. * @param {Object} warning - The warning.
  61. * @param {Boolean} api - If the warning is from the MediaWiki API.
  62. * @global
  63. */
  64. globalThis.log_warning = function(warning, api = true) {
  65. if ( isDebug ) {
  66. console.warn( '--- Warning start ---\n' + inspect( warning ) + '\n--- Warning end ---' );
  67. }
  68. else if ( api ) {
  69. if ( common_warnings.main.includes( warning?.main?.['*'] ) ) delete warning.main;
  70. if ( common_warnings.query.includes( warning?.query?.['*'] ) ) delete warning.query;
  71. var warningKeys = Object.keys(warning);
  72. if ( warningKeys.length ) console.warn( '- Warning: ' + warningKeys.join(', ') );
  73. }
  74. else console.warn( '--- Warning ---\n' + inspect( warning ) );
  75. }
  76. if ( !globalThis.verifyOauthUser ) {
  77. /**
  78. * Oauth wiki user verification.
  79. * @param {String} state - Unique state for the authorization.
  80. * @param {String} access_token - Access token.
  81. * @param {Object} [settings] - Settings to skip oauth.
  82. * @param {import('discord.js').TextChannel} settings.channel - The channel.
  83. * @param {String} settings.user - The user id.
  84. * @param {String} settings.wiki - The OAuth2 wiki.
  85. * @param {import('discord.js').CommandInteraction|import('discord.js').ButtonInteraction} [settings.interaction] - The interaction.
  86. * @param {Function} [settings.fail] - The function to call when the verifiction errors.
  87. * @param {import('discord.js').Message} [settings.sourceMessage] - The source message with the command.
  88. * @global
  89. */
  90. globalThis.verifyOauthUser = function(state, access_token, settings) {
  91. return settings?.fail?.();
  92. };
  93. }