verification.js 19 KB

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