1
0

verification.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. import Lang from '../util/i18n.js';
  2. import { got, db, sendMsg, createNotice, escapeText, hasPerm } from './util.js';
  3. import { createRequire } from 'module';
  4. const require = createRequire(import.meta.url);
  5. const {limit: {verification: verificationLimit}, usergroups} = require('../util/default.json');
  6. const fieldset = {
  7. channel: '<div>'
  8. + '<label for="wb-settings-channel">Channel:</label>'
  9. + '<select id="wb-settings-channel" name="channel-0" required></select>'
  10. + '</div>'
  11. + '<button type="button" id="wb-settings-channel-more" class="addmore">Add more</button>',
  12. role: '<div>'
  13. + '<label for="wb-settings-role">Role:</label>'
  14. + '<select id="wb-settings-role" name="role-0" required></select>'
  15. + '<input type="radio" id="wb-settings-role-0-add" name="role-0-change" value="+">'
  16. + '<label for="wb-settings-role-0-add" class="radio-label">Add</label>'
  17. + '<input type="radio" id="wb-settings-role-0-remove" name="role-0-change" value="-">'
  18. + '<label for="wb-settings-role-0-remove" class="radio-label">Remove</label>'
  19. + '</div>'
  20. + '<button type="button" id="wb-settings-role-more" class="addmore">Add more</button>',
  21. usergroup: '<label for="wb-settings-usergroup">Wiki user group:</label>'
  22. + '<input type="text" id="wb-settings-usergroup" name="usergroup" list="wb-settings-usergroup-list" autocomplete="on">'
  23. + '<datalist id="wb-settings-usergroup-list">'
  24. + usergroups.sorted.filter( group => group !== '__CUSTOM__' ).map( group => {
  25. return `<option value="${group}"></option>`
  26. } ).join('')
  27. + usergroups.global.filter( group => group !== '__CUSTOM__' ).map( group => {
  28. return `<option value="${group}"></option>`
  29. } ).join('')
  30. + '</datalist>'
  31. + '<div id="wb-settings-usergroup-multiple">'
  32. + '<label for="wb-settings-usergroup-and">Require all user groups:</label>'
  33. + '<input type="checkbox" id="wb-settings-usergroup-and" name="usergroup_and">'
  34. + '</div>',
  35. editcount: '<label for="wb-settings-editcount">Minimal edit count:</label>'
  36. + '<input type="number" id="wb-settings-editcount" name="editcount" min="0" max="1000000" required>',
  37. postcount: '<div id="wb-settings-postcount-input">'
  38. + '<label for="wb-settings-postcount">Minimal post count:</label>'
  39. + '<input type="number" id="wb-settings-postcount" name="postcount" min="0" max="1000000" required>'
  40. + '</div><div class="wb-settings-postcount">'
  41. + '<span>Only Fandom wikis:</span>'
  42. + '<input type="radio" id="wb-settings-postcount-and" name="posteditcount" value="and" required>'
  43. + '<label for="wb-settings-postcount-and" class="radio-label">Require both edit and post count.</label>'
  44. + '</div><div class="wb-settings-postcount">'
  45. + '<input type="radio" id="wb-settings-postcount-or" name="posteditcount" value="or" required>'
  46. + '<label for="wb-settings-postcount-or" class="radio-label">Require either edit or post count.</label>'
  47. + '</div><div class="wb-settings-postcount">'
  48. + '<input type="radio" id="wb-settings-postcount-both" name="posteditcount" value="both" required>'
  49. + '<label for="wb-settings-postcount-both" class="radio-label">Require combined edit and post count.</label>'
  50. + '</div>',
  51. accountage: '<label for="wb-settings-accountage">Account age (in days):</label>'
  52. + '<input type="number" id="wb-settings-accountage" name="accountage" min="0" max="1000000" required>',
  53. rename: '<label for="wb-settings-rename">Rename users:</label>'
  54. + '<input type="checkbox" id="wb-settings-rename" name="rename">',
  55. save: '<input type="submit" id="wb-settings-save" name="save_settings">',
  56. delete: '<input type="submit" id="wb-settings-delete" name="delete_settings" formnovalidate>'
  57. };
  58. /**
  59. * Create a settings form
  60. * @param {import('cheerio').CheerioAPI} $ - The response body
  61. * @param {String} header - The form header
  62. * @param {import('./i18n.js').default} dashboardLang - The user language
  63. * @param {Object} settings - The current settings
  64. * @param {String} settings.channel
  65. * @param {String} settings.role
  66. * @param {String} settings.usergroup
  67. * @param {Number} settings.editcount
  68. * @param {Number} settings.postcount
  69. * @param {Number} settings.accountage
  70. * @param {Boolean} settings.rename
  71. * @param {String} [settings.defaultrole]
  72. * @param {import('./util.js').Channel[]} guildChannels - The guild channels
  73. * @param {import('./util.js').Role[]} guildRoles - The guild roles
  74. * @param {String} wiki - The guild wiki
  75. */
  76. function createForm($, header, dashboardLang, settings, guildChannels, guildRoles, wiki) {
  77. var readonly = ( process.env.READONLY ? true : false );
  78. var fields = [];
  79. let channel = $('<div>').append(fieldset.channel);
  80. channel.find('label').text(dashboardLang.get('verification.form.channel'));
  81. let curCat = null;
  82. channel.find('#wb-settings-channel').append(
  83. $('<option class="wb-settings-channel-default defaultSelect" hidden>').val('').text(dashboardLang.get('verification.form.select_channel')),
  84. ...guildChannels.filter( guildChannel => {
  85. return ( hasPerm(guildChannel.userPermissions, 'VIEW_CHANNEL') || guildChannel.isCategory || settings.channel.includes( '|' + guildChannel.id + '|' ) );
  86. } ).map( guildChannel => {
  87. if ( guildChannel.isCategory ) {
  88. curCat = $('<optgroup>').attr('label', guildChannel.name);
  89. return curCat;
  90. }
  91. var optionChannel = $(`<option class="wb-settings-channel-${guildChannel.id}">`).val(guildChannel.id).text(`${guildChannel.id} – #${guildChannel.name}`);
  92. if ( !hasPerm(guildChannel.userPermissions, 'VIEW_CHANNEL') ) {
  93. optionChannel.addClass('wb-settings-error');
  94. }
  95. if ( !curCat ) return optionChannel;
  96. optionChannel.appendTo(curCat);
  97. } ).filter( catChannel => {
  98. if ( !catChannel ) return false;
  99. if ( catChannel.is('optgroup') && !catChannel.children('option').length ) return false;
  100. return true;
  101. } )
  102. );
  103. if ( settings.channel ) {
  104. let settingsChannels = settings.channel.split('|').filter( guildChannel => guildChannel.length );
  105. channel.find('#wb-settings-channel').append(
  106. ...settingsChannels.filter( guildChannel => {
  107. return !channel.find(`.wb-settings-channel-${guildChannel}`).length;
  108. } ).map( guildChannel => {
  109. return $(`<option class="wb-settings-channel-${guildChannel}">`).val(guildChannel).text(`${guildChannel} – #UNKNOWN`).addClass('wb-settings-error');
  110. } )
  111. );
  112. if ( settingsChannels.length > 1 ) channel.find('div').after(
  113. ...settingsChannels.slice(1).map( (guildChannel, i) => {
  114. var additionalChannel = channel.find('#wb-settings-channel').clone();
  115. additionalChannel.find(`.wb-settings-channel-default`).removeAttr('hidden');
  116. additionalChannel.find(`.wb-settings-channel-${guildChannel}`).attr('selected', '');
  117. additionalChannel.removeAttr('id').removeAttr('required');
  118. additionalChannel.attr('name', 'channel-' + (i + 1));
  119. return $('<div>').addClass('wb-settings-additional-select').append(additionalChannel);
  120. } )
  121. );
  122. channel.find(`#wb-settings-channel .wb-settings-channel-${settingsChannels[0]}`).attr('selected', '');
  123. }
  124. else {
  125. channel.find('.wb-settings-channel-default').attr('selected', '');
  126. channel.find('button.addmore').attr('hidden', '');
  127. }
  128. fields.push(channel);
  129. let role = $('<div>').append(fieldset.role);
  130. role.find('label').eq(0).text(dashboardLang.get('verification.form.role'));
  131. role.find('label').eq(1).text(dashboardLang.get('verification.form.role_add'));
  132. role.find('label').eq(2).text(dashboardLang.get('verification.form.role_remove'));
  133. role.find('#wb-settings-role').append(
  134. $('<option class="wb-settings-role-default defaultSelect" hidden>').val('').text(dashboardLang.get('verification.form.select_role')),
  135. ...guildRoles.filter( guildRole => {
  136. return guildRole.lower || settings.role.replace( /-/g, '' ).split('|').includes( guildRole.id );
  137. } ).map( guildRole => {
  138. var optionRole = $(`<option class="wb-settings-role-${guildRole.id}">`).val(guildRole.id);
  139. if ( !guildRole.lower ) optionRole.addClass('wb-settings-error');
  140. return optionRole.text(`${guildRole.id} – @${guildRole.name}`);
  141. } )
  142. );
  143. if ( settings.role ) {
  144. let settingsRoles = settings.role.split('|').map( guildRole => {
  145. if ( !guildRole.startsWith( '-' ) ) return {id: guildRole, suffix: 'add'};
  146. return {id: guildRole.replace( '-', '' ), suffix: 'remove'};
  147. } );
  148. role.find('#wb-settings-role').append(
  149. ...settingsRoles.filter( guildRole => {
  150. return !role.find(`.wb-settings-role-${guildRole.id}`).length;
  151. } ).map( guildRole => {
  152. return $(`<option class="wb-settings-role-${guildRole.id}">`).val(guildRole.id).text(`${guildRole.id} – @UNKNOWN`).addClass('wb-settings-error');
  153. } )
  154. );
  155. if ( settingsRoles.length > 1 ) role.find('div').after(
  156. ...settingsRoles.slice(1).map( (guildRole, i) => {
  157. var id = i + 1;
  158. var additionalDiv = role.find('div').clone();
  159. additionalDiv.find('label').eq(0).remove();
  160. var additionalRole = additionalDiv.find('#wb-settings-role');
  161. additionalRole.find(`.wb-settings-role-default`).removeAttr('hidden');
  162. additionalRole.find(`.wb-settings-role-${guildRole.id}`).attr('selected', '');
  163. additionalRole.removeAttr('id').removeAttr('required').attr('name', 'role-' + id);
  164. additionalDiv.find('input').attr('name', 'role-' + id + '-change');
  165. additionalDiv.find('input').eq(0).attr('id', 'wb-settings-role-' + id + '-add');
  166. additionalDiv.find('label').eq(0).attr('for', 'wb-settings-role-' + id + '-add');
  167. additionalDiv.find('input').eq(1).attr('id', 'wb-settings-role-' + id + '-remove');
  168. additionalDiv.find('label').eq(1).attr('for', 'wb-settings-role-' + id + '-remove');
  169. additionalDiv.find(`#wb-settings-role-${id}-${guildRole.suffix}`).attr('checked', '');
  170. return additionalDiv.addClass('wb-settings-additional-select');
  171. } )
  172. );
  173. role.find(`#wb-settings-role .wb-settings-role-${settingsRoles[0].id}`).attr('selected', '');
  174. role.find(`#wb-settings-role-0-${settingsRoles[0].suffix}`).attr('checked', '');
  175. }
  176. else {
  177. if ( role.find(`.wb-settings-role-${settings.defaultrole}`).length ) {
  178. role.find(`.wb-settings-role-${settings.defaultrole}`).attr('selected', '');
  179. }
  180. else role.find('.wb-settings-role-default').attr('selected', '');
  181. role.find('#wb-settings-role-0-add').attr('checked', '');
  182. role.find('button.addmore').attr('hidden', '');
  183. }
  184. fields.push(role);
  185. let usergroup = $('<div>').append(fieldset.usergroup);
  186. usergroup.find('label').eq(0).text(dashboardLang.get('verification.form.usergroup'));
  187. usergroup.find('label').eq(1).text(dashboardLang.get('verification.form.usergroup_and'));
  188. if ( settings.usergroup.startsWith( 'AND|' ) ) {
  189. settings.usergroup = settings.usergroup.substring(4);
  190. usergroup.find('#wb-settings-usergroup-and').attr('checked', '');
  191. }
  192. usergroup.find('#wb-settings-usergroup').val(settings.usergroup.split('|').join(', '));
  193. if ( !settings.usergroup.includes( '|' ) ) {
  194. usergroup.find('#wb-settings-usergroup-multiple').attr('style', 'display: none;');
  195. }
  196. fields.push(usergroup);
  197. $('<script>').attr('src', wiki + 'api.php?action=query&meta=allmessages|siteinfo&amprefix=group-&amincludelocal=true&amenableparser=true&amlang=' + dashboardLang.lang + '&siprop=usergroups&format=json&callback=fillUsergroupList').attr('defer', '').insertAfter('script#indexjs');
  198. let editcount = $('<div>').append(fieldset.editcount);
  199. editcount.find('label').text(dashboardLang.get('verification.form.editcount'));
  200. editcount.find('#wb-settings-editcount').val(settings.editcount);
  201. fields.push(editcount);
  202. let postcount = $('<div>').append(fieldset.postcount);
  203. postcount.find('label').eq(0).text(dashboardLang.get('verification.form.postcount'));
  204. postcount.find('span').text(dashboardLang.get('verification.form.postcount_fandom'));
  205. postcount.find('label').eq(1).text(dashboardLang.get('verification.form.postcount_and'));
  206. postcount.find('label').eq(2).text(dashboardLang.get('verification.form.postcount_or'));
  207. postcount.find('label').eq(3).text(dashboardLang.get('verification.form.postcount_both'));
  208. postcount.find('#wb-settings-postcount').val(Math.abs(settings.postcount));
  209. if ( settings.postcount === null ) {
  210. postcount.find('#wb-settings-postcount-both').attr('checked', '');
  211. postcount.find('#wb-settings-postcount-input').attr('style', 'display: none;');
  212. }
  213. else if ( settings.postcount < 0 ) postcount.find('#wb-settings-postcount-or').attr('checked', '');
  214. else postcount.find('#wb-settings-postcount-and').attr('checked', '');
  215. fields.push(postcount);
  216. let accountage = $('<div>').append(fieldset.accountage);
  217. accountage.find('label').text(dashboardLang.get('verification.form.accountage'));
  218. accountage.find('#wb-settings-accountage').val(settings.accountage);
  219. fields.push(accountage);
  220. if ( settings.rename || guildChannels.some( guildChannel => {
  221. return hasPerm(guildChannel.botPermissions, 'MANAGE_NICKNAMES');
  222. } ) ) {
  223. let rename = $('<div>').append(fieldset.rename);
  224. rename.find('label').text(dashboardLang.get('verification.form.rename'));
  225. if ( settings.rename ) rename.find('#wb-settings-rename').attr('checked', '');
  226. fields.push(rename);
  227. }
  228. fields.push($(fieldset.save).val(dashboardLang.get('general.save')));
  229. if ( settings.channel ) {
  230. fields.push($(fieldset.delete).val(dashboardLang.get('general.delete')).attr('onclick', `return confirm('${dashboardLang.get('verification.form.confirm').replace( /'/g, '\\$&' )}');`));
  231. }
  232. var form = $('<fieldset>').append(...fields);
  233. if ( readonly ) {
  234. form.find('input').attr('readonly', '');
  235. form.find('input[type="checkbox"], input[type="radio"]:not(:checked), option, optgroup').attr('disabled', '');
  236. form.find('input[type="submit"], button.addmore').remove();
  237. }
  238. form.find('button.addmore').text(dashboardLang.get('verification.form.more'));
  239. return $('<form id="wb-settings" method="post" enctype="application/x-www-form-urlencoded">').append(
  240. $('<h2>').text(header),
  241. form
  242. );
  243. }
  244. /**
  245. * Let a user change verifications
  246. * @param {import('http').ServerResponse} res - The server response
  247. * @param {import('cheerio').CheerioAPI} $ - The response body
  248. * @param {import('./util.js').Guild} guild - The current guild
  249. * @param {String[]} args - The url parts
  250. * @param {import('./i18n.js').default} dashboardLang - The user language
  251. */
  252. function dashboard_verification(res, $, guild, args, dashboardLang) {
  253. db.query( 'SELECT wiki, discord.role defaultrole, prefix, configid, verification.channel, verification.role, editcount, postcount, usergroup, accountage, rename FROM discord LEFT JOIN verification ON discord.guild = verification.guild WHERE discord.guild = $1 AND discord.channel IS NULL ORDER BY configid ASC', [guild.id] ).then( ({rows}) => {
  254. if ( rows.length === 0 ) {
  255. createNotice($, 'nosettings', dashboardLang, [guild.id]);
  256. $('#text .description').html(dashboardLang.get('verification.explanation'));
  257. $('#text code.prefix').prepend(escapeText(process.env.prefix));
  258. $('.channel#verification').addClass('selected');
  259. let body = $.html();
  260. res.writeHead(200, {'Content-Length': Buffer.byteLength(body)});
  261. res.write( body );
  262. return res.end();
  263. }
  264. if ( !hasPerm(guild.botPermissions, 'MANAGE_ROLES') ) {
  265. createNotice($, 'missingperm', dashboardLang, ['Manage Roles']);
  266. $('#text .description').html(dashboardLang.get('verification.explanation'));
  267. $('#text code.prefix').prepend(escapeText(rows[0].prefix));
  268. $('.channel#verification').addClass('selected');
  269. let body = $.html();
  270. res.writeHead(200, {'Content-Length': Buffer.byteLength(body)});
  271. res.write( body );
  272. return res.end();
  273. }
  274. var wiki = rows[0].wiki;
  275. var defaultrole = rows[0].defaultrole;
  276. var prefix = rows[0].prefix;
  277. if ( rows.length === 1 && rows[0].configid === null ) rows.pop();
  278. $('<p>').html(dashboardLang.get('verification.desc', true, $('<code>').text(guild.name))).appendTo('#text .description');
  279. let suffix = ( args[0] === 'owner' ? '?owner=true' : '' );
  280. $('#channellist #verification').after(
  281. ...rows.map( row => {
  282. let text = `${row.configid} - ${( guild.roles.find( role => {
  283. return role.id === row.role.replace( /-/g, '' ).split('|')[0];
  284. } )?.name || guild.channels.find( channel => {
  285. return channel.id === row.channel.split('|')[1];
  286. } )?.name || row.usergroup.split('|')[( row.usergroup.startsWith('AND|') ? 1 : 0 )] )}`;
  287. return $('<a class="channel">').attr('id', `channel-${row.configid}`).append(
  288. $('<img>').attr('src', '/src/channel.svg'),
  289. $('<div>').text(text)
  290. ).attr('title', text).attr('href', `/guild/${guild.id}/verification/${row.configid}${suffix}`);
  291. } ),
  292. ( process.env.READONLY || rows.length >= verificationLimit[( guild.patreon ? 'patreon' : 'default' )] ? '' :
  293. $('<a class="channel" id="channel-new">').append(
  294. $('<img>').attr('src', '/src/channel.svg'),
  295. $('<div>').text(dashboardLang.get('verification.new'))
  296. ).attr('href', `/guild/${guild.id}/verification/new${suffix}`) ),
  297. ( !rows.length ? '' :
  298. $('<a class="channel" id="channel-notice">').append(
  299. $('<img>').attr('src', '/src/channel.svg'),
  300. $('<div>').text(dashboardLang.get('verification.notice'))
  301. ).attr('href', `/guild/${guild.id}/verification/notice${suffix}`) )
  302. );
  303. if ( args[4] === 'new' && !( process.env.READONLY || rows.length >= verificationLimit[( guild.patreon ? 'patreon' : 'default' )] ) ) {
  304. $('.channel#channel-new').addClass('selected');
  305. createForm($, dashboardLang.get('verification.form.new'), dashboardLang, {
  306. channel: '', role: '', usergroup: 'user',
  307. editcount: 0, postcount: 0, accountage: 0,
  308. rename: false, defaultrole
  309. }, guild.channels, guild.roles, wiki).attr('action', `/guild/${guild.id}/verification/new`).appendTo('#text');
  310. }
  311. else if ( rows.some( row => row.configid.toString() === args[4] ) ) {
  312. let row = rows.find( row => row.configid.toString() === args[4] );
  313. $(`.channel#channel-${row.configid}`).addClass('selected');
  314. createForm($, dashboardLang.get('verification.form.entry', false, row.configid), dashboardLang, row, guild.channels, guild.roles, wiki).attr('action', `/guild/${guild.id}/verification/${row.configid}`).appendTo('#text');
  315. }
  316. else if ( args[4] === 'notice' && rows.length ) {
  317. $(`.channel#channel-notice`).addClass('selected');
  318. return db.query( 'SELECT logchannel, flags, onsuccess, onmatch FROM verifynotice WHERE guild = $1', [guild.id] ).then( ({rows:[row]}) => {
  319. let curCat = null;
  320. $('<form id="wb-settings" method="post" enctype="application/x-www-form-urlencoded">').append(
  321. $('<h2>').text(dashboardLang.get('verification.form.notice')),
  322. $('<fieldset>').append(
  323. $('<div>').append(
  324. $('<label for="wb-settings-channel">').text(dashboardLang.get('verification.form.logging')),
  325. $('<select id="wb-settings-channel" name="channel">').append(
  326. $('<option class="wb-settings-channel-default defaultSelect">').val('').text(dashboardLang.get('verification.form.select_channel')),
  327. ...guild.channels.filter( guildChannel => {
  328. return ( ( hasPerm(guildChannel.botPermissions, 'VIEW_CHANNEL', 'SEND_MESSAGES') && hasPerm(guildChannel.userPermissions, 'VIEW_CHANNEL') ) || guildChannel.isCategory );
  329. } ).map( guildChannel => {
  330. if ( guildChannel.isCategory ) {
  331. curCat = $('<optgroup>').attr('label', guildChannel.name);
  332. return curCat;
  333. }
  334. var optionChannel = $(`<option class="wb-settings-channel-${guildChannel.id}">`).val(guildChannel.id).text(`${guildChannel.id} – #${guildChannel.name}`);
  335. if ( guildChannel.id === row?.logchannel ) optionChannel.attr('selected', '');
  336. if ( !curCat ) return optionChannel;
  337. optionChannel.appendTo(curCat);
  338. } ).filter( catChannel => {
  339. if ( !catChannel ) return false;
  340. if ( catChannel.is('optgroup') && !catChannel.children('option').length ) return false;
  341. return true;
  342. } )
  343. ),
  344. $('<div id="wb-settings-logall-hide">').append(
  345. $('<label for="wb-settings-flag_logall">').text(dashboardLang.get('verification.form.flag_logall')),
  346. $('<input type="checkbox" id="wb-settings-flag_logall" name="flag_logall">').attr('checked', ( (row?.flags & 1 << 1) === 1 << 1 ? '' : null ))
  347. ).attr('style', ( row?.logchannel ? '' : 'display: none;'))
  348. ),
  349. $('<div>').append(
  350. $('<label for="wb-settings-flag_private">').text(dashboardLang.get('verification.form.flag_private')),
  351. $('<input type="checkbox" id="wb-settings-flag_private" name="flag_private">').attr('checked', ( (row?.flags & 1 << 0) === 1 << 0 ? '' : null ))
  352. ),
  353. $('<div>').append(
  354. $('<label for="wb-settings-success">').text(dashboardLang.get('verification.form.success')).append(
  355. $('<div>').html('&nbsp;')
  356. ),
  357. $('<textarea id="wb-settings-success" name="success" spellcheck="true" maxlength="1000" cols="65">').attr('rows', ( row?.onsuccess || '' ).split('\n').length + 3).attr('placeholder', dashboardLang.get('verification.form.success_placeholder')).text(row?.onsuccess || '')
  358. ),
  359. $('<div>').append(
  360. $('<label for="wb-settings-match">').text(dashboardLang.get('verification.form.match')).append(
  361. $('<div>').html('&nbsp;')
  362. ),
  363. $('<textarea id="wb-settings-match" name="match" spellcheck="true" maxlength="1000" cols="65">').attr('rows', ( row?.onmatch || '' ).split('\n').length + 3).attr('placeholder', dashboardLang.get('verification.form.match_placeholder')).text(row?.onmatch || '')
  364. ),
  365. $('<input type="submit" id="wb-settings-save" name="save_settings">').val(dashboardLang.get('general.save'))
  366. )
  367. ).attr('action', `/guild/${guild.id}/verification/notice`).appendTo('#text');
  368. if ( process.env.READONLY ) {
  369. $('input, textarea').attr('readonly', '');
  370. $('textarea, option, optgroup').attr('disabled', '');
  371. $('input[type="submit"]').remove();
  372. }
  373. $('<div class="description">').html(dashboardLang.get('verification.help_notice')).appendTo('#text');
  374. let body = $.html();
  375. res.writeHead(200, {'Content-Length': Buffer.byteLength(body)});
  376. res.write( body );
  377. return res.end();
  378. }, dberror => {
  379. console.log( '- Dashboard: Error while getting the verification notices: ' + dberror );
  380. createNotice($, 'error', dashboardLang);
  381. $('#text .description').html(dashboardLang.get('verification.explanation'));
  382. $('#text code.prefix').prepend(escapeText(process.env.prefix));
  383. $('.channel#verification').addClass('selected');
  384. let body = $.html();
  385. res.writeHead(200, {'Content-Length': Buffer.byteLength(body)});
  386. res.write( body );
  387. return res.end();
  388. } );
  389. }
  390. else {
  391. $('.channel#verification').addClass('selected');
  392. $('#text .description').html(dashboardLang.get('verification.explanation'));
  393. $('#text code.prefix').prepend(escapeText(prefix));
  394. }
  395. let body = $.html();
  396. res.writeHead(200, {'Content-Length': Buffer.byteLength(body)});
  397. res.write( body );
  398. return res.end();
  399. }, dberror => {
  400. console.log( '- Dashboard: Error while getting the verifications: ' + dberror );
  401. createNotice($, 'error', dashboardLang);
  402. $('#text .description').html(dashboardLang.get('verification.explanation'));
  403. $('#text code.prefix').prepend(escapeText(process.env.prefix));
  404. $('.channel#verification').addClass('selected');
  405. let body = $.html();
  406. res.writeHead(200, {'Content-Length': Buffer.byteLength(body)});
  407. res.write( body );
  408. return res.end();
  409. } );
  410. }
  411. /**
  412. * Change verifications
  413. * @param {Function} res - The server response
  414. * @param {import('./util.js').Settings} userSettings - The settings of the user
  415. * @param {String} guild - The id of the guild
  416. * @param {String|Number} type - The setting to change
  417. * @param {Object} settings - The new settings
  418. * @param {String[]} [settings.usergroup]
  419. * @param {String} [settings.usergroup_and]
  420. * @param {Number} settings.editcount
  421. * @param {Number} [settings.postcount]
  422. * @param {String} settings.posteditcount
  423. * @param {Number} settings.accountage
  424. * @param {String} [settings.rename]
  425. * @param {String} [settings.save_settings]
  426. * @param {String} [settings.delete_settings]
  427. */
  428. function update_verification(res, userSettings, guild, type, settings) {
  429. if ( type === 'default' ) {
  430. return res(`/guild/${guild}/verification`, 'savefail');
  431. }
  432. if ( type === 'notice' ) return update_notices(res, userSettings, guild, type, settings);
  433. if ( !settings.save_settings === !settings.delete_settings ) {
  434. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  435. }
  436. /** @type {String[]} */
  437. var channels = [];
  438. /** @type {{id: String, prefix: String}[]} */
  439. var roles = [];
  440. if ( settings.save_settings ) {
  441. channels = Object.keys(settings).filter( channel => {
  442. return /^channel-\d$/.test(channel) && /^\d+$/.test(settings[channel]);
  443. } ).map( channel => settings[channel] );
  444. roles = Object.keys(settings).filter( role => {
  445. return /^role-\d$/.test(role) && /^\d+$/.test(settings[role]);
  446. } ).map( role => {
  447. return {id: settings[role], prefix: ( settings[role + '-change'] === '-' ? '-' : '' )};
  448. } );
  449. if ( !channels.length || !roles.length ) {
  450. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  451. }
  452. if ( !/^\d+ \d+$/.test(`${settings.editcount} ${settings.accountage}`) ) {
  453. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  454. }
  455. if ( !( ['and','or','both'].includes( settings.posteditcount ) && ( /^\d+$/.test(settings.postcount) || settings.posteditcount === 'both' ) ) ) {
  456. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  457. }
  458. channels = channels.filter( (channel, i, self) => {
  459. return self.indexOf(channel) === i;
  460. } );
  461. if ( !settings.usergroup ) settings.usergroup = 'user';
  462. settings.usergroup = settings.usergroup.replace( /_/g, ' ' ).trim().toLowerCase();
  463. settings.usergroup = settings.usergroup.split(/\s*[,|]\s*/).map( usergroup => {
  464. if ( usergroup === '*' ) return 'user';
  465. return usergroup.replace( / /g, '_' );
  466. } ).filter( (usergroup, i, self) => {
  467. return ( usergroup.length && self.indexOf(usergroup) === i );
  468. } );
  469. if ( !settings.usergroup.length ) settings.usergroup.push('user');
  470. if ( settings.usergroup.length > 10 || settings.usergroup.some( usergroup => {
  471. return ( usergroup.length > 100 );
  472. } ) ) return res(`/guild/${guild}/verification/${type}`, 'invalidusergroup');
  473. settings.editcount = parseInt(settings.editcount, 10);
  474. settings.accountage = parseInt(settings.accountage, 10);
  475. if ( settings.editcount > 1000000 || settings.accountage > 1000000 ) {
  476. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  477. }
  478. if ( settings.posteditcount === 'both' ) settings.postcount = null;
  479. else settings.postcount = parseInt(settings.postcount, 10);
  480. if ( settings.posteditcount === 'or' ) settings.postcount = settings.postcount * -1;
  481. if ( settings.postcount > 1000000 || settings.postcount < -1000000 ) {
  482. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  483. }
  484. if ( type === 'new' ) {
  485. let curGuild = userSettings.guilds.isMember.get(guild);
  486. if ( channels.some( channel => {
  487. return !curGuild.channels.some( guildChannel => {
  488. return ( guildChannel.id === channel && !guildChannel.isCategory );
  489. } );
  490. } ) || roles.some( role => {
  491. return !curGuild.roles.some( guildRole => {
  492. return ( guildRole.id === role.id && guildRole.lower );
  493. } );
  494. } ) ) return res(`/guild/${guild}/verification/new`, 'savefail');
  495. }
  496. }
  497. if ( settings.delete_settings && type === 'new' ) {
  498. return res(`/guild/${guild}/verification/new`, 'savefail');
  499. }
  500. if ( type !== 'new' ) type = parseInt(type, 10);
  501. sendMsg( {
  502. type: 'getMember',
  503. member: userSettings.user.id,
  504. guild: guild
  505. } ).then( response => {
  506. if ( !response ) {
  507. userSettings.guilds.notMember.set(guild, userSettings.guilds.isMember.get(guild));
  508. userSettings.guilds.isMember.delete(guild);
  509. return res(`/guild/${guild}`, 'savefail');
  510. }
  511. if ( response === 'noMember' || !hasPerm(response.userPermissions, 'MANAGE_GUILD') ) {
  512. userSettings.guilds.isMember.delete(guild);
  513. return res('/', 'savefail');
  514. }
  515. if ( settings.delete_settings ) return db.query( 'DELETE FROM verification WHERE guild = $1 AND configid = $2 RETURNING channel, role, editcount, postcount, usergroup, accountage, rename', [guild, type] ).then( ({rows:[row]}) => {
  516. console.log( `- Dashboard: Verification successfully removed: ${guild}#${type}` );
  517. res(`/guild/${guild}/verification`, 'save');
  518. /*
  519. if ( slashCommand?.id ) db.query( 'SELECT COUNT(1) FROM verification WHERE guild = $1', [guild] ).then( ({rows:[{count}]}) => {
  520. if ( count > 0 ) return;
  521. got.put( 'https://discord.com/api/v8/applications/' + process.env.bot + '/guilds/' + guild + '/commands/' + slashCommand.id + '/permissions', {
  522. headers:{
  523. Authorization: 'Bot ' + process.env.token
  524. },
  525. json: {
  526. permissions: []
  527. },
  528. timeout: {
  529. request: 10000
  530. }
  531. } ).then( response=> {
  532. if ( response.statusCode !== 200 || !response.body ) {
  533. console.log( '- Dashboard: ' + response.statusCode + ': Error while disabling the slash command: ' + response.body?.message );
  534. return;
  535. }
  536. console.log( '- Dashboard: Slash command successfully disabled.' );
  537. }, error => {
  538. console.log( '- Dashboard: Error while disabling the slash command: ' + error );
  539. } );
  540. }, dberror => {
  541. console.log( '- Dashboard: Error while disabling the slash command: ' + dberror );
  542. } );
  543. */
  544. if ( row ) db.query( 'SELECT lang FROM discord WHERE guild = $1 AND channel IS NULL', [guild] ).then( ({rows:[channel]}) => {
  545. var lang = new Lang(channel.lang);
  546. var text = lang.get('verification.dashboard.removed', `<@${userSettings.user.id}>`, type);
  547. if ( row ) {
  548. text += '\n' + lang.get('verification.channel') + ' <#' + row.channel.split('|').filter( channel => channel.length ).join('>, <#') + '>';
  549. let rolesRow = [
  550. row.role.split('|').filter( role => !role.startsWith( '-' ) ),
  551. row.role.split('|').filter( role => role.startsWith( '-' ) ).map( role => role.replace( '-', '' ) )
  552. ];
  553. if ( rolesRow[0].length ) text += '\n' + lang.get('verification.role_add') + ' <@&' + rolesRow[0].join('>, <@&') + '>';
  554. if ( rolesRow[1].length ) text += '\n' + lang.get('verification.role_remove') + ' <@&' + rolesRow[1].join('>, <@&') + '>';
  555. if ( row.postcount === null ) {
  556. text += '\n' + lang.get('verification.posteditcount') + ' `' + row.editcount + '`';
  557. }
  558. else {
  559. text += '\n' + lang.get('verification.editcount') + ' `' + row.editcount + '`';
  560. text += '\n' + lang.get('verification.postcount') + ' `' + Math.abs(row.postcount) + '`';
  561. if ( row.postcount < 0 ) text += ' ' + lang.get('verification.postcount_or');
  562. }
  563. 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') + ' `') ) + '`';
  564. text += '\n' + lang.get('verification.accountage') + ' `' + row.accountage + '` ' + lang.get('verification.indays');
  565. text += '\n' + lang.get('verification.rename') + ' *`' + lang.get('verification.' + ( row.rename ? 'enabled' : 'disabled')) + '`*';
  566. }
  567. text += `\n<${new URL(`/guild/${guild}/verification`, process.env.dashboard).href}>`;
  568. sendMsg( {
  569. type: 'notifyGuild', guild, text
  570. } ).catch( error => {
  571. console.log( '- Dashboard: Error while notifying the guild: ' + error );
  572. } );
  573. }, dberror => {
  574. console.log( '- Dashboard: Error while notifying the guild: ' + dberror );
  575. } );
  576. }, dberror => {
  577. console.log( '- Dashboard: Error while removing the verification: ' + dberror );
  578. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  579. } );
  580. if ( !hasPerm(response.botPermissions, 'MANAGE_ROLES') ) {
  581. return res(`/guild/${guild}/verification`, 'savefail');
  582. }
  583. if ( type === 'new' ) return db.query( 'SELECT wiki, lang, ARRAY_REMOVE(ARRAY_AGG(configid ORDER BY configid), NULL) count FROM discord LEFT JOIN verification ON discord.guild = verification.guild WHERE discord.guild = $1 AND discord.channel IS NULL GROUP BY wiki, lang', [guild] ).then( ({rows:[row]}) => {
  584. if ( !row ) return res(`/guild/${guild}/verification`, 'savefail');
  585. if ( row.count.length >= verificationLimit[( response.patreon ? 'patreon' : 'default' )] ) {
  586. return res(`/guild/${guild}/verification`, 'savefail');
  587. }
  588. return got.get( row.wiki + 'api.php?action=query&meta=allmessages&amprefix=group-&amincludelocal=true&amenableparser=true&format=json' ).then( gresponse => {
  589. var body = gresponse.body;
  590. if ( gresponse.statusCode !== 200 || body?.batchcomplete === undefined || !body?.query?.allmessages ) {
  591. console.log( '- Dashboard: ' + gresponse.statusCode + ': Error while getting the usergroups: ' + body?.error?.info );
  592. return;
  593. }
  594. var groups = body.query.allmessages.filter( group => {
  595. if ( group.name === 'group-all' ) return false;
  596. if ( group.name === 'group-membership-link-with-expiry' ) return false;
  597. if ( group.name.endsWith( '.css' ) || group.name.endsWith( '.js' ) ) return false;
  598. return true;
  599. } ).map( group => {
  600. return {
  601. name: group.name.replace( /^group-/, '' ).replace( /-member$/, '' ),
  602. content: group['*'].replace( / /g, '_' ).toLowerCase()
  603. };
  604. } );
  605. settings.usergroup = settings.usergroup.map( usergroup => {
  606. if ( groups.some( group => group.name === usergroup ) ) return usergroup;
  607. if ( groups.some( group => group.content === usergroup ) ) {
  608. return groups.find( group => group.content === usergroup ).name;
  609. }
  610. if ( /^admins?$/.test(usergroup) ) return 'sysop';
  611. if ( usergroup === '*' ) return 'user';
  612. return usergroup;
  613. } );
  614. }, error => {
  615. console.log( '- Dashboard: Error while getting the usergroups: ' + error );
  616. } ).finally( () => {
  617. if ( settings.usergroup_and ) settings.usergroup.unshift('AND');
  618. var configid = 1;
  619. for ( let i of row.count ) {
  620. if ( configid === i ) configid++;
  621. else break;
  622. }
  623. db.query( 'INSERT INTO verification(guild, configid, channel, role, editcount, postcount, usergroup, accountage, rename) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9)', [guild, configid, '|' + channels.join('|') + '|', roles.map( role => role.prefix + role.id ).join('|'), settings.editcount, settings.postcount, settings.usergroup.join('|'), settings.accountage, ( settings.rename ? 1 : 0 )] ).then( () => {
  624. console.log( `- Dashboard: Verification successfully added: ${guild}#${configid}` );
  625. res(`/guild/${guild}/verification/${configid}`, 'save');
  626. /*
  627. if ( !row.count.length && slashCommand?.id ) got.put( 'https://discord.com/api/v8/applications/' + process.env.bot + '/guilds/' + guild + '/commands/' + slashCommand.id + '/permissions', {
  628. headers:{
  629. Authorization: 'Bot ' + process.env.token
  630. },
  631. json: {
  632. permissions: [
  633. {
  634. id: guild,
  635. type: 1,
  636. permission: true
  637. }
  638. ]
  639. },
  640. timeout: {
  641. request: 10000
  642. }
  643. } ).then( response=> {
  644. if ( response.statusCode !== 200 || !response.body ) {
  645. console.log( '- Dashboard: ' + response.statusCode + ': Error while enabling the slash command: ' + response.body?.message );
  646. return;
  647. }
  648. console.log( '- Dashboard: Slash command successfully enabled.' );
  649. }, error => {
  650. console.log( '- Dashboard: Error while enabling the slash command: ' + error );
  651. } );
  652. */
  653. var lang = new Lang(row.lang);
  654. var text = lang.get('verification.dashboard.added', `<@${userSettings.user.id}>`, configid);
  655. text += '\n' + lang.get('verification.channel') + ' <#' + channels.join('>, <#') + '>';
  656. let rolesRow = [
  657. roles.filter( role => !role.prefix ).map( role => '<@&' + role.id + '>' ),
  658. roles.filter( role => role.prefix ).map( role => '<@&' + role.id + '>' )
  659. ];
  660. if ( rolesRow[0].length ) text += '\n' + lang.get('verification.role_add') + ' ' + rolesRow[0].join(', ');
  661. if ( rolesRow[1].length ) text += '\n' + lang.get('verification.role_remove') + ' ' + rolesRow[1].join(', ');
  662. if ( settings.postcount === null ) {
  663. text += '\n' + lang.get('verification.posteditcount') + ' `' + settings.editcount + '`';
  664. }
  665. else {
  666. text += '\n' + lang.get('verification.editcount') + ' `' + settings.editcount + '`';
  667. text += '\n' + lang.get('verification.postcount') + ' `' + Math.abs(settings.postcount) + '`';
  668. if ( settings.postcount < 0 ) text += ' ' + lang.get('verification.postcount_or');
  669. }
  670. 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') + ' `') ) + '`';
  671. text += '\n' + lang.get('verification.accountage') + ' `' + settings.accountage + '` ' + lang.get('verification.indays');
  672. text += '\n' + lang.get('verification.rename') + ' *`' + lang.get('verification.' + ( settings.rename ? 'enabled' : 'disabled')) + '`*';
  673. text += `\n<${new URL(`/guild/${guild}/verification/${configid}`, process.env.dashboard).href}>`;
  674. if ( settings.rename && !hasPerm(response.botPermissions, 'MANAGE_NICKNAMES') ) {
  675. text += '\n\n' + lang.get('verification.rename_no_permission', `<@${process.env.bot}>`);
  676. }
  677. if ( roles.some( role => {
  678. return !userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  679. return ( guildRole.id === role.id && guildRole.lower );
  680. } );
  681. } ) ) {
  682. text += '\n';
  683. roles.forEach( role => {
  684. if ( !userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  685. return ( guildRole.id === role.id );
  686. } ) ) {
  687. text += '\n' + lang.get('verification.role_deleted', `<@&${role.id}>`);
  688. }
  689. else if ( userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  690. return ( guildRole.id === role.id && !guildRole.lower );
  691. } ) ) {
  692. text += '\n' + lang.get('verification.role_too_high', `<@&${role.id}>`, `<@${process.env.bot}>`);
  693. }
  694. } );
  695. }
  696. sendMsg( {
  697. type: 'notifyGuild', guild, text
  698. } ).catch( error => {
  699. console.log( '- Dashboard: Error while notifying the guild: ' + error );
  700. } );
  701. }, dberror => {
  702. console.log( '- Dashboard: Error while adding the verification: ' + dberror );
  703. return res(`/guild/${guild}/verification/new`, 'savefail');
  704. } );
  705. } );
  706. }, dberror => {
  707. console.log( '- Dashboard: Error while checking for verifications: ' + dberror );
  708. return res(`/guild/${guild}/verification/new`, 'savefail');
  709. } );
  710. return db.query( 'SELECT wiki, lang, verification.channel, verification.role, editcount, postcount, usergroup, accountage, rename FROM discord LEFT JOIN verification ON discord.guild = verification.guild AND verification.configid = $1 WHERE discord.guild = $2 AND discord.channel IS NULL', [type, guild] ).then( ({rows:[row]}) => {
  711. if ( !row?.channel ) return res(`/guild/${guild}/verification`, 'savefail');
  712. row.channel = row.channel.split('|').filter( channel => channel.length );
  713. var newChannel = channels.filter( channel => !row.channel.includes( channel ) );
  714. /** @type {String[][]} */
  715. var rolesRow = [
  716. row.role.split('|').filter( role => !role.startsWith( '-' ) ),
  717. row.role.split('|').filter( role => role.startsWith( '-' ) ).map( role => role.replace( '-', '' ) )
  718. ];
  719. var newRole = roles.filter( role => {
  720. return !rolesRow[0].includes( role.id ) && !rolesRow[1].includes( role.id );
  721. } );
  722. row.usergroup = row.usergroup.split('|');
  723. var newUsergroup = settings.usergroup.filter( group => !row.usergroup.includes( group ) );
  724. if ( newChannel.length || newRole.length ) {
  725. let curGuild = userSettings.guilds.isMember.get(guild);
  726. if ( newChannel.some( channel => {
  727. return !curGuild.channels.some( guildChannel => {
  728. return ( guildChannel.id === channel && !guildChannel.isCategory );
  729. } );
  730. } ) || newRole.some( role => {
  731. return !curGuild.roles.some( guildRole => {
  732. return ( guildRole.id === role.id && guildRole.lower );
  733. } );
  734. } ) ) return res(`/guild/${guild}/verification/${type}`, 'savefail');
  735. }
  736. ( newUsergroup.length ? got.get( row.wiki + 'api.php?action=query&meta=allmessages&amprefix=group-&amincludelocal=true&amenableparser=true&format=json' ).then( gresponse => {
  737. var body = gresponse.body;
  738. if ( gresponse.statusCode !== 200 || body?.batchcomplete === undefined || !body?.query?.allmessages ) {
  739. console.log( '- Dashboard: ' + gresponse.statusCode + ': Error while getting the usergroups: ' + body?.error?.info );
  740. return;
  741. }
  742. var groups = body.query.allmessages.filter( group => {
  743. if ( group.name === 'group-all' ) return false;
  744. if ( group.name === 'group-membership-link-with-expiry' ) return false;
  745. if ( group.name.endsWith( '.css' ) || group.name.endsWith( '.js' ) ) return false;
  746. return true;
  747. } ).map( group => {
  748. return {
  749. name: group.name.replace( /^group-/, '' ).replace( /-member$/, '' ),
  750. content: group['*'].replace( / /g, '_' ).toLowerCase()
  751. };
  752. } );
  753. settings.usergroup = settings.usergroup.map( usergroup => {
  754. if ( groups.some( group => group.name === usergroup ) ) return usergroup;
  755. if ( groups.some( group => group.content === usergroup ) ) {
  756. return groups.find( group => group.content === usergroup ).name;
  757. }
  758. if ( /^admins?$/.test(usergroup) ) return 'sysop';
  759. if ( usergroup === '*' ) return 'user';
  760. return usergroup;
  761. } );
  762. }, error => {
  763. console.log( '- Dashboard: Error while getting the usergroups: ' + error );
  764. } ) : Promise.resolve() ).finally( () => {
  765. if ( settings.usergroup_and ) settings.usergroup.unshift('AND');
  766. var lang = new Lang(row.lang);
  767. var diff = [];
  768. if ( newChannel.length || row.channel.some( channel => {
  769. return !channels.includes( channel );
  770. } ) ) {
  771. diff.push(lang.get('verification.channel') + ` ~~<#${row.channel.join('>, <#')}>~~ → <#${channels.join('>, <#')}>`);
  772. }
  773. if ( roles.some( role => {
  774. if ( role.prefix ) return false;
  775. return !rolesRow[0].includes( role.id );
  776. } ) || rolesRow[0].some( roleid => {
  777. return !roles.some( role => !role.prefix && role.id === roleid );
  778. } ) ) {
  779. diff.push(lang.get('verification.role_add') + ' ~~' + ( rolesRow[0].length ? '<@&' + rolesRow[0].join('>, <@&') + '>' : '*`' + lang.get('verification.role_none') + '`*' ) + '~~ → ' + ( roles.some( role => !role.prefix ) ? roles.filter( role => !role.prefix ).map( role => '<@&' + role.id + '>' ).join(', ') : '*`' + lang.get('verification.role_none') + '`*' ));
  780. }
  781. if ( roles.some( role => {
  782. if ( !role.prefix ) return false;
  783. return !rolesRow[1].includes( role.id );
  784. } ) || rolesRow[1].some( roleid => {
  785. return !roles.some( role => role.prefix && role.id === roleid );
  786. } ) ) {
  787. diff.push(lang.get('verification.role_remove') + ' ~~' + ( rolesRow[1].length ? '<@&' + rolesRow[1].join('>, <@&') + '>' : '*`' + lang.get('verification.role_none') + '`*' ) + '~~ → ' + ( roles.some( role => role.prefix ) ? roles.filter( role => role.prefix ).map( role => '<@&' + role.id + '>' ).join(', ') : '*`' + lang.get('verification.role_none') + '`*' ));
  788. }
  789. if ( row.postcount !== settings.postcount && ( row.postcount === null || settings.postcount === null ) ) {
  790. if ( row.postcount === null ) {
  791. diff.push('~~' + lang.get('verification.posteditcount') + ` \`${row.editcount}\`~~`);
  792. diff.push('→ ' + lang.get('verification.editcount') + ` \`${settings.editcount}\``);
  793. diff.push('→ ' + lang.get('verification.postcount') + ` \`${Math.abs(settings.postcount)}\`` + ( settings.postcount < 0 ? ' ' + lang.get('verification.postcount_or') : '' ));
  794. }
  795. if ( settings.postcount === null ) {
  796. diff.push('~~' + lang.get('verification.editcount') + ` \`${row.editcount}\`~~`);
  797. diff.push('~~' + lang.get('verification.postcount') + ` \`${Math.abs(row.postcount)}\`` + ( row.postcount < 0 ? ' ' + lang.get('verification.postcount_or') : '' ) + '~~');
  798. diff.push('→ ' + lang.get('verification.posteditcount') + ` \`${settings.editcount}\``);
  799. }
  800. }
  801. else {
  802. if ( row.editcount !== settings.editcount ) {
  803. diff.push(lang.get('verification.editcount') + ` ~~\`${row.editcount}\`~~ → \`${settings.editcount}\``);
  804. }
  805. if ( row.postcount !== settings.postcount ) {
  806. if ( ( row.postcount >= 0 && settings.postcount < 0 ) || ( row.postcount < 0 && settings.postcount >= 0 ) ) {
  807. diff.push('~~' + lang.get('verification.postcount') + ` \`${Math.abs(row.postcount)}\`` + ( row.postcount < 0 ? ' ' + lang.get('verification.postcount_or') : '' ) + '~~');
  808. diff.push('→ ' + lang.get('verification.postcount') + ` \`${Math.abs(settings.postcount)}\`` + ( settings.postcount < 0 ? ' ' + lang.get('verification.postcount_or') : '' ));
  809. }
  810. else diff.push(lang.get('verification.postcount') + ` ~~\`${Math.abs(row.postcount)}\`~~ → \`${Math.abs(settings.postcount)}\`` + ( settings.postcount < 0 ? ' ' + lang.get('verification.postcount_or') : '' ));
  811. }
  812. }
  813. if ( newUsergroup.length || row.usergroup.some( usergroup => {
  814. return !settings.usergroup.includes( usergroup );
  815. } ) ) {
  816. 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') + ' `') ) + '`');
  817. }
  818. if ( row.accountage !== settings.accountage ) {
  819. diff.push(lang.get('verification.accountage') + ` ~~\`${row.accountage}\`~~ → \`${settings.accountage}\``);
  820. }
  821. if ( row.rename !== ( settings.rename ? 1 : 0 ) ) {
  822. diff.push(lang.get('verification.rename') + ` ~~*\`${lang.get('verification.' + ( row.rename ? 'enabled' : 'disabled'))}\`*~~ → *\`${lang.get('verification.' + ( settings.rename ? 'enabled' : 'disabled'))}\`*`);
  823. }
  824. if ( !diff.length ) return res(`/guild/${guild}/verification/${type}`, 'save');
  825. db.query( 'UPDATE verification SET channel = $1, role = $2, editcount = $3, postcount = $4, usergroup = $5, accountage = $6, rename = $7 WHERE guild = $8 AND configid = $9', ['|' + channels.join('|') + '|', roles.map( role => role.prefix + role.id ).join('|'), settings.editcount, settings.postcount, settings.usergroup.join('|'), settings.accountage, ( settings.rename ? 1 : 0 ), guild, type] ).then( () => {
  826. console.log( `- Dashboard: Verification successfully updated: ${guild}#${type}` );
  827. res(`/guild/${guild}/verification/${type}`, 'save');
  828. var text = lang.get('verification.dashboard.updated', `<@${userSettings.user.id}>`, type);
  829. text += '\n' + diff.join('\n');
  830. text += `\n<${new URL(`/guild/${guild}/verification/${type}`, process.env.dashboard).href}>`;
  831. if ( settings.rename && !hasPerm(response.botPermissions, 'MANAGE_NICKNAMES') ) {
  832. text += '\n\n' + lang.get('verification.rename_no_permission', `<@${process.env.bot}>`);
  833. }
  834. if ( roles.some( role => {
  835. return !userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  836. return ( guildRole.id === role.id && guildRole.lower );
  837. } );
  838. } ) ) {
  839. text += '\n';
  840. roles.forEach( role => {
  841. if ( !userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  842. return ( guildRole.id === role.id );
  843. } ) ) {
  844. text += '\n' + lang.get('verification.role_deleted', `<@&${role.id}>`);
  845. }
  846. else if ( userSettings.guilds.isMember.get(guild).roles.some( guildRole => {
  847. return ( guildRole.id === role.id && !guildRole.lower );
  848. } ) ) {
  849. text += '\n' + lang.get('verification.role_too_high', `<@&${role.id}>`, `<@${process.env.bot}>`);
  850. }
  851. } );
  852. }
  853. sendMsg( {
  854. type: 'notifyGuild', guild, text
  855. } ).catch( error => {
  856. console.log( '- Dashboard: Error while notifying the guild: ' + error );
  857. } );
  858. }, dberror => {
  859. console.log( '- Dashboard: Error while updating the verification: ' + dberror );
  860. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  861. } );
  862. } );
  863. }, dberror => {
  864. console.log( '- Dashboard: Error while checking for verifications: ' + dberror );
  865. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  866. } );
  867. }, error => {
  868. console.log( '- Dashboard: Error while getting the member: ' + error );
  869. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  870. } );
  871. }
  872. /**
  873. * Change verification notices
  874. * @param {Function} res - The server response
  875. * @param {import('./util.js').Settings} userSettings - The settings of the user
  876. * @param {String} guild - The id of the guild
  877. * @param {String} type - The setting to change
  878. * @param {Object} settings - The new settings
  879. * @param {String} [settings.channel]
  880. * @param {String} [settings.flag_logall]
  881. * @param {String} [settings.flag_private]
  882. * @param {String} [settings.success]
  883. * @param {String} [settings.match]
  884. * @param {String} settings.save_settings
  885. */
  886. function update_notices(res, userSettings, guild, type, settings) {
  887. if ( !settings.save_settings ) {
  888. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  889. }
  890. if ( settings.channel && !/^\d+$/.test(settings.channel) ) {
  891. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  892. }
  893. if ( settings.success && settings.success.trim().length > 1000 ) {
  894. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  895. }
  896. if ( settings.match && settings.match.trim().length > 1000 ) {
  897. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  898. }
  899. settings.channel = ( settings.channel || null );
  900. settings.success = ( settings.success?.trim().replace( /`ˋ`/g, '```' ) || null );
  901. settings.match = ( settings.match?.trim().replace( /`ˋ`/g, '```' ) || null );
  902. sendMsg( {
  903. type: 'getMember',
  904. member: userSettings.user.id,
  905. guild: guild,
  906. newchannel: settings.channel
  907. } ).then( response => {
  908. if ( !response ) {
  909. userSettings.guilds.notMember.set(guild, userSettings.guilds.isMember.get(guild));
  910. userSettings.guilds.isMember.delete(guild);
  911. return res(`/guild/${guild}`, 'savefail');
  912. }
  913. if ( response === 'noMember' || !hasPerm(response.userPermissions, 'MANAGE_GUILD') ) {
  914. userSettings.guilds.isMember.delete(guild);
  915. return res('/', 'savefail');
  916. }
  917. if ( settings.channel && ( response.message === 'noChannel' || !( hasPerm(response.botPermissionsNew, 'VIEW_CHANNEL', 'SEND_MESSAGES') && hasPerm(response.userPermissions, 'VIEW_CHANNEL') ) ) ) {
  918. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  919. }
  920. return db.connect().then( client => {
  921. return client.query( 'SELECT logchannel, flags, onsuccess, onmatch FROM verifynotice WHERE guild = $1', [guild] ).then( ({rows:[row]}) => {
  922. var flags = ( settings.flag_private ? 1 << 0 : 0 ) + ( settings.flag_logall ? 1 << 1 : 0 );
  923. if ( !row ) {
  924. if ( !( settings.channel || flags || settings.success || settings.match ) ) {
  925. return res(`/guild/${guild}/verification/${type}`, 'save');
  926. }
  927. return client.query( 'INSERT INTO verifynotice(guild, logchannel, flags, onsuccess, onmatch) VALUES($1, $2, $3, $4, $5)', [guild, settings.channel, flags, settings.success, settings.match] ).then( () => {
  928. console.log( `- Dashboard: Verification notices successfully added: ${guild}` );
  929. res(`/guild/${guild}/verification/${type}`, 'save');
  930. return client.query( 'SELECT lang FROM discord WHERE guild = $1 AND channel IS NULL', [guild] ).then( ({rows:[channel]}) => {
  931. var lang = new Lang(channel?.lang);
  932. var text = lang.get('verification.dashboard.added_notice', `<@${userSettings.user.id}>`) + '\n';
  933. if ( settings.channel ) text += `${lang.get('verification.logging')} <#${settings.channel}>\n`;
  934. if ( settings.flag_logall ) text += `${lang.get('verification.flag_logall')} *\`${lang.get('verification.enabled')}\`*\n`;
  935. if ( settings.flag_private ) text += `${lang.get('verification.flag_private')} *\`${lang.get('verification.enabled')}\`*\n`;
  936. if ( settings.success ) text += `${lang.get('verification.success')} \`\`\`md\n${settings.success.replace( /```/g, '`ˋ`' )}\n\`\`\``;
  937. if ( settings.match ) text += `${lang.get('verification.match')} \`\`\`md\n${settings.match.replace( /```/g, '`ˋ`' )}\n\`\`\``;
  938. text += `<${new URL(`/guild/${guild}/verification/${type}`, process.env.dashboard).href}>`;
  939. if ( settings.success?.includes( '](' ) || settings.match?.includes( '](' ) ) {
  940. text += '\n\n' + lang.get('verification.notice_embed');
  941. }
  942. sendMsg( {
  943. type: 'notifyGuild', guild, text
  944. } ).catch( error => {
  945. console.log( '- Dashboard: Error while notifying the guild: ' + error );
  946. } );
  947. }, dberror => {
  948. console.log( '- Dashboard: Error while notifying the guild: ' + dberror );
  949. } );
  950. }, dberror => {
  951. console.log( '- Dashboard: Error while adding the verification notices: ' + dberror );
  952. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  953. } );
  954. }
  955. if ( settings.channel === row.logchannel && flags === row.flags && settings.success === row.onsuccess && settings.match === row.onmatch ) {
  956. return res(`/guild/${guild}/verification/${type}`, 'save');
  957. }
  958. return client.query( 'UPDATE verifynotice SET logchannel = $1, flags = $2, onsuccess = $3, onmatch = $4 WHERE guild = $5', [settings.channel, flags, settings.success, settings.match, guild] ).then( () => {
  959. console.log( `- Dashboard: Verification notices successfully updated: ${guild}` );
  960. res(`/guild/${guild}/verification/${type}`, 'save');
  961. return client.query( 'SELECT lang FROM discord WHERE guild = $1 AND channel IS NULL', [guild] ).then( ({rows:[channel]}) => {
  962. var lang = new Lang(channel?.lang);
  963. var text = lang.get('verification.dashboard.updated_notice', `<@${userSettings.user.id}>`) + '\n';
  964. if ( settings.channel !== row.logchannel ) {
  965. text += lang.get('verification.logging') + ' ~~' + ( row.logchannel ? `<#${row.logchannel}>` : `*\`${lang.get('verification.disabled')}\`*` ) + '~~ → ' + ( settings.channel ? `<#${settings.channel}>` : `*\`${lang.get('verification.disabled')}\`*` ) + '\n';
  966. }
  967. if ( ( (flags & 1 << 1) === 1 << 1 ) !== ( (row.flags & 1 << 1) === 1 << 1 ) ) {
  968. text += lang.get('verification.flag_logall') + ' ~~*`' + lang.get('verification.' + ( (row.flags & 1 << 1) === 1 << 1 ? 'enabled' : 'disabled')) + '`*~~ → *`' + lang.get('verification.' + ( settings.flag_logall ? 'enabled' : 'disabled')) + '`*\n';
  969. }
  970. if ( ( (flags & 1 << 0) === 1 << 0 ) !== ( (row.flags & 1 << 0) === 1 << 0 ) ) {
  971. text += lang.get('verification.flag_private') + ' ~~*`' + lang.get('verification.' + ( (row.flags & 1 << 0) === 1 << 0 ? 'enabled' : 'disabled')) + '`*~~ → *`' + lang.get('verification.' + ( settings.flag_private ? 'enabled' : 'disabled')) + '`*\n';
  972. }
  973. if ( settings.success !== row.onsuccess ) {
  974. text += lang.get('verification.success') + ' ' + ( row.onsuccess ? '~~```md\n' + row.onsuccess.replace( /\\/g, '\\$&' ).replace( /```/g, '`ˋ`' ) + '\n```~~' : `~~*\`${lang.get('verification.disabled')}\`*~~ → ` ) + ( settings.success ? '```md\n' + settings.success.replace( /\\/g, '\\$&' ).replace( /```/g, '`ˋ`' ) + '\n```' : ` → *\`${lang.get('verification.disabled')}\`*\n` );
  975. }
  976. if ( settings.match !== row.onmatch ) {
  977. text += lang.get('verification.match') + ' ' + ( row.onmatch ? '~~```md\n' + row.onmatch.replace( /\\/g, '\\$&' ).replace( /```/g, '`ˋ`' ) + '\n```~~' : `~~*\`${lang.get('verification.disabled')}\`*~~ → ` ) + ( settings.match ? '```md\n' + settings.match.replace( /\\/g, '\\$&' ).replace( /```/g, '`ˋ`' ) + '\n```' : ` → *\`${lang.get('verification.disabled')}\`*\n` );
  978. }
  979. text += `<${new URL(`/guild/${guild}/verification/${type}`, process.env.dashboard).href}>`;
  980. if ( settings.success?.includes( '](' ) || settings.match?.includes( '](' ) ) {
  981. text += '\n\n' + lang.get('verification.notice_embed');
  982. }
  983. sendMsg( {
  984. type: 'notifyGuild', guild, text
  985. } ).catch( error => {
  986. console.log( '- Dashboard: Error while notifying the guild: ' + error );
  987. } );
  988. }, dberror => {
  989. console.log( '- Dashboard: Error while notifying the guild: ' + dberror );
  990. } );
  991. }, dberror => {
  992. console.log( '- Dashboard: Error while updating the verification notices: ' + dberror );
  993. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  994. } );
  995. }, dberror => {
  996. console.log( '- Dashboard: Error while getting the current verification notices: ' + dberror );
  997. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  998. } ).finally( () => {
  999. client.release();
  1000. } );
  1001. }, dberror => {
  1002. console.log( '- Error while connecting to the database client: ' + dberror );
  1003. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  1004. } );
  1005. }, error => {
  1006. console.log( '- Dashboard: Error while getting the member: ' + error );
  1007. return res(`/guild/${guild}/verification/${type}`, 'savefail');
  1008. } );
  1009. }
  1010. export {
  1011. dashboard_verification as get,
  1012. update_verification as post
  1013. };