verify.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var db = require('../util/database.js');
  2. var verify = require('../functions/verify.js');
  3. const {sendMessage} = require('../util/functions.js');
  4. /**
  5. * Post a message with inline wiki links.
  6. * @param {Object} interaction - The interaction.
  7. * @param {import('discord.js').Client} interaction.client - The client of the interaction.
  8. * @param {import('../util/i18n.js')} lang - The user language.
  9. * @param {import('../util/wiki.js')} wiki - The wiki for the interaction.
  10. * @param {import('discord.js').TextChannel} [channel] - The channel for the interaction.
  11. */
  12. function slash_verify(interaction, lang, wiki, channel) {
  13. var reply = '<@' + ( interaction.member?.nick ? '!' : '' ) + interaction.user.id + '>, ';
  14. var allowed_mentions = {
  15. users: [interaction.user.id]
  16. };
  17. if ( !channel?.guild ) return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  18. data: {
  19. type: 4,
  20. data: {
  21. content: reply + lang.get('verify.missing'),
  22. allowed_mentions,
  23. flags: 64
  24. }
  25. }
  26. } ).catch(log_error);
  27. if ( !channel.guild.me.permissions.has('MANAGE_ROLES') ) {
  28. console.log( channel.guild.id + ': Missing permissions - MANAGE_ROLES' );
  29. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  30. data: {
  31. type: 4,
  32. data: {
  33. content: reply + lang.get('general.missingperm') + ' `MANAGE_ROLES`',
  34. allowed_mentions,
  35. flags: 64
  36. }
  37. }
  38. } ).catch(log_error);
  39. }
  40. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  41. data: {
  42. type: 5,
  43. data: {
  44. allowed_mentions,
  45. flags: 0
  46. }
  47. }
  48. } ).then( () => {
  49. return db.query( 'SELECT role, editcount, postcount, usergroup, accountage, rename FROM verification WHERE guild = $1 AND channel LIKE $2 ORDER BY configid ASC', [interaction.guild_id, '%|' + interaction.channel_id + '|%'] ).then( ({rows}) => {
  50. if ( !rows.length ) return sendMessage(interaction, {
  51. content: reply + lang.get('verify.missing') + ( interaction.member.permissions.has('MANAGE_GUILD') ? '\n' + new URL(`/guild/${interaction.guild_id}/verification`, process.env.dashboard).href : '' ),
  52. allowed_mentions
  53. }, channel);
  54. return channel.guild.members.fetch(interaction.user.id).then( member => {
  55. var username = ( interaction.data.options?.[0]?.value || '' ).replace( /^\s*<@!?(\d+)>\s*$/, (mention, id) => {
  56. if ( id === interaction.user.id ) {
  57. return ( interaction.member?.nick || interaction.user.username );
  58. }
  59. let user = channel.guild.members.cache.get(id);
  60. if ( user ) return user.displayName;
  61. else {
  62. user = interaction.client.users.cache.get(user);
  63. if ( user ) return user.username;
  64. }
  65. } ).replace( /_/g, ' ' ).trim().replace( /^<\s*(.*)\s*>$/, '$1' ).split('#')[0].substring(0, 250).trim();
  66. if ( /^(?:https?:)?\/\/([a-z\d-]{1,50})\.(?:gamepedia\.com\/|(?:fandom\.com|wikia\.org)\/(?:[a-z-]{1,8}\/)?(?:wiki\/)?)/.test(username) ) {
  67. username = decodeURIComponent( username.replace( /^(?:https?:)?\/\/([a-z\d-]{1,50})\.(?:gamepedia\.com\/|(?:fandom\.com|wikia\.org)\/(?:[a-z-]{1,8}\/)?(?:wiki\/)?)/, '' ) );
  68. }
  69. if ( wiki.isGamepedia() ) username = username.replace( /^userprofile\s*:\s*/i, '' );
  70. if ( !username.trim() ) return sendMessage(interaction, {
  71. content: lang.get('interaction.verify'),
  72. allowed_mentions
  73. }, channel);
  74. return verify(lang, channel, member, username, wiki, rows).then( result => {
  75. var message = {
  76. content: reply + result.content,
  77. embeds: [result.embed],
  78. allowed_mentions
  79. };
  80. if ( result.reaction ) {
  81. if ( result.reaction === 'nowiki' ) message.content = lang.get('interaction.nowiki');
  82. else message.content = reply + lang.get('verify.error_reply');
  83. message.embeds = [];
  84. }
  85. return sendMessage(interaction, message, channel);
  86. }, error => {
  87. console.log( '- Error during the verifications: ' + error );
  88. return sendMessage(interaction, {
  89. content: reply + lang.get('verify.error_reply'),
  90. allowed_mentions
  91. }, channel);
  92. } );
  93. }, error => {
  94. console.log( '- Error while getting the member: ' + error );
  95. return sendMessage(interaction, {
  96. content: reply + lang.get('verify.error_reply'),
  97. allowed_mentions
  98. }, channel);
  99. } );
  100. }, dberror => {
  101. console.log( '- Error while getting the verifications: ' + dberror );
  102. return sendMessage(interaction, {
  103. content: reply + lang.get('verify.error_reply'),
  104. allowed_mentions
  105. }, channel);
  106. } );
  107. }, log_error );
  108. }
  109. module.exports = {
  110. name: 'verify',
  111. run: slash_verify
  112. };