verification.js 20 KB

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