verification.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. const {limit: {verification: verificationLimit}} = require('../util/default.json');
  2. const Lang = require('../util/i18n.js');
  3. const {got, db, sendMsg, hasPerm} = require('./util.js');
  4. const fieldset = {
  5. channel: '<label for="wb-settings-channel">Channel:</label>'
  6. + '<select id="wb-settings-channel" name="channel" required></select>'
  7. + '<button type="button" id="wb-settings-channel-more" class="addmore">Add more</button>',
  8. role: '<label for="wb-settings-role">Role:</label>'
  9. + '<select id="wb-settings-role" name="role" required></select>'
  10. + '<button type="button" id="wb-settings-role-more" class="addmore">Add more</button>',
  11. usergroup: '<label for="wb-settings-usergroup">Wiki user group:</label>'
  12. + '<input type="text" id="wb-settings-usergroup" name="usergroup">'
  13. + '<br>'
  14. + '<label for="wb-settings-usergroup-and">Require all user groups:</label>'
  15. + '<input type="checkbox" id="wb-settings-usergroup-and" name="usergroup_and">',
  16. editcount: '<label for="wb-settings-editcount">Minimal edit count:</label>'
  17. + '<input type="number" id="wb-settings-editcount" name="editcount" min="0" required>',
  18. accountage: '<label for="wb-settings-accountage">Account age (in days):</label>'
  19. + '<input type="number" id="wb-settings-accountage" name="accountage" min="0" required>',
  20. rename: '<label for="wb-settings-rename">Rename users:</label>'
  21. + '<input type="checkbox" id="wb-settings-rename" name="rename">',
  22. save: '<input type="submit" id="wb-settings-save" name="save_settings">',
  23. delete: '<input type="submit" id="wb-settings-delete" name="delete_settings">'
  24. };
  25. /**
  26. * Create a settings form
  27. * @param {import('cheerio')} $ - The response body
  28. * @param {String} header - The form header
  29. * @param {Object} settings - The current settings
  30. * @param {String} settings.channel
  31. * @param {String} settings.role
  32. * @param {String} settings.usergroup
  33. * @param {Number} settings.editcount
  34. * @param {Number} settings.accountage
  35. * @param {Boolean} settings.rename
  36. * @param {String} [settings.defaultrole]
  37. * @param {Object[]} guildChannels - The guild channels
  38. * @param {String} guildChannels.id
  39. * @param {String} guildChannels.name
  40. * @param {Number} guildChannels.userPermissions
  41. * @param {Object[]} guildRoles - The guild roles
  42. * @param {String} guildRoles.id
  43. * @param {String} guildRoles.name
  44. * @param {Boolean} guildRoles.lower
  45. */
  46. function createForm($, header, settings, guildChannels, guildRoles) {
  47. var readonly = ( process.env.READONLY ? true : false );
  48. var fields = [];
  49. let channel = $('<div>').append(fieldset.channel);
  50. channel.find('#wb-settings-channel').append(
  51. $('<option class="wb-settings-channel-default defaultSelect" hidden>').val('').text('-- Select a Channel --'),
  52. ...guildChannels.filter( guildChannel => {
  53. return ( hasPerm(guildChannel.userPermissions, 'VIEW_CHANNEL') || settings.channel.includes( '|' + guildChannel.id + '|' ) );
  54. } ).map( guildChannel => {
  55. var optionChannel = $(`<option class="wb-settings-channel-${guildChannel.id}">`).val(guildChannel.id);
  56. if ( !hasPerm(guildChannel.userPermissions, 'VIEW_CHANNEL') ) {
  57. optionChannel.addClass('wb-settings-error');
  58. }
  59. return optionChannel.text(`${guildChannel.id} – #${guildChannel.name}`);
  60. } )
  61. );
  62. if ( settings.channel ) {
  63. let settingsChannels = settings.channel.split('|').filter( guildChannel => guildChannel.length );
  64. channel.find('#wb-settings-channel').append(
  65. ...settingsChannels.filter( guildChannel => {
  66. return !channel.find(`.wb-settings-channel-${guildChannel}`).length;
  67. } ).map( guildChannel => {
  68. return $(`<option class="wb-settings-channel-${guildChannel}">`).val(guildChannel).text(`${guildChannel} – #UNKNOWN`).addClass('wb-settings-error');
  69. } )
  70. );
  71. if ( settingsChannels.length > 1 ) channel.find('#wb-settings-channel').after(
  72. ...settingsChannels.slice(1).map( guildChannel => {
  73. var additionalChannel = channel.find('#wb-settings-channel').clone();
  74. additionalChannel.addClass('wb-settings-additional-select');
  75. additionalChannel.find(`.wb-settings-channel-default`).removeAttr('hidden');
  76. additionalChannel.find(`.wb-settings-channel-${guildChannel}`).attr('selected', '');
  77. return additionalChannel.removeAttr('id').removeAttr('required');
  78. } )
  79. );
  80. channel.find(`#wb-settings-channel .wb-settings-channel-${settingsChannels[0]}`).attr('selected', '');
  81. }
  82. else {
  83. channel.find('.wb-settings-channel-default').attr('selected', '');
  84. channel.find('button.addmore').attr('hidden', '');
  85. }
  86. fields.push(channel);
  87. let role = $('<div>').append(fieldset.role);
  88. role.find('#wb-settings-role').append(
  89. $('<option class="wb-settings-role-default defaultSelect" hidden>').val('').text('-- Select a Role --'),
  90. ...guildRoles.filter( guildRole => {
  91. return guildRole.lower || settings.role.split('|').includes( guildRole.id );
  92. } ).map( guildRole => {
  93. var optionRole = $(`<option class="wb-settings-role-${guildRole.id}">`).val(guildRole.id);
  94. if ( !guildRole.lower ) optionRole.addClass('wb-settings-error');
  95. return optionRole.text(`${guildRole.id} – @${guildRole.name}`);
  96. } )
  97. );
  98. if ( settings.role ) {
  99. let settingsRoles = settings.role.split('|');
  100. role.find('#wb-settings-role').append(
  101. ...settingsRoles.filter( guildRole => {
  102. return !role.find(`.wb-settings-role-${guildRole}`).length;
  103. } ).map( guildRole => {
  104. return $(`<option class="wb-settings-role-${guildRole}">`).val(guildRole).text(`${guildRole} – @UNKNOWN`).addClass('wb-settings-error');
  105. } )
  106. );
  107. if ( settingsRoles.length > 1 ) role.find('#wb-settings-role').after(
  108. ...settingsRoles.slice(1).map( guildRole => {
  109. var additionalRole = role.find('#wb-settings-role').clone();
  110. additionalRole.addClass('wb-settings-additional-select');
  111. additionalRole.find(`.wb-settings-role-default`).removeAttr('hidden');
  112. additionalRole.find(`.wb-settings-role-${guildRole}`).attr('selected', '');
  113. return additionalRole.removeAttr('id').removeAttr('required');
  114. } )
  115. );
  116. role.find(`#wb-settings-role .wb-settings-role-${settingsRoles[0]}`).attr('selected', '');
  117. }
  118. else {
  119. if ( role.find(`.wb-settings-role-${settings.defaultrole}`).length ) {
  120. role.find(`.wb-settings-role-${settings.defaultrole}`).attr('selected', '');
  121. }
  122. else role.find('.wb-settings-role-default').attr('selected', '');
  123. role.find('button.addmore').attr('hidden', '');
  124. }
  125. fields.push(role);
  126. let usergroup = $('<div>').append(fieldset.usergroup);
  127. if ( settings.usergroup.startsWith( 'AND|' ) ) {
  128. settings.usergroup = settings.usergroup.substring(4);
  129. usergroup.find('#wb-settings-usergroup-and').attr('checked', '');
  130. }
  131. usergroup.find('#wb-settings-usergroup').val(settings.usergroup.split('|').join(', '));
  132. fields.push(usergroup);
  133. let editcount = $('<div>').append(fieldset.editcount);
  134. editcount.find('#wb-settings-editcount').val(settings.editcount);
  135. fields.push(editcount);
  136. let accountage = $('<div>').append(fieldset.accountage);
  137. accountage.find('#wb-settings-accountage').val(settings.accountage);
  138. fields.push(accountage);
  139. let rename = $('<div>').append(fieldset.rename);
  140. if ( settings.rename ) rename.find('#wb-settings-rename').attr('checked', '');
  141. fields.push(rename);
  142. fields.push($(fieldset.save).val('Save'));
  143. if ( settings.channel ) {
  144. fields.push($(fieldset.delete).val('Delete').attr('onclick', `return confirm('Are you sure?');`));
  145. }
  146. var form = $('<fieldset>').append(...fields);
  147. if ( readonly ) {
  148. form.find('input').attr('readonly', '');
  149. form.find('input[type="checkbox"], option').attr('disabled', '');
  150. form.find('input[type="submit"], button.addmore').remove();
  151. }
  152. return $('<form id="wb-settings" method="post" enctype="application/x-www-form-urlencoded">').append(
  153. $('<h2>').text(header),
  154. form
  155. );
  156. }
  157. /**
  158. * Let a user change verifications
  159. * @param {import('http').ServerResponse} res - The server response
  160. * @param {import('cheerio')} $ - The response body
  161. * @param {import('./util.js').Guild} guild - The current guild
  162. * @param {String[]} args - The url parts
  163. */
  164. function dashboard_verification(res, $, guild, args) {
  165. if ( !hasPerm(guild.botPermissions, 'MANAGE_ROLES') ) {
  166. $('#text .description').text('Wiki-Bot is missing the "MANAGE_ROLES" permission!\n\n*Insert explanation about verification here*');
  167. $('.channel#verification').addClass('selected');
  168. let body = $.html();
  169. res.writeHead(200, {'Content-Length': body.length});
  170. res.write( body );
  171. return res.end();
  172. }
  173. db.all( 'SELECT wiki, discord.role defaultrole, configid, verification.channel, verification.role, editcount, usergroup, accountage, rename FROM discord LEFT JOIN verification ON discord.guild = verification.guild WHERE discord.guild = ? AND discord.channel IS NULL ORDER BY configid ASC', [guild.id], function(dberror, rows) {
  174. if ( dberror ) {
  175. console.log( '- Dashboard: Error while getting the verifications: ' + dberror );
  176. $('#text .description').text('Failed to load the verifications!');
  177. $('.channel#verification').addClass('selected');
  178. let body = $.html();
  179. res.writeHead(200, {'Content-Length': body.length});
  180. res.write( body );
  181. return res.end();
  182. }
  183. if ( rows.length === 0 ) {
  184. $('#text .description').text(`You need to set up the server first: /guild/${guild.id}/settings`);
  185. $('.channel#verification').addClass('selected');
  186. let body = $.html();
  187. res.writeHead(200, {'Content-Length': body.length});
  188. res.write( body );
  189. return res.end();
  190. }
  191. var wiki = rows[0].wiki;
  192. var defaultrole = rows[0].defaultrole;
  193. if ( rows.length === 1 && rows[0].configid === null ) rows.pop();
  194. $('#text .description').text(`These are the verifications for "${guild.name}":`);
  195. $('#channellist #verification').after(
  196. ...rows.map( row => {
  197. return $('<a class="channel">').attr('id', `channel-${row.configid}`).append(
  198. $('<img>').attr('src', '/src/channel.svg'),
  199. $('<div>').text(`${row.configid} - ${( guild.roles.find( role => {
  200. return role.id === row.role.split('|')[0];
  201. } )?.name || guild.channels.find( channel => {
  202. return channel.id === row.channel.split('|')[1];
  203. } )?.name || row.usergroup.split('|')[0] )}`)
  204. ).attr('href', `/guild/${guild.id}/verification/${row.configid}`);
  205. } ),
  206. ( process.env.READONLY || rows.length >= verificationLimit[( guild.patreon ? 'patreon' : 'default' )] ? '' :
  207. $('<a class="channel" id="channel-new">').append(
  208. $('<img>').attr('src', '/src/channel.svg'),
  209. $('<div>').text('New verification')
  210. ).attr('href', `/guild/${guild.id}/verification/new`) )
  211. );
  212. if ( args[4] === 'new' ) {
  213. $('.channel#channel-new').addClass('selected');
  214. createForm($, 'New Verification', {
  215. channel: '', role: '', usergroup: 'user',
  216. editcount: 0, accountage: 0, rename: false, defaultrole
  217. }, guild.channels, guild.roles).attr('action', `/guild/${guild.id}/verification/new`).appendTo('#text');
  218. }
  219. else if ( rows.some( row => row.configid.toString() === args[4] ) ) {
  220. let row = rows.find( row => row.configid.toString() === args[4] );
  221. $(`.channel#channel-${row.configid}`).addClass('selected');
  222. createForm($, `Verification #${row.configid}`, row, guild.channels, guild.roles).attr('action', `/guild/${guild.id}/verification/${row.configid}`).appendTo('#text');
  223. }
  224. else {
  225. $('.channel#verification').addClass('selected');
  226. $('#text .description').text('*Insert explanation about verification here*');
  227. }
  228. let body = $.html();
  229. res.writeHead(200, {'Content-Length': body.length});
  230. res.write( body );
  231. return res.end();
  232. } );
  233. }
  234. /**
  235. * Change verifications
  236. * @param {Function} res - The server response
  237. * @param {import('./util.js').Settings} userSettings - The settings of the user
  238. * @param {String} guild - The id of the guild
  239. * @param {String|Number} type - The setting to change
  240. * @param {Object} settings - The new settings
  241. * @param {String[]} settings.channel
  242. * @param {String[]} settings.role
  243. * @param {String[]} [settings.usergroup]
  244. * @param {String} [settings.usergroup_and]
  245. * @param {Number} settings.editcount
  246. * @param {Number} settings.accountage
  247. * @param {String} [settings.rename]
  248. * @param {String} [settings.save_settings]
  249. * @param {String} [settings.delete_settings]
  250. */
  251. function update_verification(res, userSettings, guild, type, settings) {
  252. if ( type === 'default' ) {
  253. return res(`/guild/${guild}/verification?save=failed`);
  254. }
  255. if ( !settings.save_settings === !settings.delete_settings ) {
  256. return res(`/guild/${guild}/verification/${type}?save=failed`);
  257. }
  258. if ( settings.save_settings ) {
  259. if ( !/^[\d|]+ [\d|]+$/.test(`${settings.channel} ${settings.role}`) ) {
  260. return res(`/guild/${guild}/verification/${type}?save=failed`);
  261. }
  262. if ( !/^\d+ \d+$/.test(`${settings.editcount} ${settings.accountage}`) ) {
  263. return res(`/guild/${guild}/verification/${type}?save=failed`);
  264. }
  265. settings.channel = settings.channel.split('|').filter( (channel, i, self) => {
  266. return ( channel.length && self.indexOf(channel) === i );
  267. } );
  268. if ( !settings.channel.length || settings.channel.length > 10 ) {
  269. return res(`/guild/${guild}/verification/${type}?save=failed`);
  270. }
  271. settings.role = settings.role.split('|').filter( (role, i, self) => {
  272. return ( role.length && self.indexOf(role) === i );
  273. } );
  274. if ( !settings.role.length || settings.role.length > 10 ) {
  275. return res(`/guild/${guild}/verification/${type}?save=failed`);
  276. }
  277. if ( !settings.usergroup ) settings.usergroup = 'user';
  278. settings.usergroup = settings.usergroup.replace( /_/g, ' ' ).trim().toLowerCase();
  279. settings.usergroup = settings.usergroup.split(/\s*[,|]\s*/).map( usergroup => {
  280. if ( usergroup === '*' ) return 'user';
  281. return usergroup.replace( / /g, '_' );
  282. } ).filter( (usergroup, i, self) => {
  283. return ( usergroup.length && self.indexOf(usergroup) === i );
  284. } );
  285. if ( !settings.usergroup.length ) settings.usergroup.push('user');
  286. if ( settings.usergroup.length > 10 || settings.usergroup.some( usergroup => {
  287. return ( usergroup.length > 100 );
  288. } ) ) return res(`/guild/${guild}/verification/${type}?save=failed`);
  289. settings.editcount = parseInt(settings.editcount, 10);
  290. settings.accountage = parseInt(settings.accountage, 10);
  291. if ( type === 'new' ) {
  292. let curGuild = userSettings.guilds.isMember.get(guild);
  293. if ( settings.channel.some( channel => {
  294. return !curGuild.channels.some( guildChannel => guildChannel.id === channel );
  295. } ) || settings.role.some( role => {
  296. return !curGuild.roles.some( guildRole => guildRole.id === role && guildRole.lower );
  297. } ) ) return res(`/guild/${guild}/verification/new?save=failed`);
  298. }
  299. }
  300. if ( settings.delete_settings && type === 'new' ) {
  301. return res(`/guild/${guild}/verification/new?save=failed`);
  302. }
  303. if ( type !== 'new' ) type = parseInt(type, 10);
  304. sendMsg( {
  305. type: 'getMember',
  306. member: userSettings.user.id,
  307. guild: guild
  308. } ).then( response => {
  309. if ( !response ) {
  310. userSettings.guilds.notMember.set(guild, userSettings.guilds.isMember.get(guild));
  311. userSettings.guilds.isMember.delete(guild);
  312. return res(`/guild/${guild}?save=failed`);
  313. }
  314. if ( response === 'noMember' || !hasPerm(response.userPermissions, 'MANAGE_GUILD') ) {
  315. userSettings.guilds.isMember.delete(guild);
  316. return res('/?save=failed');
  317. }
  318. if ( settings.delete_settings ) return db.get( 'SELECT lang, verification.channel, verification.role, editcount, usergroup, accountage, rename FROM discord LEFT JOIN verification ON discord.guild = verification.guild AND configid = ? WHERE discord.guild = ? AND discord.channel IS NULL', [type, guild], function(dberror, row) {
  319. if ( !dberror && !row?.channel ) return res(`/guild/${guild}/verification?save=success`);
  320. db.run( 'DELETE FROM verification WHERE guild = ? AND configid = ?', [guild, type], function (delerror) {
  321. if ( delerror ) {
  322. console.log( '- Dashboard: Error while removing the verification: ' + delerror );
  323. return res(`/guild/${guild}/verification/${type}?save=failed`);
  324. }
  325. console.log( `- Dashboard: Verification successfully removed: ${guild}#${type}` );
  326. res(`/guild/${guild}/verification?save=success`);
  327. if ( dberror ) {
  328. console.log( '- Dashboard: Error while notifying the guild: ' + dberror );
  329. return;
  330. }
  331. var lang = new Lang(row.lang);
  332. var text = lang.get('verification.dashboard.removed', `<@${userSettings.user.id}>`, type);
  333. if ( row ) {
  334. text += '\n' + lang.get('verification.channel') + ' <#' + row.channel.split('|').filter( channel => channel.length ).join('>, <#') + '>';
  335. text += '\n' + lang.get('verification.role') + ' <@&' + row.role.split('|').join('>, <@&') + '>';
  336. text += '\n' + lang.get('verification.editcount') + ' `' + row.editcount + '`';
  337. text += '\n' + lang.get('verification.usergroup') + ' `' + ( row.usergroup.startsWith( 'AND|' ) ? row.usergroup.split('|').slice(1).join('` ' + lang.get('verification.and') + ' `') : row.usergroup.split('|').join('` ' + lang.get('verification.or') + ' `') ) + '`';
  338. text += '\n' + lang.get('verification.accountage') + ' `' + row.accountage + '` ' + lang.get('verification.indays');
  339. text += '\n' + lang.get('verification.rename') + ' *`' + lang.get('verification.' + ( row.rename ? 'enabled' : 'disabled')) + '`*';
  340. }
  341. text += `\n<${new URL(`/guild/${guild}/verification`, process.env.dashboard).href}>`;
  342. sendMsg( {
  343. type: 'notifyGuild', guild, text
  344. } ).catch( error => {
  345. console.log( '- Dashboard: Error while notifying the guild: ' + error );
  346. } );
  347. } );
  348. } );
  349. if ( !hasPerm(response.botPermissions, 'MANAGE_ROLES') ) {
  350. return res(`/guild/${guild}/verification?save=failed`);
  351. }
  352. if ( type === 'new' ) return db.get( 'SELECT wiki, lang, GROUP_CONCAT(configid) count FROM discord LEFT JOIN verification ON discord.guild = verification.guild WHERE discord.guild = ? AND discord.channel IS NULL', [guild], function(curerror, row) {
  353. if ( curerror ) {
  354. console.log( '- Dashboard: Error while checking for verifications: ' + curerror );
  355. return res(`/guild/${guild}/verification/new?save=failed`);
  356. }
  357. if ( !row ) return res(`/guild/${guild}/verification?save=failed`);
  358. if ( row.count === null ) row.count = [];
  359. else row.count = row.count.split(',').map( configid => parseInt(configid, 10) );
  360. if ( row.count.length >= verificationLimit[( response.patreon ? 'patreon' : 'default' )] ) {
  361. return res(`/guild/${guild}/verification?save=failed`);
  362. }
  363. return got.get( row.wiki + 'api.php?action=query&meta=allmessages&amprefix=group-&amincludelocal=true&amenableparser=true&format=json' ).then( gresponse => {
  364. var body = gresponse.body;
  365. if ( gresponse.statusCode !== 200 || !body || !body.query || !body.query.allmessages ) {
  366. console.log( '- Dashboard: ' + gresponse.statusCode + ': Error while getting the usergroups: ' + body?.error?.info );
  367. return;
  368. }
  369. var groups = body.query.allmessages.filter( group => {
  370. if ( group.name === 'group-all' ) return false;
  371. if ( group.name === 'group-membership-link-with-expiry' ) return false;
  372. if ( group.name.endsWith( '.css' ) || group.name.endsWith( '.js' ) ) return false;
  373. return true;
  374. } ).map( group => {
  375. return {
  376. name: group.name.replace( /^group-/, '' ).replace( /-member$/, '' ),
  377. content: group['*'].replace( / /g, '_' ).toLowerCase()
  378. };
  379. } );
  380. settings.usergroup = settings.usergroup.map( usergroup => {
  381. if ( groups.some( group => group.name === usergroup ) ) return usergroup;
  382. if ( groups.some( group => group.content === usergroup ) ) {
  383. return groups.find( group => group.content === usergroup ).name;
  384. }
  385. if ( /^admins?$/.test(usergroup) ) return 'sysop';
  386. if ( usergroup === '*' ) return 'user';
  387. return usergroup;
  388. } );
  389. }, error => {
  390. console.log( '- Dashboard: Error while getting the usergroups: ' + error );
  391. } ).finally( () => {
  392. if ( settings.usergroup_and ) settings.usergroup.unshift('AND');
  393. var configid = 1;
  394. for ( let i of row.count ) {
  395. if ( configid === i ) configid++;
  396. else break;
  397. }
  398. db.run( 'INSERT INTO verification(guild, configid, channel, role, editcount, usergroup, accountage, rename) VALUES(?, ?, ?, ?, ?, ?, ?, ?)', [guild, configid, '|' + settings.channel.join('|') + '|', settings.role.join('|'), settings.editcount, settings.usergroup.join('|'), settings.accountage, ( settings.rename ? 1 : 0 )], function (dberror) {
  399. if ( dberror ) {
  400. console.log( '- Dashboard: Error while adding the verification: ' + dberror );
  401. return res(`/guild/${guild}/verification/new?save=failed`);
  402. }
  403. console.log( `- Dashboard: Verification successfully added: ${guild}#${configid}` );
  404. res(`/guild/${guild}/verification/${configid}?save=success`);
  405. var lang = new Lang(row.lang);
  406. var text = lang.get('verification.dashboard.added', `<@${userSettings.user.id}>`, configid);
  407. text += '\n' + lang.get('verification.channel') + ' <#' + settings.channel.join('>, <#') + '>';
  408. text += '\n' + lang.get('verification.role') + ' <@&' + settings.role.join('>, <@&') + '>';
  409. text += '\n' + lang.get('verification.editcount') + ' `' + settings.editcount + '`';
  410. text += '\n' + lang.get('verification.usergroup') + ' `' + ( settings.usergroup_and ? settings.usergroup.slice(1).join('` ' + lang.get('verification.and') + ' `') : settings.usergroup.join('` ' + lang.get('verification.or') + ' `') ) + '`';
  411. text += '\n' + lang.get('verification.accountage') + ' `' + settings.accountage + '` ' + lang.get('verification.indays');
  412. text += '\n' + lang.get('verification.rename') + ' *`' + lang.get('verification.' + ( settings.rename ? 'enabled' : 'disabled')) + '`*';
  413. text += `\n<${new URL(`/guild/${guild}/verification/${configid}`, process.env.dashboard).href}>`;
  414. if ( settings.rename && !hasPerm(response.botPermissions, 'MANAGE_NICKNAMES') ) {
  415. text += '\n\n' + lang.get('verification.rename_no_permission', `<@${process.env.bot}>`);
  416. }
  417. if ( settings.role.some( role => {
  418. return !userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  419. return ( guildRole.id === role && guildRole.lower );
  420. } );
  421. } ) ) {
  422. text += '\n';
  423. settings.role.forEach( role => {
  424. if ( !userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  425. return ( guildRole.id === role );
  426. } ) ) {
  427. text += '\n' + lang.get('verification.role_deleted', `<@&${role}>`);
  428. }
  429. else if ( userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  430. return ( guildRole.id === role && !guildRole.lower );
  431. } ) ) {
  432. text += '\n' + lang.get('verification.role_too_high', `<@&${role}>`, `<@${process.env.bot}>`);
  433. }
  434. } );
  435. }
  436. sendMsg( {
  437. type: 'notifyGuild', guild, text
  438. } ).catch( error => {
  439. console.log( '- Dashboard: Error while notifying the guild: ' + error );
  440. } );
  441. } );
  442. } );
  443. } );
  444. return db.get( 'SELECT wiki, lang, verification.channel, verification.role, editcount, usergroup, accountage, rename FROM discord LEFT JOIN verification ON discord.guild = verification.guild AND verification.configid = ? WHERE discord.guild = ? AND discord.channel IS NULL', [type, guild], function(curerror, row) {
  445. if ( curerror ) {
  446. console.log( '- Dashboard: Error while checking for verifications: ' + curerror );
  447. return res(`/guild/${guild}/verification/${type}?save=failed`);
  448. }
  449. if ( !row?.channel ) return res(`/guild/${guild}/verification?save=failed`);
  450. row.channel = row.channel.split('|').filter( channel => channel.length );
  451. var newChannel = settings.channel.filter( channel => !row.channel.includes( channel ) );
  452. row.role = row.role.split('|');
  453. var newRole = settings.role.filter( role => !row.role.includes( role ) );
  454. row.usergroup = row.usergroup.split('|');
  455. var newUsergroup = settings.usergroup.filter( group => !row.usergroup.includes( group ) );
  456. if ( newChannel.length || newRole.length ) {
  457. let curGuild = userSettings.guilds.isMember.get(guild);
  458. if ( newChannel.some( channel => {
  459. return !curGuild.channels.some( guildChannel => guildChannel.id === channel );
  460. } ) || newRole.some( role => {
  461. return !curGuild.roles.some( guildRole => guildRole.id === role && guildRole.lower );
  462. } ) ) return res(`/guild/${guild}/verification/${type}?save=failed`);
  463. }
  464. ( newUsergroup.length ? got.get( row.wiki + 'api.php?action=query&meta=allmessages&amprefix=group-&amincludelocal=true&amenableparser=true&format=json' ).then( gresponse => {
  465. var body = gresponse.body;
  466. if ( gresponse.statusCode !== 200 || !body || !body.query || !body.query.allmessages ) {
  467. console.log( '- Dashboard: ' + gresponse.statusCode + ': Error while getting the usergroups: ' + body?.error?.info );
  468. return;
  469. }
  470. var groups = body.query.allmessages.filter( group => {
  471. if ( group.name === 'group-all' ) return false;
  472. if ( group.name === 'group-membership-link-with-expiry' ) return false;
  473. if ( group.name.endsWith( '.css' ) || group.name.endsWith( '.js' ) ) return false;
  474. return true;
  475. } ).map( group => {
  476. return {
  477. name: group.name.replace( /^group-/, '' ).replace( /-member$/, '' ),
  478. content: group['*'].replace( / /g, '_' ).toLowerCase()
  479. };
  480. } );
  481. settings.usergroup = settings.usergroup.map( usergroup => {
  482. if ( groups.some( group => group.name === usergroup ) ) return usergroup;
  483. if ( groups.some( group => group.content === usergroup ) ) {
  484. return groups.find( group => group.content === usergroup ).name;
  485. }
  486. if ( /^admins?$/.test(usergroup) ) return 'sysop';
  487. if ( usergroup === '*' ) return 'user';
  488. return usergroup;
  489. } );
  490. }, error => {
  491. console.log( '- Dashboard: Error while getting the usergroups: ' + error );
  492. } ) : Promise.resolve() ).finally( () => {
  493. if ( settings.usergroup_and ) settings.usergroup.unshift('AND');
  494. var lang = new Lang(row.lang);
  495. var diff = [];
  496. if ( newChannel.length || row.channel.some( channel => {
  497. return !settings.channel.includes( channel );
  498. } ) ) {
  499. diff.push(lang.get('verification.channel') + ` ~~<#${row.channel.join('>, <#')}>~~ → <#${settings.channel.join('>, <#')}>`);
  500. }
  501. if ( newRole.length || row.role.some( role => {
  502. return !settings.role.includes( role );
  503. } ) ) {
  504. diff.push(lang.get('verification.role') + ` ~~<@&${row.role.join('>, <@&')}>~~ → <@&${settings.role.join('>, <@&')}>`);
  505. }
  506. if ( row.editcount !== settings.editcount ) {
  507. diff.push(lang.get('verification.editcount') + ` ~~\`${row.editcount}\`~~ → \`${settings.editcount}\``);
  508. }
  509. if ( newUsergroup.length || row.usergroup.some( usergroup => {
  510. return !settings.usergroup.includes( usergroup );
  511. } ) ) {
  512. diff.push(lang.get('verification.usergroup') + ' ~~`' + ( row.usergroup[0] === 'AND' ? row.usergroup.slice(1).join('` ' + lang.get('verification.and') + ' `') : row.usergroup.join('` ' + lang.get('verification.or') + ' `') ) + '`~~ → `' + ( settings.usergroup_and ? settings.usergroup.slice(1).join('` ' + lang.get('verification.and') + ' `') : settings.usergroup.join('` ' + lang.get('verification.or') + ' `') ) + '`');
  513. }
  514. if ( row.accountage !== settings.accountage ) {
  515. diff.push(lang.get('verification.accountage') + ` ~~\`${row.accountage}\`~~ → \`${settings.accountage}\``);
  516. }
  517. if ( row.rename !== ( settings.rename ? 1 : 0 ) ) {
  518. diff.push(lang.get('verification.rename') + ` ~~*\`${lang.get('verification.' + ( row.rename ? 'enabled' : 'disabled'))}\`*~~ → *\`${lang.get('verification.' + ( settings.rename ? 'enabled' : 'disabled'))}\`*`);
  519. }
  520. if ( !diff.length ) return res(`/guild/${guild}/verification/${type}?save=success`);
  521. db.run( 'UPDATE verification SET channel = ?, role = ?, editcount = ?, usergroup = ?, accountage = ?, rename = ? WHERE guild = ? AND configid = ?', ['|' + settings.channel.join('|') + '|', settings.role.join('|'), settings.editcount, settings.usergroup.join('|'), settings.accountage, ( settings.rename ? 1 : 0 ), guild, type], function (dberror) {
  522. if ( dberror ) {
  523. console.log( '- Dashboard: Error while updating the verification: ' + dberror );
  524. return res(`/guild/${guild}/verification/${type}?save=failed`);
  525. }
  526. console.log( `- Dashboard: Verification successfully unpdated: ${guild}#${type}` );
  527. res(`/guild/${guild}/verification/${type}?save=success`);
  528. var text = lang.get('verification.dashboard.updated', `<@${userSettings.user.id}>`, type);
  529. text += '\n' + diff.join('\n');
  530. text += `\n<${new URL(`/guild/${guild}/verification/${type}`, process.env.dashboard).href}>`;
  531. if ( settings.rename && !hasPerm(response.botPermissions, 'MANAGE_NICKNAMES') ) {
  532. text += '\n\n' + lang.get('verification.rename_no_permission', `<@${process.env.bot}>`);
  533. }
  534. if ( settings.role.some( role => {
  535. return !userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  536. return ( guildRole.id === role && guildRole.lower );
  537. } );
  538. } ) ) {
  539. text += '\n';
  540. settings.role.forEach( role => {
  541. if ( !userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  542. return ( guildRole.id === role );
  543. } ) ) {
  544. text += '\n' + lang.get('verification.role_deleted', `<@&${role}>`);
  545. }
  546. else if ( userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  547. return ( guildRole.id === role && !guildRole.lower );
  548. } ) ) {
  549. text += '\n' + lang.get('verification.role_too_high', `<@&${role}>`, `<@${process.env.bot}>`);
  550. }
  551. } );
  552. }
  553. sendMsg( {
  554. type: 'notifyGuild', guild, text
  555. } ).catch( error => {
  556. console.log( '- Dashboard: Error while notifying the guild: ' + error );
  557. } );
  558. } );
  559. } );
  560. } );
  561. }, error => {
  562. console.log( '- Dashboard: Error while getting the member: ' + error );
  563. return res(`/guild/${guild}/verification/${type}?save=failed`);
  564. } );
  565. }
  566. module.exports = {
  567. get: dashboard_verification,
  568. post: update_verification
  569. };