verification.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. const {limit: {verification: verificationLimit}} = require('../util/default.json');
  2. var db = require('../util/database.js');
  3. /**
  4. * Processes the "verification" command.
  5. * @param {import('../util/i18n.js')} lang - The user language.
  6. * @param {import('discord.js').Message} msg - The Discord message.
  7. * @param {String[]} args - The command arguments.
  8. * @param {String} line - The command as plain text.
  9. * @param {String} wiki - The wiki for the message.
  10. */
  11. function cmd_verification(lang, msg, args, line, wiki) {
  12. if ( !msg.isAdmin() ) {
  13. if ( msg.channel.type === 'text' && !pause[msg.guild.id] ) this.verify(lang, msg, args, line, wiki);
  14. else msg.reactEmoji('❌');
  15. return;
  16. }
  17. if ( !msg.guild.me.permissions.has('MANAGE_ROLES') ) {
  18. console.log( msg.guild.id + ': Missing permissions - MANAGE_ROLES' );
  19. return msg.replyMsg( lang.get('general.missingperm') + ' `MANAGE_ROLES`' );
  20. }
  21. db.all( 'SELECT configid, channel, role, editcount, usergroup, accountage, rename FROM verification WHERE guild = ? ORDER BY configid ASC', [msg.guild.id], (error, rows) => {
  22. if ( error || !rows ) {
  23. console.log( '- Error while getting the verifications: ' + error );
  24. msg.reactEmoji('error', true);
  25. return error;
  26. }
  27. var prefix = ( patreons[msg.guild.id] || process.env.prefix );
  28. if ( args[0] && args[0].toLowerCase() === 'add' ) {
  29. var limit = verificationLimit[( msg.guild.id in patreons ? 'patreon' : 'default' )];
  30. if ( rows.length >= limit ) return msg.replyMsg( lang.get('verification.max_entries'), {}, true );
  31. var roles = args.slice(1).join(' ').split('|').map( role => role.replace( /^\s*<?\s*(.*?)\s*>?\s*$/, '$1' ) ).filter( role => role.length );
  32. if ( !roles.length ) return msg.replyMsg( lang.get('verification.no_role') + '\n`' + prefix + 'verification add ' + lang.get('verification.new_role') + '`', {}, true );
  33. if ( roles.length > 10 ) return msg.replyMsg( lang.get('verification.role_max'), {}, true );
  34. roles = roles.map( role => {
  35. var new_role = '';
  36. if ( /^\d+$/.test(role) ) new_role = msg.guild.roles.cache.get(role);
  37. if ( !new_role ) new_role = msg.guild.roles.cache.find( gc => gc.name === role.replace( /^@/, '' ) );
  38. if ( !new_role ) new_role = msg.guild.roles.cache.find( gc => gc.name.toLowerCase() === role.toLowerCase().replace( /^@/, '' ) );
  39. return new_role;
  40. } );
  41. if ( roles.some( role => !role ) ) return msg.replyMsg( lang.get('verification.role_missing'), {}, true );
  42. if ( roles.some( role => role.managed ) ) return msg.replyMsg( lang.get('verification.role_managed'), {}, true );
  43. roles = roles.map( role => role.id ).join('|');
  44. var new_configid = 1;
  45. for ( let i of rows.map( row => row.configid ) ) {
  46. if ( new_configid === i ) new_configid++;
  47. else break;
  48. }
  49. return db.run( 'INSERT INTO verification(guild, configid, channel, role) VALUES(?, ?, ?, ?)', [msg.guild.id, new_configid, '|' + msg.channel.id + '|', roles], function (dberror) {
  50. if ( dberror ) {
  51. console.log( '- Error while adding the verification: ' + dberror );
  52. msg.replyMsg( lang.get('verification.save_failed'), {}, true );
  53. return dberror;
  54. }
  55. console.log( '- Verification successfully added.' );
  56. msg.replyMsg( lang.get('verification.added') + formatVerification(false, false, {configid: new_configid, role: roles}), {}, true );
  57. } );
  58. }
  59. if ( !rows.some( row => row.configid.toString() === args[0] ) ) {
  60. if ( args.length ) {
  61. if ( !pause[msg.guild.id] ) this.verify(lang, msg, args, line, wiki);
  62. return;
  63. }
  64. var text = '';
  65. if ( rows.length ) text += lang.get('verification.current') + rows.map( row => formatVerification(false, true, row) ).join('');
  66. else text += lang.get('verification.missing');
  67. text += '\n\n' + lang.get('verification.add_more') + '\n`' + prefix + 'verification add ' + lang.get('verification.new_role') + '`';
  68. return msg.sendChannel( '<@' + msg.author.id + '>, ' + text, {split:true}, true );
  69. }
  70. var row = rows.find( row => row.configid.toString() === args[0] );
  71. if ( args[1] ) args[1] = args[1].toLowerCase();
  72. if ( args[1] === 'delete' && !args.slice(2).join('') ) {
  73. return db.run( 'DELETE FROM verification WHERE guild = ? AND configid = ?', [msg.guild.id, row.configid], function (dberror) {
  74. if ( dberror ) {
  75. console.log( '- Error while removing the verification: ' + dberror );
  76. msg.replyMsg( lang.get('verification.save_failed'), {}, true );
  77. return dberror;
  78. }
  79. console.log( '- Verification successfully removed.' );
  80. msg.replyMsg( lang.get('verification.deleted'), {}, true );
  81. } );
  82. }
  83. if ( args[1] === 'rename' && !args.slice(2).join('') ) {
  84. if ( !row.rename && !msg.guild.me.permissions.has('MANAGE_NICKNAMES') ) {
  85. console.log( msg.guild.id + ': Missing permissions - MANAGE_NICKNAMES' );
  86. return msg.replyMsg( lang.get('general.missingperm') + ' `MANAGE_NICKNAMES`' );
  87. }
  88. return db.run( 'UPDATE verification SET rename = ? WHERE guild = ? AND configid = ?', [( row.rename ? 0 : 1 ), msg.guild.id, row.configid], function (dberror) {
  89. if ( dberror ) {
  90. console.log( '- Error while updating the verification: ' + dberror );
  91. msg.replyMsg( lang.get('verification.save_failed'), {}, true );
  92. return dberror;
  93. }
  94. console.log( '- Verification successfully updated.' );
  95. row.rename = ( row.rename ? 0 : 1 );
  96. msg.sendChannel( '<@' + msg.author.id + '>, ' + lang.get('verification.updated') + formatVerification(), {split:true}, true );
  97. } );
  98. }
  99. if ( args[2] ) {
  100. args[2] = args.slice(2).join(' ').replace( /^\s*<?\s*(.*?)\s*>?\s*$/, '$1' );
  101. if ( args[1] === 'channel' ) {
  102. var channels = args[2].replace( /\s*>?\s*\|\s*<?\s*/g, '|' ).split('|').filter( channel => channel.length );
  103. if ( channels.length > 10 ) return msg.replyMsg( lang.get('verification.channel_max'), {}, true );
  104. channels = channels.map( channel => {
  105. var new_channel = '';
  106. if ( /^\d+$/.test(channel) ) new_channel = msg.guild.channels.cache.filter( tc => tc.type === 'text' ).get(channel);
  107. if ( !new_channel ) new_channel = msg.guild.channels.cache.filter( gc => gc.type === 'text' ).find( gc => gc.name === channel.replace( /^#/, '' ) );
  108. if ( !new_channel ) new_channel = msg.guild.channels.cache.filter( gc => gc.type === 'text' ).find( gc => gc.name.toLowerCase() === channel.toLowerCase().replace( /^#/, '' ) );
  109. return new_channel;
  110. } );
  111. if ( channels.some( channel => !channel ) ) return msg.replyMsg( lang.get('verification.channel_missing'), {}, true );
  112. channels = channels.map( channel => channel.id ).join('|');
  113. if ( channels.length ) return db.run( 'UPDATE verification SET channel = ? WHERE guild = ? AND configid = ?', ['|' + channels + '|', msg.guild.id, row.configid], function (dberror) {
  114. if ( dberror ) {
  115. console.log( '- Error while updating the verification: ' + dberror );
  116. msg.replyMsg( lang.get('verification.save_failed'), {}, true );
  117. return dberror;
  118. }
  119. console.log( '- Verification successfully updated.' );
  120. row.channel = '|' + channels + '|';
  121. msg.sendChannel( '<@' + msg.author.id + '>, ' + lang.get('verification.updated') + formatVerification(), {split:true}, true );
  122. } );
  123. }
  124. if ( args[1] === 'role' ) {
  125. var roles = args[2].replace( /\s*>?\s*\|\s*<?\s*/g, '|' ).split('|').filter( role => role.length );
  126. if ( roles.length > 10 ) return msg.replyMsg( lang.get('verification.role_max'), {}, true );
  127. roles = roles.map( role => {
  128. var new_role = '';
  129. if ( /^\d+$/.test(role) ) new_role = msg.guild.roles.cache.get(role);
  130. if ( !new_role ) new_role = msg.guild.roles.cache.find( gc => gc.name === role.replace( /^@/, '' ) );
  131. if ( !new_role ) new_role = msg.guild.roles.cache.find( gc => gc.name.toLowerCase() === role.toLowerCase().replace( /^@/, '' ) );
  132. return new_role;
  133. } );
  134. if ( roles.some( role => !role ) ) return msg.replyMsg( lang.get('verification.role_missing'), {}, true );
  135. if ( roles.some( role => role.managed ) ) return msg.replyMsg( lang.get('verification.role_managed'), {}, true );
  136. roles = roles.map( role => role.id ).join('|');
  137. if ( roles.length ) return db.run( 'UPDATE verification SET role = ? WHERE guild = ? AND configid = ?', [roles, msg.guild.id, row.configid], function (dberror) {
  138. if ( dberror ) {
  139. console.log( '- Error while updating the verification: ' + dberror );
  140. msg.replyMsg( lang.get('verification.save_failed'), {}, true );
  141. return dberror;
  142. }
  143. console.log( '- Verification successfully updated.' );
  144. row.role = roles;
  145. msg.sendChannel( '<@' + msg.author.id + '>, ' + lang.get('verification.updated') + formatVerification(), {split:true}, true );
  146. } );
  147. }
  148. if ( ( args[1] === 'editcount' || args[1] === 'accountage' ) && /^\d+$/.test(args[2]) ) {
  149. args[2] = parseInt(args[2], 10);
  150. if ( args[2] > 1000000 ) return msg.replyMsg( lang.get('verification.value_too_high'), {}, true );
  151. return db.run( 'UPDATE verification SET ' + args[1] + ' = ? WHERE guild = ? AND configid = ?', [args[2], msg.guild.id, row.configid], function (dberror) {
  152. if ( dberror ) {
  153. console.log( '- Error while updating the verification: ' + dberror );
  154. msg.replyMsg( lang.get('verification.save_failed'), {}, true );
  155. return dberror;
  156. }
  157. console.log( '- Verification successfully updated.' );
  158. row[args[1]] = args[2];
  159. msg.sendChannel( '<@' + msg.author.id + '>, ' + lang.get('verification.updated') + formatVerification(), {split:true}, true );
  160. } );
  161. }
  162. if ( args[1] === 'usergroup' ) {
  163. var usergroups = args[2].replace( /\s*>?\s*\|\s*<?\s*/g, '|' ).replace( / /g, '_' ).toLowerCase().split('|').filter( usergroup => usergroup.length );
  164. var and_or = '';
  165. if ( /^\s*AND\s*\|/.test(args[2]) ) {
  166. usergroups = usergroups.slice(1);
  167. and_or = 'AND|';
  168. }
  169. if ( usergroups.length > 10 ) return msg.replyMsg( lang.get('verification.usergroup_max'), {}, true );
  170. if ( usergroups.some( usergroup => usergroup.length > 100 ) ) return msg.replyMsg( lang.get('verification.usergroup_too_long'), {}, true );
  171. 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', {
  172. responseType: 'json'
  173. } ).then( response => {
  174. var body = response.body;
  175. if ( body && body.warnings ) log_warn(body.warnings);
  176. if ( response.statusCode !== 200 || !body || !body.query || !body.query.allmessages ) {
  177. if ( wiki.noWiki(response.url) || response.statusCode === 410 ) console.log( '- This wiki doesn\'t exist!' );
  178. else console.log( '- ' + response.statusCode + ': Error while getting the usergroups: ' + ( body && body.error && body.error.info ) );
  179. }
  180. var groups = body.query.allmessages.filter( group => !/\.(?:css|js)$/.test(group.name) && group.name !== 'group-all' ).map( group => {
  181. return {
  182. name: group.name.replace( /^group-/, '' ).replace( /-member$/, '' ),
  183. content: group['*'].replace( / /g, '_' ).toLowerCase()
  184. };
  185. } );
  186. usergroups = usergroups.map( usergroup => {
  187. if ( groups.some( group => group.name === usergroup ) ) return usergroup;
  188. if ( groups.some( group => group.content === usergroup ) ) return groups.find( group => group.content === usergroup ).name;
  189. if ( /^admins?$/.test(usergroup) ) return 'sysop'
  190. return usergroup;
  191. } );
  192. }, error => {
  193. console.log( '- Error while getting the usergroups: ' + error );
  194. } ).finally( () => {
  195. usergroups = usergroups.join('|');
  196. db.run( 'UPDATE verification SET usergroup = ? WHERE guild = ? AND configid = ?', [and_or + usergroups, msg.guild.id, row.configid], function (dberror) {
  197. if ( dberror ) {
  198. console.log( '- Error while updating the verification: ' + dberror );
  199. msg.replyMsg( lang.get('verification.save_failed'), {}, true );
  200. return dberror;
  201. }
  202. console.log( '- Verification successfully updated.' );
  203. row.usergroup = and_or + usergroups;
  204. msg.sendChannel( '<@' + msg.author.id + '>, ' + lang.get('verification.updated') + formatVerification(), {split:true}, true );
  205. if ( reaction ) reaction.removeEmoji();
  206. } );
  207. } ) );
  208. }
  209. }
  210. return msg.sendChannel( '<@' + msg.author.id + '>, ' + lang.get('verification.current_selected', row.configid) + formatVerification(true) +'\n\n' + lang.get('verification.delete_current') + '\n`' + prefix + 'verification ' + row.configid + ' delete`', {split:true}, true );
  211. function formatVerification(showCommands, hideNotice, {
  212. configid,
  213. channel = '|' + msg.channel.id + '|',
  214. role,
  215. editcount = 0,
  216. usergroup = 'user',
  217. accountage = 0,
  218. rename = 0
  219. } = row) {
  220. var verification_text = '\n\n`' + prefix + 'verification ' + configid + '`';
  221. verification_text += '\n' + lang.get('verification.channel') + ' <#' + channel.split('|').filter( channel => channel.length ).join('>, <#') + '>';
  222. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' channel ' + lang.get('verification.new_channel') + '`\n';
  223. verification_text += '\n' + lang.get('verification.role') + ' <@&' + role.split('|').join('>, <@&') + '>';
  224. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' role ' + lang.get('verification.new_role') + '`\n';
  225. verification_text += '\n' + lang.get('verification.editcount') + ' `' + editcount + '`';
  226. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' editcount ' + lang.get('verification.new_editcount') + '`\n';
  227. 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') + ' `') ) + '`';
  228. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' usergroup ' + lang.get('verification.new_usergroup') + '`\n';
  229. verification_text += '\n' + lang.get('verification.accountage') + ' `' + accountage + '` ' + lang.get('verification.indays');
  230. if ( showCommands ) verification_text += '\n`' + prefix + 'verification ' + row.configid + ' accountage ' + lang.get('verification.new_accountage') + '`\n';
  231. verification_text += '\n' + lang.get('verification.rename') + ' *`' + lang.get('verification.' + ( rename ? 'enabled' : 'disabled')) + '`*';
  232. if ( showCommands ) verification_text += ' ' + lang.get('verification.toggle') + '\n`' + prefix + 'verification ' + row.configid + ' rename`\n';
  233. if ( !hideNotice && rename && !msg.guild.me.permissions.has('MANAGE_NICKNAMES') ) {
  234. verification_text += '\n\n' + lang.get('verification.rename_no_permission', msg.guild.me.toString());
  235. }
  236. if ( !hideNotice && role.split('|').some( role => msg.guild.me.roles.highest.comparePositionTo(role) <= 0 ) ) {
  237. verification_text += '\n';
  238. role.split('|').forEach( role => {
  239. if ( msg.guild.me.roles.highest.comparePositionTo(role) <= 0 ) {
  240. verification_text += '\n' + lang.get('verification.role_too_high', '<@&' + role + '>', msg.guild.me.toString());
  241. }
  242. } );
  243. }
  244. return verification_text;
  245. }
  246. } );
  247. }
  248. module.exports = {
  249. name: 'verification',
  250. everyone: true,
  251. pause: true,
  252. owner: false,
  253. run: cmd_verification
  254. };