verify.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. var db = require('../util/database.js');
  2. var verify = require('../functions/verify.js');
  3. const {sendMessage} = require('../util/functions.js');
  4. /**
  5. * Wiki user verification.
  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. console.log( interaction.guild_id + ': Slash: /' + interaction.data.name + ' ' + interaction.data.options?.[0]?.value );
  28. if ( !channel.guild.me.permissions.has('MANAGE_ROLES') ) {
  29. console.log( channel.guild.id + ': Missing permissions - MANAGE_ROLES' );
  30. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  31. data: {
  32. type: 4,
  33. data: {
  34. content: reply + lang.get('general.missingperm') + ' `MANAGE_ROLES`',
  35. allowed_mentions,
  36. flags: 64
  37. }
  38. }
  39. } ).catch(log_error);
  40. }
  41. 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}) => {
  42. if ( !rows.length ) return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  43. data: {
  44. type: 4,
  45. data: {
  46. content: reply + lang.get('verify.missing') + ( interaction.member.permissions.has('MANAGE_GUILD') ? '\n' + new URL(`/guild/${interaction.guild_id}/verification`, process.env.dashboard).href : '' ),
  47. allowed_mentions,
  48. flags: 64
  49. }
  50. }
  51. } ).catch(log_error);
  52. var username = ( interaction.data.options?.[0]?.value || '' ).replace( /^\s*<@!?(\d+)>\s*$/, (mention, id) => {
  53. if ( id === interaction.user.id ) {
  54. return ( interaction.member?.nick || interaction.user.username );
  55. }
  56. let user = channel.guild.members.cache.get(id);
  57. if ( user ) return user.displayName;
  58. else {
  59. user = interaction.client.users.cache.get(user);
  60. if ( user ) return user.username;
  61. }
  62. return mention;
  63. } ).replace( /_/g, ' ' ).trim().replace( /^<\s*(.*)\s*>$/, '$1' ).split('#')[0].substring(0, 250).trim();
  64. if ( /^(?:https?:)?\/\/([a-z\d-]{1,50})\.(?:gamepedia\.com\/|(?:fandom\.com|wikia\.org)\/(?:[a-z-]{1,8}\/)?(?:wiki\/)?)/.test(username) ) {
  65. username = decodeURIComponent( username.replace( /^(?:https?:)?\/\/([a-z\d-]{1,50})\.(?:gamepedia\.com\/|(?:fandom\.com|wikia\.org)\/(?:[a-z-]{1,8}\/)?(?:wiki\/)?)/, '' ) );
  66. }
  67. if ( wiki.isGamepedia() ) username = username.replace( /^userprofile\s*:\s*/i, '' );
  68. if ( !username.trim() ) return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  69. data: {
  70. type: 4,
  71. data: {
  72. content: lang.get('interaction.verify'),
  73. allowed_mentions,
  74. flags: 64
  75. }
  76. }
  77. } ).catch(log_error);
  78. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  79. data: {
  80. type: 5,
  81. data: {
  82. allowed_mentions,
  83. flags: 0
  84. }
  85. }
  86. } ).then( () => {
  87. return channel.guild.members.fetch(interaction.user.id).then( member => {
  88. return verify(lang, channel, member, username, wiki, rows).then( result => {
  89. var message = {
  90. content: reply + result.content,
  91. embeds: [result.embed],
  92. allowed_mentions,
  93. components: []
  94. };
  95. if ( result.add_button ) message.components.push({
  96. type: 1,
  97. components: [
  98. {
  99. type: 2,
  100. style: 1,
  101. label: lang.get('verify.button_again'),
  102. emoji: {id: null, name: '🔂'},
  103. custom_id: 'verify_again',
  104. disabled: false
  105. }
  106. ]
  107. });
  108. if ( result.reaction ) {
  109. if ( result.reaction === 'nowiki' ) message.content = lang.get('interaction.nowiki');
  110. else message.content = reply + lang.get('verify.error_reply');
  111. message.embeds = [];
  112. }
  113. return sendMessage(interaction, message, channel, false).then( msg => {
  114. if ( !result.logging.channel || !channel.guild.channels.cache.has(result.logging.channel) ) return;
  115. if ( msg ) {
  116. if ( result.logging.embed ) result.logging.embed.addField(msg.url, '<#' + channel.id + '>');
  117. else result.logging.content += '\n<#' + channel.id + '> – <' + msg.url + '>';
  118. }
  119. channel.guild.channels.cache.get(result.logging.channel).send(result.logging.content, {
  120. embed: result.logging.embed,
  121. allowedMentions: {parse: []}
  122. }).catch(log_error);
  123. } );
  124. }, error => {
  125. console.log( '- Error during the verifications: ' + error );
  126. return sendMessage(interaction, {
  127. content: reply + lang.get('verify.error_reply'),
  128. allowed_mentions
  129. }, channel);
  130. } );
  131. }, error => {
  132. console.log( '- Error while getting the member: ' + error );
  133. return sendMessage(interaction, {
  134. content: reply + lang.get('verify.error_reply'),
  135. allowed_mentions
  136. }, channel);
  137. } );
  138. }, log_error );
  139. }, dberror => {
  140. console.log( '- Error while getting the verifications: ' + dberror );
  141. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  142. data: {
  143. type: 4,
  144. data: {
  145. content: reply + lang.get('verify.error_reply'),
  146. allowed_mentions,
  147. flags: 64
  148. }
  149. }
  150. } ).catch(log_error);
  151. } );
  152. }
  153. /**
  154. * Wiki user verification.
  155. * @param {Object} interaction - The interaction.
  156. * @param {import('discord.js').Client} interaction.client - The client of the interaction.
  157. * @param {import('../util/i18n.js')} lang - The user language.
  158. * @param {import('../util/wiki.js')} wiki - The wiki for the interaction.
  159. * @param {import('discord.js').TextChannel} [channel] - The channel for the interaction.
  160. */
  161. function button_verify(interaction, lang, wiki, channel) {
  162. var username = interaction?.message?.embeds?.[0]?.title?.replace( /\\(\\)?/g, '$1' );
  163. if ( !username || !channel?.guild || !interaction.message?.mentions?.[0]?.id ) {
  164. interaction.message.allowed_mentions = {
  165. users: [interaction.user.id]
  166. };
  167. interaction.message.components = [];
  168. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  169. data: {
  170. type: 7,
  171. data: interaction.message
  172. }
  173. } ).catch(log_error);
  174. }
  175. if ( interaction.user.id !== interaction.message.mentions[0].id ) {
  176. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  177. data: {type: 6}
  178. } ).then( () => {
  179. interaction.client.api.webhooks(interaction.application_id, interaction.token).post( {
  180. data: {
  181. content: lang.get('verify.button_wrong_user', `<@${interaction.message.mentions[0].id}>`),
  182. allowed_mentions: {
  183. parse: []
  184. },
  185. flags: 64
  186. }
  187. } ).catch(log_error);
  188. }, log_error);
  189. }
  190. 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}) => {
  191. if ( !rows.length || !channel.guild.me.permissions.has('MANAGE_ROLES') ) {
  192. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  193. data: {type: 6}
  194. } ).catch(log_error);
  195. }
  196. var reply = '<@' + ( interaction.member?.nick ? '!' : '' ) + interaction.user.id + '>, ';
  197. var allowed_mentions = {
  198. users: [interaction.user.id]
  199. };
  200. interaction.message.allowed_mentions = allowed_mentions;
  201. interaction.message.components[0].components[0].disabled = true;
  202. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  203. data: {
  204. type: 7,
  205. data: interaction.message
  206. }
  207. } ).then( () => {
  208. return channel.guild.members.fetch(interaction.user.id).then( member => {
  209. console.log( interaction.guild_id + ': Button: ' + interaction.data.custom_id + ' ' + username );
  210. return verify(lang, channel, member, username, wiki, rows).then( result => {
  211. var message = {
  212. content: reply + result.content,
  213. embeds: [result.embed],
  214. allowed_mentions,
  215. components: []
  216. };
  217. if ( result.reaction ) {
  218. if ( result.reaction === 'nowiki' ) message.content = lang.get('interaction.nowiki');
  219. else message.content = reply + lang.get('verify.error_reply');
  220. message.embeds = [];
  221. }
  222. else if ( result.add_button ) message.components.push({
  223. type: 1,
  224. components: [
  225. {
  226. type: 2,
  227. style: 1,
  228. label: lang.get('verify.button_again'),
  229. emoji: {id: null, name: '🔂'},
  230. custom_id: 'verify_again',
  231. disabled: false
  232. }
  233. ]
  234. });
  235. sendMessage(interaction, message, channel, false);
  236. if ( result.logging.channel && channel.guild.channels.cache.has(result.logging.channel) ) {
  237. let msg_url = `https://discord.com/channels/${channel.guild.id}/${channel.id}/${interaction.message.id}`;
  238. if ( result.logging.embed ) result.logging.embed.addField(msg_url, '<#' + channel.id + '>');
  239. else result.logging.content += '\n<#' + channel.id + '> – <' + msg_url + '>';
  240. channel.guild.channels.cache.get(result.logging.channel).send(result.logging.content, {
  241. embed: result.logging.embed,
  242. allowedMentions: {parse: []}
  243. }).catch(log_error);
  244. }
  245. interaction.client.api.webhooks(interaction.application_id, interaction.token).post( {
  246. data: {
  247. content: message.content,
  248. embeds: message.embeds,
  249. allowed_mentions,
  250. components: [],
  251. flags: 64
  252. }
  253. } ).catch(log_error);
  254. }, error => {
  255. console.log( '- Error during the verifications: ' + error );
  256. return sendMessage(interaction, {
  257. content: reply + lang.get('verify.error_reply'),
  258. allowed_mentions
  259. }, channel);
  260. } );
  261. }, error => {
  262. console.log( '- Error while getting the member: ' + error );
  263. return sendMessage(interaction, {
  264. content: reply + lang.get('verify.error_reply'),
  265. allowed_mentions
  266. }, channel);
  267. } );
  268. }, log_error);
  269. }, dberror => {
  270. console.log( '- Error while getting the verifications: ' + dberror );
  271. return interaction.client.api.interactions(interaction.id, interaction.token).callback.post( {
  272. data: {type: 6}
  273. } ).catch(log_error);
  274. } );
  275. }
  276. module.exports = {
  277. name: 'verify',
  278. run: slash_verify,
  279. button: button_verify
  280. };