verification.js 20 KB

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