verify.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. const {randomBytes} = require('crypto');
  2. var db = require('../util/database.js');
  3. var verify = require('../functions/verify.js');
  4. const {oauthVerify, allowDelete, escapeFormatting} = require('../util/functions.js');
  5. /**
  6. * Processes the "verify" command.
  7. * @param {import('../util/i18n.js')} lang - The user language.
  8. * @param {import('discord.js').Message} msg - The Discord message.
  9. * @param {String[]} args - The command arguments.
  10. * @param {String} line - The command as plain text.
  11. * @param {import('../util/wiki.js')} wiki - The wiki for the message.
  12. */
  13. function cmd_verify(lang, msg, args, line, wiki) {
  14. if ( !msg.channel.isGuild() || msg.defaultSettings ) return this.LINK(lang, msg, line, wiki);
  15. if ( !msg.guild.me.permissions.has('MANAGE_ROLES') ) {
  16. if ( msg.isAdmin() ) {
  17. console.log( msg.guild.id + ': Missing permissions - MANAGE_ROLES' );
  18. msg.replyMsg( lang.get('general.missingperm') + ' `MANAGE_ROLES`' );
  19. }
  20. else if ( !msg.onlyVerifyCommand ) this.LINK(lang, msg, line, wiki);
  21. return;
  22. }
  23. db.query( 'SELECT role, editcount, postcount, usergroup, accountage, rename FROM verification WHERE guild = $1 AND channel LIKE $2 ORDER BY configid ASC', [msg.guild.id, '%|' + msg.channel.id + '|%'] ).then( ({rows}) => {
  24. if ( !rows.length ) {
  25. if ( msg.onlyVerifyCommand ) return;
  26. return msg.replyMsg( lang.get('verify.missing') + ( msg.isAdmin() ? '\n`' + ( patreons[msg.guild.id] || process.env.prefix ) + 'verification`' : '' ) );
  27. }
  28. if ( wiki.hasOAuth2() && process.env.dashboard ) {
  29. let oauth = [wiki.hostname + wiki.pathname.slice(0, -1)];
  30. if ( wiki.isWikimedia() ) oauth.push('wikimedia');
  31. if ( wiki.isMiraheze() ) oauth.push('miraheze');
  32. if ( process.env['oauth_' + ( oauth[1] || oauth[0] )] && process.env['oauth_' + ( oauth[1] || oauth[0] ) + '_secret'] ) {
  33. let state = `${oauth[0]} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex') + ( oauth[1] ? ` ${oauth[1]}` : '' );
  34. while ( oauthVerify.has(state) ) {
  35. state = `${oauth[0]} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex') + ( oauth[1] ? ` ${oauth[1]}` : '' );
  36. }
  37. oauthVerify.set(state, {
  38. state, wiki: wiki.href,
  39. channel: msg.channel,
  40. user: msg.author.id
  41. });
  42. msg.client.shard.send({id: 'verifyUser', state});
  43. let oauthURL = wiki + 'rest.php/oauth2/authorize?' + new URLSearchParams({
  44. response_type: 'code', redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
  45. client_id: process.env['oauth_' + ( oauth[1] || oauth[0] )], state
  46. }).toString();
  47. return msg.member.send( lang.get('verify.oauth_message_dm', escapeFormatting(msg.guild.name)) + '\n<' + oauthURL + '>', {
  48. components: [
  49. {
  50. type: 1,
  51. components: [
  52. {
  53. type: 2,
  54. style: 5,
  55. label: lang.get('verify.oauth_button'),
  56. emoji: {id: null, name: '🔗'},
  57. url: oauthURL,
  58. disabled: false
  59. }
  60. ]
  61. }
  62. ]
  63. } ).then( message => {
  64. msg.reactEmoji('📩');
  65. allowDelete(message, msg.author.id);
  66. }, error => {
  67. if ( error?.code === 50007 ) { // CANNOT_MESSAGE_USER
  68. return msg.replyMsg( lang.get('verify.oauth_private') );
  69. }
  70. log_error(error);
  71. msg.reactEmoji('error');
  72. } );
  73. }
  74. }
  75. var username = args.join(' ').replace( /_/g, ' ' ).trim().replace( /^<\s*(.*)\s*>$/, '$1' ).replace( /^@/, '' ).split('#')[0].substring(0, 250).trim();
  76. if ( /^(?:https?:)?\/\/([a-z\d-]{1,50})\.(?:gamepedia\.com\/|(?:fandom\.com|wikia\.org)\/(?:[a-z-]{1,8}\/)?(?:wiki\/)?)/.test(username) ) {
  77. username = decodeURIComponent( username.replace( /^(?:https?:)?\/\/([a-z\d-]{1,50})\.(?:gamepedia\.com\/|(?:fandom\.com|wikia\.org)\/(?:[a-z-]{1,8}\/)?(?:wiki\/)?)/, '' ) );
  78. }
  79. if ( wiki.isGamepedia() ) username = username.replace( /^userprofile\s*:\s*/i, '' );
  80. if ( !username.trim() ) {
  81. args[0] = line.split(' ')[0];
  82. if ( args[0] === 'verification' ) args[0] = ( lang.localNames.verify || 'verify' );
  83. return this.help(lang, msg, args, line, wiki);
  84. }
  85. msg.reactEmoji('⏳').then( reaction => {
  86. verify(lang, msg.channel, msg.member, username, wiki, rows).then( result => {
  87. if ( result.oauth ) {
  88. let state = `${result.oauth[0]} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex') + ( result.oauth[1] ? ` ${result.oauth[1]}` : '' );
  89. while ( oauthVerify.has(state) ) {
  90. state = `${result.oauth[0]} ${global.shardId}` + Date.now().toString(16) + randomBytes(16).toString('hex') + ( result.oauth[1] ? ` ${result.oauth[1]}` : '' );
  91. }
  92. oauthVerify.set(state, {
  93. state, wiki: wiki.href,
  94. channel: msg.channel,
  95. user: msg.author.id
  96. });
  97. msg.client.shard.send({id: 'verifyUser', state});
  98. let oauthURL = wiki + 'rest.php/oauth2/authorize?' + new URLSearchParams({
  99. response_type: 'code', redirect_uri: new URL('/oauth/mw', process.env.dashboard).href,
  100. client_id: process.env['oauth_' + ( result.oauth[1] || result.oauth[0] )], state
  101. }).toString();
  102. msg.member.send( lang.get('verify.oauth_message_dm', escapeFormatting(msg.guild.name)) + '\n<' + oauthURL + '>', {
  103. components: [
  104. {
  105. type: 1,
  106. components: [
  107. {
  108. type: 2,
  109. style: 5,
  110. label: lang.get('verify.oauth_button'),
  111. emoji: {id: null, name: '🔗'},
  112. url: oauthURL,
  113. disabled: false
  114. }
  115. ]
  116. }
  117. ]
  118. } ).then( message => {
  119. msg.reactEmoji('📩');
  120. allowDelete(message, msg.author.id);
  121. }, error => {
  122. if ( error?.code === 50007 ) { // CANNOT_MESSAGE_USER
  123. return msg.replyMsg( lang.get('verify.oauth_private') );
  124. }
  125. log_error(error);
  126. msg.reactEmoji('error');
  127. } );
  128. }
  129. else if ( result.reaction ) msg.reactEmoji(result.reaction);
  130. else {
  131. var options = {embed: result.embed, components: []};
  132. if ( result.add_button ) options.components.push({
  133. type: 1,
  134. components: [
  135. {
  136. type: 2,
  137. style: 1,
  138. label: lang.get('verify.button_again'),
  139. emoji: {id: null, name: '🔂'},
  140. custom_id: 'verify_again',
  141. disabled: false
  142. }
  143. ]
  144. });
  145. msg.replyMsg( result.content, options, false, false ).then( message => {
  146. if ( !result.logging.channel || !msg.guild.channels.cache.has(result.logging.channel) ) return;
  147. if ( message ) {
  148. if ( result.logging.embed ) result.logging.embed.addField(message.url, '<#' + msg.channel.id + '>');
  149. else result.logging.content += '\n<#' + msg.channel.id + '> – <' + message.url + '>';
  150. }
  151. msg.guild.channels.cache.get(result.logging.channel).send(result.logging.content, {
  152. embed: result.logging.embed,
  153. allowedMentions: {parse: []}
  154. }).catch(log_error);
  155. } );
  156. }
  157. if ( reaction ) reaction.removeEmoji();
  158. }, error => {
  159. console.log( '- Error during the verifications: ' + error );
  160. msg.replyMsg( lang.get('verify.error_reply'), {}, false, false ).then( message => {
  161. if ( message ) message.reactEmoji('error');
  162. } );
  163. } );
  164. } );
  165. }, dberror => {
  166. console.log( '- Error while getting the verifications: ' + dberror );
  167. msg.replyMsg( lang.get('verify.error_reply'), {}, false, false ).then( message => {
  168. if ( message ) message.reactEmoji('error');
  169. } );
  170. } );
  171. }
  172. module.exports = {
  173. name: 'verify',
  174. everyone: true,
  175. pause: false,
  176. owner: false,
  177. run: cmd_verify
  178. };