verification.js 20 KB

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