verification.js 19 KB

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