verification.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. const {Util, MessageActionRow, MessageButton, Permissions: {FLAGS}} = require('discord.js');
  2. const help_setup = require('../functions/helpsetup.js');
  3. const {limit: {verification: verificationLimit}} = require('../util/default.json');
  4. var db = require('../util/database.js');
  5. const {got} = require('../util/functions.js');
  6. /**
  7. * Processes the "verification" command.
  8. * @param {import('../util/i18n.js')} lang - The user language.
  9. * @param {import('discord.js').Message} msg - The Discord message.
  10. * @param {String[]} args - The command arguments.
  11. * @param {String} line - The command as plain text.
  12. * @param {import('../util/wiki.js')} wiki - The wiki for the message.
  13. */
  14. function cmd_verification(lang, msg, args, line, wiki) {
  15. if ( !msg.isAdmin() ) {
  16. if ( msg.channel.isGuild() && !pause[msg.guildId] ) this.verify(lang, msg, args, line, wiki);
  17. else msg.reactEmoji('❌');
  18. return;
  19. }
  20. if ( msg.defaultSettings ) return help_setup(lang, msg);
  21. if ( !msg.guild.me.permissions.has(FLAGS.MANAGE_ROLES) ) {
  22. console.log( msg.guildId + ': Missing permissions - MANAGE_ROLES' );
  23. return msg.replyMsg( lang.get('general.missingperm') + ' `MANAGE_ROLES`' );
  24. }
  25. db.query( 'SELECT configid, channel, role, editcount, postcount, usergroup, accountage, rename FROM verification WHERE guild = $1 ORDER BY configid ASC', [msg.guildId] ).then( ({rows}) => {
  26. var prefix = ( patreons[msg.guildId] || process.env.prefix );
  27. var button = null;
  28. var components = [];
  29. if ( process.env.dashboard ) {
  30. button = new MessageButton().setLabel(lang.get('settings.button')).setEmoji('<:wikibot:588723255972593672>').setStyle('LINK').setURL(new URL(`/guild/${msg.guildId}/verification`, process.env.dashboard).href);
  31. components.push(new MessageActionRow().addComponents(button));
  32. }
  33. if ( args[0] && args[0].toLowerCase() === 'add' ) {
  34. var limit = verificationLimit[( patreons[msg.guildId] ? 'patreon' : 'default' )];
  35. if ( rows.length >= limit ) return msg.replyMsg( lang.get('verification.max_entries'), true );
  36. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, true );
  37. button?.setURL(new URL(`/guild/${msg.guildId}/verification/new`, button.url).href);
  38. var roles = args.slice(1).join(' ').split('|').map( role => role.replace( /^\s*<?\s*(.*?)\s*>?\s*$/, '$1' ) ).filter( role => role.length );
  39. if ( !roles.length ) return msg.replyMsg( {content: lang.get('verification.no_role') + '\n`' + prefix + 'verification add ' + lang.get('verification.new_role') + '`', components}, true );
  40. if ( roles.length > 10 ) return msg.replyMsg( {content: lang.get('verification.role_max'), components}, true );
  41. roles = roles.map( role => {
  42. var new_role = ['', null];
  43. if ( role.startsWith( '-' ) ) {
  44. role = role.replace( '-', '' );
  45. new_role[0] = '-';
  46. }
  47. if ( /^\d+$/.test(role) ) new_role[1] = msg.guild.roles.cache.get(role);
  48. if ( !new_role[1] ) new_role[1] = msg.guild.roles.cache.find( gc => gc.name === role.replace( /^@/, '' ) );
  49. if ( !new_role[1] ) new_role[1] = msg.guild.roles.cache.find( gc => gc.name.toLowerCase() === role.toLowerCase().replace( /^@/, '' ) );
  50. return new_role;
  51. } );
  52. if ( roles.some( role => !role[1] ) ) return msg.replyMsg( {content: lang.get('verification.role_missing'), components}, true );
  53. if ( roles.some( role => role[1].managed || role[1].id === msg.guildId ) ) return msg.replyMsg( {content: lang.get('verification.role_managed'), components}, true );
  54. roles = roles.map( role => role[0] + role[1].id ).join('|');
  55. var new_configid = 1;
  56. for ( let i of rows.map( row => row.configid ) ) {
  57. if ( new_configid === i ) new_configid++;
  58. else break;
  59. }
  60. return db.query( 'INSERT INTO verification(guild, configid, channel, role) VALUES($1, $2, $3, $4)', [msg.guildId, new_configid, '|' + ( msg.channel.isThread() ? msg.channel.parentId : msg.channelId ) + '|', roles] ).then( () => {
  61. console.log( '- Verification successfully added.' );
  62. if ( !rows.length ) msg.client.application.commands.cache.find( slashCommand => slashCommand.name === 'verify' )?.permissions.set( {
  63. guild: msg.guildId,
  64. permissions: [{
  65. id: msg.guildId,
  66. type: 'ROLE',
  67. permission: true
  68. }]
  69. } ).then( () => {
  70. console.log( '- Slash command successfully enabled.' );
  71. }, error => {
  72. console.log( '- Error while enabling the slash command: ' + error );
  73. } );
  74. msg.replyMsg( {content: lang.get('verification.added') + formatVerification(false, false, {configid: new_configid, role: roles}), components}, true );
  75. }, dberror => {
  76. console.log( '- Error while adding the verification: ' + dberror );
  77. msg.replyMsg( {content: lang.get('verification.save_failed'), components}, true );
  78. } );
  79. }
  80. if ( !rows.some( row => row.configid.toString() === args[0] ) ) {
  81. if ( args.length ) {
  82. if ( !pause[msg.guildId] ) this.verify(lang, msg, args, line, wiki);
  83. return;
  84. }
  85. var text = null;
  86. if ( rows.length ) {
  87. text = lang.get('verification.current');
  88. if ( button ) text += `\n<${button.url}>`;
  89. text += rows.map( row => formatVerification(false, true, row) ).join('');
  90. }
  91. else {
  92. text = lang.get('verification.missing');
  93. if ( button ) text += `\n<${button.url}>`;
  94. }
  95. text += '\n\n' + lang.get('verification.add_more') + '\n`' + prefix + 'verification add ' + lang.get('verification.new_role') + '`';
  96. return Util.splitMessage( text ).forEach( textpart => msg.replyMsg( {content: textpart, components}, true ) );
  97. }
  98. var row = rows.find( row => row.configid.toString() === args[0] );
  99. if ( args[1] ) args[1] = args[1].toLowerCase();
  100. if ( args[1] === 'delete' && !args.slice(2).join('') ) {
  101. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, true );
  102. return db.query( 'DELETE FROM verification WHERE guild = $1 AND configid = $2', [msg.guildId, row.configid] ).then( () => {
  103. console.log( '- Verification successfully removed.' );
  104. if ( rows.length === 1 ) msg.client.application.commands.cache.find( slashCommand => slashCommand.name === 'verify' )?.permissions.set( {
  105. guild: msg.guildId,
  106. permissions: []
  107. } ).then( () => {
  108. console.log( '- Slash command successfully disabled.' );
  109. }, error => {
  110. console.log( '- Error while disabling the slash command: ' + error );
  111. } );
  112. msg.replyMsg( {content: lang.get('verification.deleted'), components}, true );
  113. }, dberror => {
  114. console.log( '- Error while removing the verification: ' + dberror );
  115. button?.setURL(new URL(`/guild/${msg.guildId}/verification/${row.configid}`, button.url).href);
  116. msg.replyMsg( {content: lang.get('verification.save_failed'), components}, true );
  117. } );
  118. }
  119. button?.setURL(new URL(`/guild/${msg.guildId}/verification/${row.configid}`, button.url).href);
  120. if ( args[1] === 'rename' && !args.slice(2).join('') ) {
  121. if ( !row.rename && !msg.guild.me.permissions.has(FLAGS.MANAGE_NICKNAMES) ) {
  122. console.log( msg.guildId + ': Missing permissions - MANAGE_NICKNAMES' );
  123. return msg.replyMsg( lang.get('general.missingperm') + ' `MANAGE_NICKNAMES`' );
  124. }
  125. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, true );
  126. return db.query( 'UPDATE verification SET rename = $1 WHERE guild = $2 AND configid = $3', [( row.rename ? 0 : 1 ), msg.guildId, row.configid] ).then( () => {
  127. console.log( '- Verification successfully updated.' );
  128. row.rename = ( row.rename ? 0 : 1 );
  129. Util.splitMessage( lang.get('verification.updated') + formatVerification() ).forEach( textpart => msg.replyMsg( {content: textpart, components}, true ) );
  130. }, dberror => {
  131. console.log( '- Error while updating the verification: ' + dberror );
  132. msg.replyMsg( {content: lang.get('verification.save_failed'), components}, true );
  133. } );
  134. }
  135. if ( args[2] ) {
  136. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, true );
  137. args[2] = args.slice(2).join(' ').replace( /^\s*<?\s*(.*?)\s*>?\s*$/, '$1' );
  138. if ( args[1] === 'channel' ) {
  139. var channels = args[2].replace( /\s*>?\s*[,|]\s*<?\s*/g, '|' ).split('|').filter( channel => channel.length );
  140. if ( channels.length > 10 ) return msg.replyMsg( {content: lang.get('verification.channel_max'), components}, true );
  141. channels = channels.map( channel => {
  142. var new_channel = '';
  143. if ( /^\d+$/.test(channel) ) new_channel = msg.guild.channels.cache.filter( tc => tc.isGuild(false) ).get(channel);
  144. if ( !new_channel ) new_channel = msg.guild.channels.cache.filter( gc => gc.isGuild(false) ).find( gc => gc.name === channel.replace( /^#/, '' ) );
  145. if ( !new_channel ) new_channel = msg.guild.channels.cache.filter( gc => gc.isGuild(false) ).find( gc => gc.name.toLowerCase() === channel.toLowerCase().replace( /^#/, '' ) );
  146. return new_channel;
  147. } );
  148. if ( channels.some( channel => !channel ) ) return msg.replyMsg( {content: lang.get('verification.channel_missing'), components}, true );
  149. channels = channels.map( channel => channel.id ).join('|');
  150. if ( channels.length ) return db.query( 'UPDATE verification SET channel = $1 WHERE guild = $2 AND configid = $3', ['|' + channels + '|', msg.guildId, row.configid] ).then( () => {
  151. console.log( '- Verification successfully updated.' );
  152. row.channel = '|' + channels + '|';
  153. Util.splitMessage( lang.get('verification.updated') + formatVerification() ).forEach( textpart => msg.replyMsg( {content: textpart, components}, true ) );
  154. }, dberror => {
  155. console.log( '- Error while updating the verification: ' + dberror );
  156. msg.replyMsg( {content: lang.get('verification.save_failed'), components}, true );
  157. } );
  158. }
  159. if ( args[1] === 'role' ) {
  160. var roles = args[2].replace( /\s*>?\s*[,|]\s*<?\s*/g, '|' ).split('|').filter( role => role.length );
  161. if ( roles.length > 10 ) return msg.replyMsg( {content: lang.get('verification.role_max'), components}, true );
  162. roles = roles.map( role => {
  163. var new_role = ['', null];
  164. if ( role.startsWith( '-' ) ) {
  165. role = role.replace( '-', '' );
  166. new_role[0] = '-';
  167. }
  168. if ( /^\d+$/.test(role) ) new_role[1] = msg.guild.roles.cache.get(role);
  169. if ( !new_role[1] ) new_role[1] = msg.guild.roles.cache.find( gc => gc.name === role.replace( /^@/, '' ) );
  170. if ( !new_role[1] ) new_role[1] = msg.guild.roles.cache.find( gc => gc.name.toLowerCase() === role.toLowerCase().replace( /^@/, '' ) );
  171. return new_role;
  172. } );
  173. if ( roles.some( role => !role[1] ) ) return msg.replyMsg( {content: lang.get('verification.role_missing'), components}, true );
  174. if ( roles.some( role => role[1].managed || role[1].id === msg.guildId ) ) return msg.replyMsg( {content: lang.get('verification.role_managed'), components}, true );
  175. roles = roles.map( role => role[0] + role[1].id ).join('|');
  176. if ( roles.length ) return db.query( 'UPDATE verification SET role = $1 WHERE guild = $2 AND configid = $3', [roles, msg.guildId, row.configid] ).then( () => {
  177. console.log( '- Verification successfully updated.' );
  178. row.role = roles;
  179. Util.splitMessage( lang.get('verification.updated') + formatVerification() ).forEach( textpart => msg.replyMsg( {content: textpart, components}, true ) );
  180. }, dberror => {
  181. console.log( '- Error while updating the verification: ' + dberror );
  182. msg.replyMsg( {content: lang.get('verification.save_failed'), components}, true );
  183. } );
  184. }
  185. if ( ( ( args[1] === 'editcount' || args[1] === 'accountage' ) && /^\d+$/.test(args[2]) ) || ( args[1] === 'postcount' && /^(?:-?\d+|null)$/.test(args[2]) ) ) {
  186. args[2] = parseInt(args[2], 10);
  187. if ( isNaN(args[2]) ) args[2] = null;
  188. if ( args[2] > 1000000 || args[2] < -1000000 ) {
  189. return msg.replyMsg( {content: lang.get('verification.value_too_high'), components}, true );
  190. }
  191. return db.query( 'UPDATE verification SET ' + args[1] + ' = $1 WHERE guild = $2 AND configid = $3', [args[2], msg.guildId, row.configid] ).then( () => {
  192. console.log( '- Verification successfully updated.' );
  193. row[args[1]] = args[2];
  194. Util.splitMessage( lang.get('verification.updated') + formatVerification() ).forEach( textpart => msg.replyMsg( {content: textpart, components}, true ) );
  195. }, dberror => {
  196. console.log( '- Error while updating the verification: ' + dberror );
  197. msg.replyMsg( {content: lang.get('verification.save_failed'), components}, true );
  198. } );
  199. }
  200. if ( args[1] === 'usergroup' ) {
  201. var usergroups = args[2].replace( /\s*>?\s*[,|]\s*<?\s*/g, '|' ).replace( / /g, '_' ).toLowerCase().split('|').filter( usergroup => usergroup.length );
  202. var and_or = '';
  203. if ( /^\s*AND\s*\|/.test(args[2]) ) {
  204. usergroups = usergroups.slice(1);
  205. and_or = 'AND|';
  206. }
  207. if ( usergroups.length > 10 ) return msg.replyMsg( {content: lang.get('verification.usergroup_max'), components}, true );
  208. if ( usergroups.some( usergroup => usergroup.length > 100 ) ) return msg.replyMsg( {content: lang.get('verification.usergroup_too_long'), components}, true );
  209. if ( usergroups.length ) return msg.reactEmoji('⏳').then( reaction => got.get( wiki + 'api.php?action=query&meta=allmessages&amprefix=group-&amincludelocal=true&amenableparser=true&format=json' ).then( response => {
  210. var body = response.body;
  211. if ( body && body.warnings ) log_warn(body.warnings);
  212. if ( response.statusCode !== 200 || body?.batchcomplete === undefined || !body?.query?.allmessages ) {
  213. if ( wiki.noWiki(response.url, response.statusCode) ) console.log( '- This wiki doesn\'t exist!' );
  214. else console.log( '- ' + response.statusCode + ': Error while getting the usergroups: ' + ( body && body.error && body.error.info ) );
  215. return;
  216. }
  217. var groups = body.query.allmessages.filter( group => {
  218. if ( group.name === 'group-all' ) return false;
  219. if ( group.name === 'group-membership-link-with-expiry' ) return false;
  220. if ( group.name.endsWith( '.css' ) || group.name.endsWith( '.js' ) ) return false;
  221. return true;
  222. } ).map( group => {
  223. return {
  224. name: group.name.replace( /^group-/, '' ).replace( /-member$/, '' ),
  225. content: group['*'].replace( / /g, '_' ).toLowerCase()
  226. };
  227. } );
  228. usergroups = usergroups.map( usergroup => {
  229. if ( groups.some( group => group.name === usergroup ) ) return usergroup;
  230. if ( groups.some( group => group.content === usergroup ) ) {
  231. return groups.find( group => group.content === usergroup ).name;
  232. }
  233. if ( /^admins?$/.test(usergroup) ) return 'sysop';
  234. if ( usergroup === '*' ) return 'user';
  235. return usergroup;
  236. } );
  237. }, error => {
  238. console.log( '- Error while getting the usergroups: ' + error );
  239. } ).finally( () => {
  240. usergroups = usergroups.join('|');
  241. db.query( 'UPDATE verification SET usergroup = $1 WHERE guild = $2 AND configid = $3', [and_or + usergroups, msg.guildId, row.configid] ).then( () => {
  242. console.log( '- Verification successfully updated.' );
  243. row.usergroup = and_or + usergroups;
  244. Util.splitMessage( lang.get('verification.updated') + formatVerification() ).forEach( textpart => msg.replyMsg( {content: textpart, components}, true ) );
  245. if ( reaction ) reaction.removeEmoji();
  246. }, dberror => {
  247. console.log( '- Error while updating the verification: ' + dberror );
  248. msg.replyMsg( {content: lang.get('verification.save_failed'), components}, true );
  249. if ( reaction ) reaction.removeEmoji();
  250. } );
  251. } ) );
  252. }
  253. }
  254. return Util.splitMessage( lang.get('verification.current_selected', row.configid) + ( button ? `\n<${button.url}>` : '' ) + formatVerification(true) +'\n\n' + lang.get('verification.delete_current') + '\n`' + prefix + 'verification ' + row.configid + ' delete`' ).forEach( textpart => msg.replyMsg( {content: textpart, components}, true ) );
  255. function formatVerification(showCommands, hideNotice, {
  256. configid,
  257. channel = '|' + ( msg.channel.isThread() ? msg.channel.parentId : msg.channelId ) + '|',
  258. role = '',
  259. editcount = 0,
  260. postcount = 0,
  261. usergroup = 'user',
  262. accountage = 0,
  263. rename = 0
  264. } = row) {
  265. var roles = [
  266. role.split('|').filter( roleid => !roleid.startsWith( '-' ) ),
  267. role.split('|').filter( roleid => roleid.startsWith( '-' ) ).map( roleid => roleid.replace( '-', '' ) )
  268. ];
  269. var verification_text = '\n\n`' + prefix + 'verification ' + configid + '`';
  270. verification_text += '\n' + lang.get('verification.channel') + ' <#' + channel.split('|').filter( channel => channel.length ).join('>, <#') + '>';
  271. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' channel ' + lang.get('verification.new_channel') + '`\n';
  272. if ( roles[0].length ) verification_text += '\n' + lang.get('verification.role_add') + ' <@&' + roles[0].join('>, <@&') + '>';
  273. if ( roles[1].length ) verification_text += '\n' + lang.get('verification.role_remove') + ' <@&' + roles[1].join('>, <@&') + '>';
  274. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' role ' + lang.get('verification.new_role') + '`\n';
  275. if ( postcount === null ) verification_text += '\n' + lang.get('verification.posteditcount') + ' `' + editcount + '`';
  276. else verification_text += '\n' + lang.get('verification.editcount') + ' `' + editcount + '`';
  277. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' editcount ' + lang.get('verification.new_editcount') + '`\n';
  278. if ( postcount !== null ) {
  279. verification_text += '\n' + lang.get('verification.postcount') + ' `' + Math.abs(postcount) + '`';
  280. if ( postcount < 0 ) verification_text += ' ' + lang.get('verification.postcount_or');
  281. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' postcount ' + ( postcount < 0 ? '-' : '' ) + lang.get('verification.new_postcount') + '`\n';
  282. }
  283. verification_text += '\n' + lang.get('verification.usergroup') + ' `' + ( usergroup.startsWith( 'AND|' ) ? usergroup.split('|').slice(1).join('` ' + lang.get('verification.and') + ' `') : usergroup.split('|').join('` ' + lang.get('verification.or') + ' `') ) + '`';
  284. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' usergroup ' + lang.get('verification.new_usergroup') + '`\n';
  285. verification_text += '\n' + lang.get('verification.accountage') + ' `' + accountage + '` ' + lang.get('verification.indays');
  286. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' accountage ' + lang.get('verification.new_accountage') + '`\n';
  287. verification_text += '\n' + lang.get('verification.rename') + ' *`' + lang.get('verification.' + ( rename ? 'enabled' : 'disabled')) + '`*';
  288. if ( showCommands ) verification_text += ' ' + lang.get('verification.toggle') + '\n`' + prefix + 'verification ' + row.configid + ' rename`\n';
  289. if ( !hideNotice && rename && !msg.guild.me.permissions.has(FLAGS.MANAGE_NICKNAMES) ) {
  290. verification_text += '\n\n' + lang.get('verification.rename_no_permission', msg.guild.me.toString());
  291. }
  292. if ( !hideNotice && role.replace( /-/g, '' ).split('|').some( role => {
  293. return ( !msg.guild.roles.cache.has(role) || msg.guild.me.roles.highest.comparePositionTo(role) <= 0 );
  294. } ) ) {
  295. verification_text += '\n';
  296. role.replace( /-/g, '' ).split('|').forEach( role => {
  297. if ( !msg.guild.roles.cache.has(role) ) {
  298. verification_text += '\n' + lang.get('verification.role_deleted', '<@&' + role + '>');
  299. }
  300. else if ( msg.guild.me.roles.highest.comparePositionTo(role) <= 0 ) {
  301. verification_text += '\n' + lang.get('verification.role_too_high', '<@&' + role + '>', msg.guild.me.toString());
  302. }
  303. } );
  304. }
  305. return verification_text;
  306. }
  307. }, dberror => {
  308. console.log( '- Error while getting the verifications: ' + dberror );
  309. msg.reactEmoji('error', true);
  310. } );
  311. }
  312. module.exports = {
  313. name: 'verification',
  314. everyone: true,
  315. pause: true,
  316. owner: false,
  317. run: cmd_verification
  318. };