settings.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. const {defaultSettings} = require('../util/default.json');
  2. const {allLangs} = require('../i18n/allLangs.json');
  3. const {got, db, sendMsg, hasPerm} = require('./util.js');
  4. const dashboard = require('./guilds.js');
  5. const fieldset = {
  6. channel: '<label for="wb-settings-channel">Channel:</label>'
  7. + '<select id="wb-settings-channel" name="channel" required></select>',
  8. wiki: '<label for="wb-settings-wiki">Default Wiki:</label>'
  9. + '<input type="url" id="wb-settings-wiki" name="wiki" required>',
  10. //+ '<button type="button" id="wb-settings-wiki-search" class="collapsible">Search wiki</button>'
  11. //+ '<fieldset style="display: none;">'
  12. //+ '<legend>Wiki search</legend>'
  13. //+ '</fieldset>',
  14. lang: '<label for="wb-settings-lang">Language:</label>'
  15. + '<select id="wb-settings-lang" name="lang" required>'
  16. + Object.keys(allLangs.names).map( lang => {
  17. return `<option id="wb-settings-lang-${lang}" value="${lang}">${allLangs.names[lang]}</option>`
  18. } ).join('\n')
  19. + '</select>',
  20. prefix: '<label for="wb-settings-prefix">Prefix:</label>'
  21. + '<input type="text" id="wb-settings-prefix" name="prefix" pattern="^\\s*[^\\s`]+\\s*$" required>'
  22. + '<br>'
  23. + '<label for="wb-settings-prefix-space">Prefix ends with space:</label>'
  24. + '<input type="checkbox" id="wb-settings-prefix-space" name="prefix_space">',
  25. inline: '<label for="wb-settings-inline">Inline commands:</label>'
  26. + '<input type="checkbox" id="wb-settings-inline" name="inline">',
  27. voice: '<label for="wb-settings-voice">Voice channels:</label>'
  28. + '<input type="checkbox" id="wb-settings-voice" name="voice">',
  29. save: '<input type="submit" id="wb-settings-save" name="save_settings">',
  30. delete: '<input type="submit" id="wb-settings-delete" name="delete_settings">'
  31. };
  32. /**
  33. * Create a settings form
  34. * @param {import('cheerio')} $ - The response body
  35. * @param {String} header - The form header
  36. * @param {Object} settings - The current settings
  37. * @param {Boolean} settings.patreon
  38. * @param {String} settings.channel
  39. * @param {String} settings.wiki
  40. * @param {String} settings.lang
  41. * @param {Boolean} settings.inline
  42. * @param {String} settings.prefix
  43. * @param {Boolean} settings.voice
  44. * @param {Object[]} guildChannels - The guild channels
  45. * @param {String} guildChannels.id
  46. * @param {String} guildChannels.name
  47. * @param {Number} guildChannels.permissions
  48. */
  49. function createForm($, header, settings, guildChannels) {
  50. var readonly = ( process.env.READONLY ? true : false );
  51. var fields = [];
  52. if ( settings.channel ) {
  53. let channel = $('<div>').append(fieldset.channel);
  54. channel.find('#wb-settings-channel').append(
  55. ...guildChannels.map( guildChannel => {
  56. return $(`<option id="wb-settings-channel-${guildChannel.id}">`).val(guildChannel.id).text(`${guildChannel.id} – #${guildChannel.name}`)
  57. } )
  58. );
  59. if ( guildChannels.length === 1 ) {
  60. channel.find(`#wb-settings-channel-${settings.channel}`).attr('selected', '');
  61. if ( !hasPerm(guildChannels[0].permissions, 'VIEW_CHANNEL', 'SEND_MESSAGES') ) {
  62. readonly = true;
  63. }
  64. }
  65. else channel.find('#wb-settings-channel').prepend(
  66. $(`<option id="wb-settings-channel-default" selected>`).val('').text('-- Select a Channel --')
  67. );
  68. fields.push(channel);
  69. }
  70. let wiki = $('<div>').append(fieldset.wiki);
  71. wiki.find('#wb-settings-wiki').val(settings.wiki);
  72. fields.push(wiki);
  73. if ( !settings.channel || settings.patreon ) {
  74. let lang = $('<div>').append(fieldset.lang);
  75. lang.find(`#wb-settings-lang-${settings.lang}`).attr('selected', '');
  76. fields.push(lang);
  77. let inline = $('<div>').append(fieldset.inline);
  78. if ( !settings.inline ) inline.find('#wb-settings-inline').attr('checked', '');
  79. fields.push(inline);
  80. }
  81. if ( settings.patreon && !settings.channel ) {
  82. let prefix = $('<div>').append(fieldset.prefix);
  83. prefix.find('#wb-settings-prefix').val(settings.prefix.trim());
  84. if ( settings.prefix.endsWith( ' ' ) ) {
  85. prefix.find('#wb-settings-prefix-space').attr('checked', '');
  86. }
  87. fields.push(prefix);
  88. }
  89. if ( !settings.channel ) {
  90. let voice = $('<div>').append(fieldset.voice);
  91. if ( settings.voice ) voice.find('#wb-settings-voice').attr('checked', '');
  92. fields.push(voice);
  93. }
  94. fields.push($(fieldset.save).val('Save'));
  95. if ( settings.channel && settings.channel !== 'new' ) {
  96. fields.push($(fieldset.delete).val('Delete').attr('onclick', `return confirm('Are you sure?');`));
  97. }
  98. var form = $('<fieldset>').append(...fields);
  99. if ( readonly ) {
  100. form.find('input').attr('readonly', '');
  101. form.find('input[type="checkbox"], option').attr('disabled', '');
  102. form.find('input[type="submit"]').remove();
  103. }
  104. return $('<form id="wb-settings" method="post" enctype="application/x-www-form-urlencoded">').append(
  105. $('<h2>').text(header),
  106. form
  107. );
  108. }
  109. /**
  110. * Let a user change settings
  111. * @param {import('http').ServerResponse} res - The server response
  112. * @param {import('cheerio')} $ - The response body
  113. * @param {import('./util.js').Guild} guild - The current guild
  114. * @param {String[]} args - The url parts
  115. */
  116. function dashboard_settings(res, $, guild, args) {
  117. db.all( 'SELECT channel, lang, wiki, prefix, inline, voice, patreon FROM discord WHERE guild = ? ORDER BY channel ASC', [guild.id], function(dberror, rows) {
  118. if ( dberror ) {
  119. console.log( '- Dashboard: Error while getting the settings: ' + dberror );
  120. $('#text .description').text('Failed to load the settings!');
  121. $('.channel#settings').addClass('selected');
  122. let body = $.html();
  123. res.writeHead(200, {'Content-Length': body.length});
  124. res.write( body );
  125. return res.end();
  126. }
  127. $('#text .description').text(`These are the settings for "${guild.name}":`);
  128. if ( !rows.length ) {
  129. $('.channel#settings').addClass('selected');
  130. createForm($, 'Server-wide Settings', Object.assign({
  131. prefix: process.env.prefix
  132. }, defaultSettings)).attr('action', `/guild/${guild.id}/settings/default`).appendTo('#text');
  133. let body = $.html();
  134. res.writeHead(200, {'Content-Length': body.length});
  135. res.write( body );
  136. return res.end();
  137. }
  138. let isPatreon = rows.some( row => row.patreon );
  139. let channellist = rows.filter( row => row.channel ).map( row => {
  140. let channel = guild.channels.find( channel => channel.id === row.channel );
  141. return ( channel || {id: row.channel, name: 'UNKNOWN', permissions: 0} );
  142. } ).sort( (a, b) => {
  143. return guild.channels.indexOf(a) - guild.channels.indexOf(b);
  144. } );
  145. $('#channellist #settings').after(
  146. ...channellist.map( channel => {
  147. return $('<a class="channel">').attr('id', `channel-${channel.id}`).append(
  148. $('<img>').attr('src', '/src/channel.svg'),
  149. $('<div>').text(channel.name)
  150. ).attr('href', `/guild/${guild.id}/settings/${channel.id}`).attr('title', channel.id);
  151. } ),
  152. ( process.env.READONLY || !guild.channels.filter( channel => {
  153. return ( hasPerm(channel.permissions, 'VIEW_CHANNEL', 'SEND_MESSAGES') && !rows.some( row => row.channel === channel.id ) );
  154. } ).length ? '' :
  155. $('<a class="channel" id="channel-new">').append(
  156. $('<img>').attr('src', '/src/channel.svg'),
  157. $('<div>').text('New channel overwrite')
  158. ).attr('href', `/guild/${guild.id}/settings/new`) )
  159. );
  160. if ( args[4] === 'new' ) {
  161. $('.channel#channel-new').addClass('selected');
  162. createForm($, 'New Channel Overwrite', Object.assign({}, rows.find( row => !row.channel ), {
  163. patreon: isPatreon,
  164. channel: 'new'
  165. }), guild.channels.filter( channel => {
  166. return ( hasPerm(channel.permissions, 'VIEW_CHANNEL', 'SEND_MESSAGES') && !rows.some( row => row.channel === channel.id ) );
  167. } )).attr('action', `/guild/${guild.id}/settings/new`).appendTo('#text');
  168. }
  169. else if ( channellist.some( channel => channel.id === args[4] ) ) {
  170. let channel = channellist.find( channel => channel.id === args[4] );
  171. $(`.channel#channel-${channel.id}`).addClass('selected');
  172. createForm($, `#${channel.name} Settings`, Object.assign({}, rows.find( row => {
  173. return row.channel === channel.id;
  174. } ), {
  175. patreon: isPatreon
  176. }), [channel]).attr('action', `/guild/${guild.id}/settings/${channel.id}`).appendTo('#text');
  177. }
  178. else {
  179. $('.channel#settings').addClass('selected');
  180. createForm($, 'Server-wide Settings', rows.find( row => !row.channel )).attr('action', `/guild/${guild.id}/settings/default`).appendTo('#text');
  181. }
  182. let body = $.html();
  183. res.writeHead(200, {'Content-Length': body.length});
  184. res.write( body );
  185. return res.end();
  186. } );
  187. }
  188. /**
  189. * Change settings
  190. * @param {import('http').ServerResponse} res - The server response
  191. * @param {import('./util.js').Settings} userSettings - The settings of the user
  192. * @param {String} guild - The id of the guild
  193. * @param {String} type - The setting to change
  194. * @param {Object} settings - The new settings
  195. * @param {String} [settings.channel]
  196. * @param {String} settings.wiki
  197. * @param {String} [settings.lang]
  198. * @param {String} [settings.inline]
  199. * @param {String} [settings.prefix]
  200. * @param {String} [settings.prefix_space]
  201. * @param {String} [settings.voice]
  202. * @param {String} [settings.save_settings]
  203. * @param {String} [settings.delete_settings]
  204. */
  205. function update_settings(res, userSettings, guild, type, settings) {
  206. console.log( settings );
  207. sendMsg( {
  208. type: 'getMember',
  209. member: userSettings.user.id,
  210. guild: guild
  211. } ).then( response => {
  212. if ( !response ) {
  213. userSettings.guilds.notMember.set(guild, userSettings.guilds.isMember.get(guild));
  214. userSettings.guilds.isMember.delete(guild);
  215. return dashboard(res, userSettings.state,
  216. new URL(`/guild/${guild}?save=failed`, process.env.dashboard));
  217. }
  218. if ( response === 'noMember' || !hasPerm(response.permissions, 'MANAGE_SERVER') ) {
  219. userSettings.guilds.isMember.delete(guild);
  220. return dashboard(res, userSettings.state,
  221. new URL('/?save=failed', process.env.dashboard));
  222. }
  223. if ( type === 'default' ) {
  224. if ( !settings.save_settings || settings.channel
  225. || ( !response.patreon && settings.prefix ) ) {
  226. return dashboard(res, userSettings.state,
  227. new URL(`/guild/${guild}/settings?save=failed`, process.env.dashboard));
  228. }
  229. return dashboard(res, userSettings.state,
  230. new URL(`/guild/${guild}/settings?save=success`, process.env.dashboard));
  231. }
  232. if ( ( !settings.save_settings && !settings.delete_settings )
  233. || !settings.channel || settings.voice || ( !response.patreon
  234. && ( settings.prefix || settings.lang || settings.inline ) ) ) {
  235. return dashboard(res, userSettings.state,
  236. new URL(`/guild/${guild}/settings/${type}?save=failed`, process.env.dashboard));
  237. }
  238. if ( type === 'new' ) {
  239. if ( !settings.save_settings ) {
  240. return dashboard(res, userSettings.state,
  241. new URL(`/guild/${guild}/settings/new?save=failed`, process.env.dashboard));
  242. }
  243. return dashboard(res, userSettings.state,
  244. new URL(`/guild/${guild}/settings/new?save=success`, process.env.dashboard));
  245. }
  246. if ( !settings.save_settings && settings.delete_settings ) {
  247. return dashboard(res, userSettings.state,
  248. new URL(`/guild/${guild}/settings?save=success`, process.env.dashboard));
  249. }
  250. return dashboard(res, userSettings.state,
  251. new URL(`/guild/${guild}/settings/${type}?save=success`, process.env.dashboard));
  252. }, error => {
  253. console.log( '- Dashboard: Error while getting the member: ' + error );
  254. return dashboard(res, userSettings.state,
  255. new URL(`/guild/${guild}/settings/${type}?save=failed`, process.env.dashboard));
  256. } );
  257. }
  258. module.exports = {
  259. get: dashboard_settings,
  260. post: update_settings
  261. };