functions.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. import htmlparser from 'htmlparser2';
  2. import gotDefault from 'got';
  3. const got = gotDefault.extend( {
  4. throwHttpErrors: false,
  5. timeout: {
  6. request: 5_000
  7. },
  8. headers: {
  9. 'User-Agent': 'Wiki-Bot/' + ( isDebug ? 'testing' : process.env.npm_package_version ) + ' (Discord; ' + process.env.npm_package_name + ( process.env.invite ? '; ' + process.env.invite : '' ) + ')'
  10. },
  11. responseType: 'json'
  12. } );
  13. /**
  14. * @type {Map<String, {state: String, wiki: String, channel: import('discord.js').TextChannel, user: String}>}
  15. */
  16. const oauthVerify = new Map();
  17. /**
  18. * Parse infobox content
  19. * @param {Object} infobox - The content of the infobox.
  20. * @param {import('discord.js').MessageEmbed} embed - The message embed.
  21. * @param {String} [thumbnail] - The default thumbnail for the wiki.
  22. * @param {String} [pagelink] - The article path for relative links.
  23. * @returns {import('discord.js').MessageEmbed?}
  24. */
  25. function parse_infobox(infobox, embed, thumbnail, pagelink = '') {
  26. if ( !infobox || embed.fields.length >= 25 || embed.length > 5400 ) return;
  27. if ( infobox.parser_tag_version === 5 ) {
  28. infobox.data.forEach( group => {
  29. parse_infobox(group, embed, thumbnail, pagelink);
  30. } );
  31. embed.fields = embed.fields.filter( (field, i, fields) => {
  32. if ( field.name !== '\u200b' || !field.value.startsWith( '__**' ) ) return true;
  33. return ( fields[i + 1]?.name && ( fields[i + 1].name !== '\u200b' || !fields[i + 1].value.startsWith( '__**' ) ) );
  34. } );
  35. return embed;
  36. }
  37. switch ( infobox.type ) {
  38. case 'data':
  39. var {label = '', value = '', source = '', 'item-name': name = ''} = infobox.data;
  40. label = htmlToPlain(label, true).trim();
  41. value = htmlToDiscord(value, pagelink).trim();
  42. if ( label.includes( '*UNKNOWN LINK*' ) ) {
  43. if ( !( source || name ) ) break;
  44. label = '`' + ( source || name ) + '`';
  45. embed.brokenInfobox = true;
  46. }
  47. if ( value.includes( '*UNKNOWN LINK*' ) ) {
  48. if ( !( source || name ) ) break;
  49. value = '`' + ( source || name ) + '`';
  50. embed.brokenInfobox = true;
  51. }
  52. if ( label.length > 100 ) label = label.substring(0, 100) + '\u2026';
  53. if ( value.length > 500 ) value = limitLength(value, 500, 250);
  54. if ( label && value ) embed.addField( label, value, true );
  55. break;
  56. case 'panel':
  57. var embedLength = embed.fields.length;
  58. infobox.data.value.forEach( group => {
  59. parse_infobox(group, embed, thumbnail, pagelink);
  60. } );
  61. embed.fields = embed.fields.filter( (field, i, fields) => {
  62. if ( i < embedLength || field.name !== '\u200b' ) return true;
  63. if ( !field.value.startsWith( '__**' ) ) return true;
  64. return ( fields[i + 1]?.name && fields[i + 1].name !== '\u200b' );
  65. } ).filter( (field, i, fields) => {
  66. if ( i < embedLength || field.name !== '\u200b' ) return true;
  67. if ( field.value.startsWith( '__**' ) ) return true;
  68. return ( fields[i + 1]?.name && ( fields[i + 1].name !== '\u200b' || !fields[i + 1].value.startsWith( '__**' ) ) );
  69. } );
  70. break;
  71. case 'section':
  72. var {label = ''} = infobox.data;
  73. label = htmlToPlain(label).trim();
  74. if ( label.length > 100 ) label = label.substring(0, 100) + '\u2026';
  75. if ( label ) embed.addField( '\u200b', '**' + label + '**', false );
  76. case 'group':
  77. infobox.data.value.forEach( group => {
  78. parse_infobox(group, embed, thumbnail, pagelink);
  79. } );
  80. break;
  81. case 'header':
  82. var {value = ''} = infobox.data;
  83. value = htmlToPlain(value).trim();
  84. if ( value.length > 100 ) value = value.substring(0, 100) + '\u2026';
  85. if ( value ) embed.addField( '\u200b', '__**' + value + '**__', false );
  86. break;
  87. case 'image':
  88. if ( embed.thumbnail?.url !== thumbnail ) return;
  89. var image = infobox.data.find( img => {
  90. return ( /^(?:https?:)?\/\//.test(img.url) && /\.(?:png|jpg|jpeg|gif)$/.test(img.name) );
  91. } );
  92. if ( image ) embed.setThumbnail( image.url.replace( /^(?:https?:)?\/\//, 'https://' ) );
  93. break;
  94. }
  95. }
  96. /**
  97. * Make wikitext formatting usage.
  98. * @param {String} [text] - The text to modify.
  99. * @param {Boolean} [showEmbed] - If the text is used in an embed.
  100. * @param {import('./wiki.js').default} [wiki] - The wiki.
  101. * @param {String} [title] - The page title.
  102. * @param {Boolean} [fullWikitext] - If the text can contain full wikitext.
  103. * @returns {String}
  104. */
  105. function toFormatting(text = '', showEmbed = false, wiki, title = '', fullWikitext = false) {
  106. if ( showEmbed ) return toMarkdown(text, wiki, title, fullWikitext);
  107. else return toPlaintext(text, fullWikitext);
  108. };
  109. /**
  110. * Turns wikitext formatting into markdown.
  111. * @param {String} [text] - The text to modify.
  112. * @param {import('./wiki.js').default} wiki - The wiki.
  113. * @param {String} [title] - The page title.
  114. * @param {Boolean} [fullWikitext] - If the text can contain full wikitext.
  115. * @returns {String}
  116. */
  117. function toMarkdown(text = '', wiki, title = '', fullWikitext = false) {
  118. text = text.replace( /[()\\]/g, '\\$&' );
  119. var link = null;
  120. var regex = /\[\[(?:([^\|\]]+)\|)?([^\]]+)\]\]([a-z]*)/g;
  121. while ( ( link = regex.exec(text) ) !== null ) {
  122. var pagetitle = ( link[1] || link[2] );
  123. var page = wiki.toLink(( /^[#\/]/.test(pagetitle) ? title + ( pagetitle.startsWith( '/' ) ? pagetitle : '' ) : pagetitle ), '', ( pagetitle.startsWith( '#' ) ? pagetitle.substring(1) : '' ), true);
  124. text = text.replaceSave( link[0], '[' + link[2] + link[3] + '](' + page + ')' );
  125. }
  126. if ( title !== '' ) {
  127. regex = /\/\*\s*([^\*]+?)\s*\*\/\s*(.)?/g;
  128. while ( ( link = regex.exec(text) ) !== null ) {
  129. text = text.replaceSave( link[0], '[→' + link[1] + '](' + wiki.toLink(title, '', link[1], true) + ')' + ( link[2] ? ': ' + link[2] : '' ) );
  130. }
  131. }
  132. if ( fullWikitext ) {
  133. regex = /\[(?:https?:)?\/\/([^ ]+) ([^\]]+)\]/g;
  134. while ( ( link = regex.exec(text) ) !== null ) {
  135. text = text.replaceSave( link[0], '[' + link[2] + '](https://' + link[1] + ')' );
  136. }
  137. return htmlToDiscord(text, '', true, true).replaceSave( /'''/g, '**' ).replaceSave( /''/g, '*' );
  138. }
  139. return escapeFormatting(text, true);
  140. };
  141. /**
  142. * Removes wikitext formatting.
  143. * @param {String} [text] - The text to modify.
  144. * @param {Boolean} [fullWikitext] - If the text can contain full wikitext.
  145. * @returns {String}
  146. */
  147. function toPlaintext(text = '', fullWikitext = false) {
  148. text = text.replace( /\[\[(?:[^\|\]]+\|)?([^\]]+)\]\]/g, '$1' ).replace( /\/\*\s*([^\*]+?)\s*\*\//g, '→$1:' );
  149. if ( fullWikitext ) {
  150. return htmlToDiscord( text.replace( /\[(?:https?:)?\/\/(?:[^ ]+) ([^\]]+)\]/g, '$1' ) );
  151. }
  152. else return escapeFormatting(text);
  153. };
  154. /**
  155. * Change HTML text to plain text.
  156. * @param {String} html - The text in HTML.
  157. * @returns {String}
  158. */
  159. function htmlToPlain(html, includeComments = false) {
  160. var text = '';
  161. var ignoredTag = '';
  162. var parser = new htmlparser.Parser( {
  163. onopentag: (tagname, attribs) => {
  164. let classes = ( attribs.class?.split(' ') ?? [] );
  165. if ( classes.includes( 'noexcerpt' ) || ( classes.includes( 'mw-collapsible' ) && classes.includes( 'mw-collapsed' ) )
  166. || ( attribs.style?.includes( 'display' ) && /(^|;)\s*display\s*:\s*none\s*(;|$)/.test(attribs.style) ) ) {
  167. ignoredTag = tagname;
  168. return;
  169. }
  170. if ( tagname === 'sup' && classes.includes( 'reference' ) ) ignoredTag = 'sup';
  171. if ( tagname === 'span' && classes.includes( 'smwttcontent' ) ) ignoredTag = 'span';
  172. if ( tagname === 'br' ) text += ' ';
  173. },
  174. ontext: (htmltext) => {
  175. if ( !ignoredTag ) {
  176. htmltext = htmltext.replace( /[\r\n\t ]+/g, ' ' );
  177. if ( /[\n ]$/.test(text) && htmltext.startsWith( ' ' ) ) htmltext = htmltext.replace( /^ +/, '' );
  178. text += escapeFormatting(htmltext);
  179. }
  180. },
  181. onclosetag: (tagname) => {
  182. if ( tagname === ignoredTag ) ignoredTag = '';
  183. },
  184. oncomment: (commenttext) => {
  185. if ( includeComments && /^(?:IW)?LINK'" \d+(?::\d+)?$/.test(commenttext) ) {
  186. text += '*UNKNOWN LINK*';
  187. }
  188. }
  189. } );
  190. parser.write( String(html) );
  191. parser.end();
  192. return text;
  193. };
  194. /**
  195. * Change HTML text to markdown text.
  196. * @param {String} html - The text in HTML.
  197. * @param {String} [pagelink] - The article path for relative links.
  198. * @param {Boolean[]} [escapeArgs] - Arguments for the escaping of text formatting.
  199. * @returns {String}
  200. */
  201. function htmlToDiscord(html, pagelink = '', ...escapeArgs) {
  202. var text = '';
  203. var code = false;
  204. var href = '';
  205. var ignoredTag = '';
  206. var syntaxhighlight = '';
  207. var listlevel = -1;
  208. var horizontalList = '';
  209. var parser = new htmlparser.Parser( {
  210. onopentag: (tagname, attribs) => {
  211. if ( ignoredTag || code ) return;
  212. let classes = ( attribs.class?.split(' ') ?? [] );
  213. if ( classes.includes( 'noexcerpt' ) || classes.includes( 'mw-empty-elt' ) || ( classes.includes( 'mw-collapsible' ) && classes.includes( 'mw-collapsed' ) )
  214. || ( attribs.style?.includes( 'display' ) && /(^|;)\s*display\s*:\s*none\s*(;|$)/.test(attribs.style) ) ) {
  215. ignoredTag = tagname;
  216. return;
  217. }
  218. if ( classes.includes( 'hlist' ) ) horizontalList = tagname;
  219. if ( tagname === 'sup' && classes.includes( 'reference' ) ) ignoredTag = 'sup';
  220. if ( tagname === 'span' && classes.includes( 'smwttcontent' ) ) ignoredTag = 'span';
  221. if ( tagname === 'code' ) {
  222. code = true;
  223. text += '`';
  224. }
  225. if ( tagname === 'pre' ) {
  226. code = true;
  227. text += '```' + syntaxhighlight + '\n';
  228. }
  229. if ( tagname === 'div' && classes.length ) {
  230. if ( classes.includes( 'mw-highlight' ) ) {
  231. syntaxhighlight = ( classes.find( syntax => syntax.startsWith( 'mw-highlight-lang-' ) )?.replace( 'mw-highlight-lang-', '' ) || '' );
  232. }
  233. }
  234. if ( tagname === 'b' || tagname === 'strong' ) text += '**';
  235. if ( tagname === 'i' ) text += '*';
  236. if ( tagname === 's' ) text += '~~';
  237. if ( tagname === 'u' ) text += '__';
  238. if ( tagname === 'br' ) {
  239. text += '\n';
  240. if ( listlevel > -1 ) text += '\u200b '.repeat(4 * listlevel + 3);
  241. }
  242. if ( tagname === 'hr' ) {
  243. text = text.replace( / +$/, '' );
  244. if ( !text.endsWith( '\n' ) ) text += '\n';
  245. text += '─'.repeat(10) + '\n';
  246. }
  247. if ( tagname === 'p' && !text.endsWith( '\n' ) ) text += '\n';
  248. if ( tagname === 'ul' || tagname === 'ol' || tagname === 'dl' ) {
  249. if ( ++listlevel ) text += ' (';
  250. }
  251. if ( tagname === 'li' && !horizontalList ) {
  252. text = text.replace( /[ \u200b]+$/, '' );
  253. if ( !text.endsWith( '\n' ) ) text += '\n';
  254. if ( listlevel > -1 ) text += '\u200b '.repeat(4 * listlevel);
  255. text += '• ';
  256. }
  257. if ( tagname === 'dt' && !horizontalList ) {
  258. text = text.replace( /[ \u200b]+$/, '' );
  259. if ( !text.endsWith( '\n' ) ) text += '\n';
  260. if ( listlevel > -1 ) text += '\u200b '.repeat(4 * listlevel);
  261. text += '**';
  262. }
  263. if ( tagname === 'dd' && !horizontalList ) {
  264. text = text.replace( /[ \u200b]+$/, '' );
  265. if ( !text.endsWith( '\n' ) ) text += '\n';
  266. if ( listlevel > -1 ) text += '\u200b '.repeat(4 * (listlevel + 1));
  267. }
  268. if ( tagname === 'img' ) {
  269. if ( attribs.alt && attribs.src ) {
  270. let showAlt = true;
  271. if ( attribs['data-image-name'] === attribs.alt ) showAlt = false;
  272. else {
  273. let regex = new RegExp( '/([\\da-f])/\\1[\\da-f]/' + attribs.alt.replace( / /g, '_' ).replace( /\W/g, '\\$&' ) + '(?:/|\\?|$)' );
  274. if ( attribs.src.startsWith( 'data:' ) && attribs['data-src'] ) attribs.src = attribs['data-src'];
  275. if ( regex.test(attribs.src.replace( /(?:%[\dA-F]{2})+/g, partialURIdecode )) ) showAlt = false;
  276. }
  277. if ( showAlt ) {
  278. if ( href && !code ) attribs.alt = attribs.alt.replace( /[\[\]]/g, '\\$&' );
  279. if ( code ) text += attribs.alt.replace( /`/g, 'ˋ' );
  280. else text += escapeFormatting(attribs.alt, ...escapeArgs);
  281. }
  282. }
  283. }
  284. if ( tagname === 'h1' ) {
  285. text = text.replace( / +$/, '' );
  286. if ( !text.endsWith( '\n' ) ) text += '\n';
  287. text += '***__';
  288. }
  289. if ( tagname === 'h2' ) {
  290. text = text.replace( / +$/, '' );
  291. if ( !text.endsWith( '\n' ) ) text += '\n';
  292. text += '**__';
  293. }
  294. if ( tagname === 'h3' ) {
  295. text = text.replace( / +$/, '' );
  296. if ( !text.endsWith( '\n' ) ) text += '\n';
  297. text += '**';
  298. }
  299. if ( tagname === 'h4' ) {
  300. text = text.replace( / +$/, '' );
  301. if ( !text.endsWith( '\n' ) ) text += '\n';
  302. text += '__';
  303. }
  304. if ( tagname === 'h5' ) {
  305. text = text.replace( / +$/, '' );
  306. if ( !text.endsWith( '\n' ) ) text += '\n';
  307. text += '*';
  308. }
  309. if ( tagname === 'h6' ) {
  310. text = text.replace( / +$/, '' );
  311. if ( !text.endsWith( '\n' ) ) text += '\n';
  312. text += '';
  313. }
  314. if ( !pagelink ) return;
  315. if ( tagname === 'a' && attribs.href && !classes.includes( 'new' ) && /^(?:(?:https?:)?\/\/|\/|#)/.test(attribs.href) ) {
  316. href = new URL(attribs.href, pagelink).href.replace( /[()]/g, '\\$&' );
  317. if ( text.endsWith( '](<' + href + '>)' ) ) {
  318. text = text.substring(0, text.length - ( href.length + 5 ));
  319. }
  320. else text += '[';
  321. }
  322. },
  323. ontext: (htmltext) => {
  324. if ( !ignoredTag ) {
  325. if ( href && !code ) htmltext = htmltext.replace( /[\[\]]/g, '\\$&' );
  326. if ( code ) text += htmltext.replace( /`/g, 'ˋ' );
  327. else {
  328. htmltext = htmltext.replace( /[\r\n\t ]+/g, ' ' );
  329. if ( /[\n ]$/.test(text) && htmltext.startsWith( ' ' ) ) {
  330. htmltext = htmltext.replace( /^ +/, '' );
  331. }
  332. text += escapeFormatting(htmltext, ...escapeArgs);
  333. }
  334. }
  335. },
  336. onclosetag: (tagname) => {
  337. if ( tagname === ignoredTag ) {
  338. ignoredTag = '';
  339. return;
  340. }
  341. if ( code ) {
  342. if ( tagname === 'code' ) {
  343. code = false;
  344. text += '`';
  345. }
  346. if ( tagname === 'pre' ) {
  347. code = false;
  348. text += '\n```';
  349. }
  350. return;
  351. }
  352. if ( syntaxhighlight && tagname === 'div' ) syntaxhighlight = '';
  353. if ( tagname === 'b' || tagname === 'strong' ) text += '**';
  354. if ( tagname === 'i' ) text += '*';
  355. if ( tagname === 's' ) text += '~~';
  356. if ( tagname === 'u' ) text += '__';
  357. if ( tagname === 'dl' && horizontalList ) text = text.replace( /: $/, '' );
  358. if ( tagname === 'ul' || tagname === 'ol' || tagname === 'dl' ) {
  359. if ( horizontalList ) text = text.replace( / • $/, '' );
  360. if ( listlevel-- ) text += ')';
  361. }
  362. if ( ( tagname === 'li' || tagname === 'dd' ) && horizontalList ) text += ' • ';
  363. if ( tagname === 'dt' ) {
  364. text += '**';
  365. if ( horizontalList ) text += ': ';
  366. }
  367. if ( tagname === horizontalList ) horizontalList = '';
  368. if ( tagname === 'h1' ) text += '__***';
  369. if ( tagname === 'h2' ) text += '__**';
  370. if ( tagname === 'h3' ) text += '**';
  371. if ( tagname === 'h4' ) text += '__';
  372. if ( tagname === 'h5' ) text += '*';
  373. if ( tagname === 'h6' ) text += '';
  374. if ( !pagelink ) return;
  375. if ( tagname === 'a' && href ) {
  376. if ( text.endsWith( '[' ) ) text = text.substring(0, text.length - 1);
  377. else text += '](<' + href + '>)';
  378. href = '';
  379. }
  380. },
  381. oncomment: (commenttext) => {
  382. if ( pagelink && /^(?:IW)?LINK'" \d+(?::\d+)?$/.test(commenttext) ) {
  383. text += '*UNKNOWN LINK*';
  384. }
  385. }
  386. } );
  387. parser.write( String(html) );
  388. parser.end();
  389. return text;
  390. };
  391. /**
  392. * Escapes formatting.
  393. * @param {String} [text] - The text to modify.
  394. * @param {Boolean} [isMarkdown] - The text contains markdown links.
  395. * @param {Boolean} [keepLinks] - Don't escape non-markdown links.
  396. * @returns {String}
  397. */
  398. function escapeFormatting(text = '', isMarkdown = false, keepLinks = false) {
  399. if ( !isMarkdown ) text = text.replace( /\\/g, '\\\\' ).replace( /\]\(/g, ']\\(' );
  400. text = text.replace( /[`_*~:<>{}@|]/g, '\\$&' ).replace( /\/\//g, '/\\/' );
  401. if ( keepLinks ) text = text.replace( /(?:\\<)?https?\\:\/\\\/(?:[^\(\)\s]+(?=\))|[^\[\]\s]+(?=\])|[^<>\s]+>?)/g, match => {
  402. return match.replace( /\\\\/g, '/' ).replace( /\\/g, '' );
  403. } );
  404. return text;
  405. };
  406. /**
  407. * Limit text length without breaking link formatting.
  408. * @param {String} [text] - The text to modify.
  409. * @param {Number} [limit] - The character limit.
  410. * @param {Number} [maxExtra] - The maximal allowed character limit if needed.
  411. * @returns {String}
  412. */
  413. function limitLength(text = '', limit = 1000, maxExtra = 20) {
  414. var suffix = '\u2026';
  415. var link = null;
  416. var regex = /(?<!\\)\[((?:[^\[\]]|\\[\[\]])*?[^\\])\]\(<?(?:[^()]|\\[()])+?[^\\]>?\)/g;
  417. while ( ( link = regex.exec(text) ) !== null ) {
  418. if ( link.index < limit && link.index + link[0].length > limit ) {
  419. if ( link.index + link[0].length < limit + maxExtra ) suffix = link[0];
  420. else if ( link.index + link[1].length < limit + maxExtra ) suffix = link[1];
  421. if ( link.index + link[0].length < text.length ) suffix += '\u2026';
  422. limit = link.index;
  423. break;
  424. }
  425. else if ( link.index >= limit ) break;
  426. }
  427. return text.substring(0, limit) + suffix;
  428. };
  429. /**
  430. * Try to URI decode.
  431. * @param {String} m - The character to decode.
  432. * @returns {String}
  433. */
  434. function partialURIdecode(m) {
  435. var text = '';
  436. try {
  437. text = decodeURIComponent( m );
  438. }
  439. catch ( replaceError ) {
  440. if ( isDebug ) console.log( '- Failed to decode ' + m + ':' + replaceError );
  441. text = m;
  442. }
  443. return text;
  444. };
  445. /**
  446. * Check for timeout or pause.
  447. * @param {import('discord.js').Message|import('discord.js').Interaction} msg - The message.
  448. * @param {Boolean} [ignorePause] - Ignore pause for admins.
  449. * @returns {Boolean}
  450. */
  451. function breakOnTimeoutPause(msg, ignorePause = false) {
  452. if ( !msg.inGuild() ) return false;
  453. if ( msg.member?.isCommunicationDisabled() ) {
  454. console.log( '- Aborted, communication disabled for User.' );
  455. return true;
  456. }
  457. if ( msg.guild?.me.isCommunicationDisabled() ) {
  458. console.log( '- Aborted, communication disabled for Wiki-Bot.' );
  459. return true;
  460. }
  461. if ( pausedGuilds.has(msg.guildId) && !( ignorePause && ( msg.isAdmin() || msg.isOwner() ) ) ) {
  462. console.log( '- Aborted, guild paused.' );
  463. return true;
  464. };
  465. return false;
  466. };
  467. /**
  468. * Allow users to delete their command responses.
  469. * @param {import('discord.js').Message} msg - The response.
  470. * @param {String} author - The user id.
  471. */
  472. function allowDelete(msg, author) {
  473. msg?.awaitReactions?.( {
  474. filter: (reaction, user) => ( reaction.emoji.name === '🗑️' && user.id === author ),
  475. max: 1, time: 300_000
  476. } ).then( reaction => {
  477. if ( reaction.size ) msg.delete().catch(log_error);
  478. } );
  479. };
  480. /**
  481. * Sends an interaction response.
  482. * @param {import('discord.js').CommandInteraction|import('discord.js').ButtonInteraction} interaction - The interaction.
  483. * @param {import('discord.js').MessageOptions} message - The message.
  484. * @param {Boolean} [letDelete] - Let the interaction user delete the message.
  485. * @returns {Promise<import('discord.js').Message?>}
  486. */
  487. function sendMessage(interaction, message, letDelete = true) {
  488. if ( !interaction.ephemeral && letDelete && breakOnTimeoutPause(interaction) ) return Promise.resolve();
  489. if ( message?.embeds?.length && !message.embeds[0] ) message.embeds = [];
  490. return interaction.editReply( message ).then( msg => {
  491. if ( letDelete && (msg.flags & 64) !== 64 ) allowDelete(msg, interaction.user.id);
  492. return msg;
  493. }, log_error );
  494. };
  495. export {
  496. got,
  497. oauthVerify,
  498. parse_infobox,
  499. toFormatting,
  500. toMarkdown,
  501. toPlaintext,
  502. htmlToPlain,
  503. htmlToDiscord,
  504. escapeFormatting,
  505. limitLength,
  506. partialURIdecode,
  507. breakOnTimeoutPause,
  508. allowDelete,
  509. sendMessage
  510. };