verification.js 14 KB

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