settings.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. const {defaultSettings} = require('../util/default.json');
  2. const {allLangs: {names: allLangs}} = require('../i18n/allLangs.json');
  3. const {db, settingsData, sendMsg, createNotice, 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. wiki: '<label for="wb-settings-wiki">Default Wiki:</label>'
  8. + '<input type="url" id="wb-settings-wiki" name="wiki" required>',
  9. //+ '<button type="button" id="wb-settings-wiki-search" class="collapsible">Search wiki</button>'
  10. //+ '<fieldset style="display: none;">'
  11. //+ '<legend>Wiki search</legend>'
  12. //+ '</fieldset>',
  13. lang: '<label for="wb-settings-lang">Language:</label>'
  14. + '<select id="wb-settings-lang" name="lang" required>'
  15. + Object.keys(allLangs).map( lang => {
  16. return `<option id="wb-settings-lang-${lang}" value="${lang}">${allLangs[lang]}</option>`
  17. } ).join('\n')
  18. + '</select>',
  19. prefix: '<label for="wb-settings-prefix">Prefix:</label>'
  20. + '<input type="text" id="wb-settings-prefix" name="prefix" pattern="^[^ \`]+$" required>'
  21. + '<br>'
  22. + '<label for="wb-settings-prefix-space">Prefix ends with space:</label>'
  23. + '<input type="checkbox" id="wb-settings-prefix-space" name="prefix-space">',
  24. inline: '<label for="wb-settings-inline">Inline commands:</label>'
  25. + '<input type="checkbox" id="wb-settings-inline" name="inline">',
  26. voice: '<label for="wb-settings-voice">Voice channels:</label>'
  27. + '<input type="checkbox" id="wb-settings-voice" name="voice">'
  28. };
  29. /**
  30. * Let a user change settings
  31. * @param {CheerioStatic} $ - The response body
  32. */
  33. function createForm($, header, settings, guildChannels) {
  34. var readonly = ( process.env.READONLY ? true : false );
  35. var fields = [];
  36. if ( settings.channel ) {
  37. let channel = $('<div>').append(fieldset.channel);
  38. channel.find('#wb-settings-channel').append(
  39. ...guildChannels.map( guildChannel => {
  40. return $(`<option id="wb-settings-channel-${guildChannel.id}">`).val(guildChannel.id).text(`${guildChannel.id} – #${guildChannel.name}`)
  41. } )
  42. );
  43. if ( guildChannels.length === 1 ) {
  44. channel.find(`#wb-settings-channel-${settings.channel}`).attr('selected', '');
  45. if ( !hasPerm(guildChannels[0].permissions, 'VIEW_CHANNEL', 'SEND_MESSAGES') ) {
  46. readonly = true;
  47. }
  48. }
  49. else channel.find('#wb-settings-channel').prepend(
  50. $(`<option id="wb-settings-channel-default" selected>`).val('').text('-- Select a Channel --')
  51. );
  52. fields.push(channel);
  53. }
  54. let wiki = $('<div>').append(fieldset.wiki);
  55. wiki.find('#wb-settings-wiki').val(settings.wiki);
  56. fields.push(wiki);
  57. if ( !settings.channel || settings.patreon ) {
  58. let lang = $('<div>').append(fieldset.lang);
  59. lang.find(`#wb-settings-lang-${settings.lang}`).attr('selected', '');
  60. fields.push(lang);
  61. let inline = $('<div>').append(fieldset.inline);
  62. if ( !settings.inline ) inline.find('#wb-settings-inline').attr('checked', '');
  63. fields.push(inline);
  64. }
  65. if ( settings.patreon && !settings.channel ) {
  66. let prefix = $('<div>').append(fieldset.prefix);
  67. prefix.find('#wb-settings-prefix').val(settings.prefix.trim());
  68. if ( settings.prefix.endsWith( ' ' ) ) {
  69. prefix.find('#wb-settings-prefix-space').attr('checked', '');
  70. }
  71. fields.push(prefix);
  72. }
  73. if ( !settings.channel ) {
  74. let voice = $('<div>').append(fieldset.voice);
  75. if ( settings.voice ) voice.find('#wb-settings-voice').attr('checked', '');
  76. fields.push(voice);
  77. }
  78. var form = $('<fieldset>').append(...fields, '<input type="submit">');
  79. if ( readonly ) {
  80. form.find('input').attr('readonly', '');
  81. form.find('input[type="submit"], input[type="checkbox"], option').attr('disabled', '');
  82. }
  83. return $('<form id="wb-settings" method="post" enctype="application/x-www-form-urlencoded">').append(
  84. $('<h2>').text(header),
  85. form
  86. );
  87. }
  88. /**
  89. * Let a user change settings
  90. * @param {import('http').ServerResponse} res - The server response
  91. * @param {CheerioStatic} $ - The response body
  92. * @param {import('./util.js').Guild} guild - The current guild
  93. * @param {String[]} args - The url parts
  94. */
  95. function dashboard_settings(res, $, guild, args) {
  96. db.all( 'SELECT channel, lang, wiki, prefix, inline, voice, patreon FROM discord WHERE guild = ? ORDER BY channel ASC', [guild.id], function(dberror, rows) {
  97. if ( dberror ) {
  98. console.log( '- Dashboard: Error while getting the settings: ' + dberror );
  99. $('#text .description').text('Failed to load the settings!');
  100. $('.channel#settings').addClass('selected');
  101. let body = $.html();
  102. res.writeHead(200, {'Content-Length': body.length});
  103. res.write( body );
  104. return res.end();
  105. }
  106. $('#text .description').text(`These are the settings for "${guild.name}":`);
  107. if ( !rows.length ) {
  108. $('.channel#settings').addClass('selected');
  109. createForm($, 'Server-wide Settings', Object.assign({
  110. prefix: process.env.prefix
  111. }, defaultSettings)).attr('action', `/guild/${guild.id}`).appendTo('#text');
  112. let body = $.html();
  113. res.writeHead(200, {'Content-Length': body.length});
  114. res.write( body );
  115. return res.end();
  116. }
  117. let isPatreon = rows.some( row => row.patreon );
  118. let channellist = rows.filter( row => row.channel ).map( row => {
  119. let channel = guild.channels.find( channel => channel.id === row.channel );
  120. return ( channel || {id: row.channel, name: 'UNKNOWN', permissions: 0} );
  121. } ).sort( (a, b) => {
  122. return guild.channels.indexOf(a) - guild.channels.indexOf(b);
  123. } );
  124. $('#channellist #settings').after(
  125. ...channellist.map( channel => {
  126. return $('<a class="channel">').attr('href', `/guild/${guild.id}/${channel.id}`).append(
  127. $('<img>').attr('src', '/src/channel.svg'),
  128. $('<div>').text(channel.name)
  129. ).attr('id', `channel-${channel.id}`).attr('title', channel.id);
  130. } ),
  131. ( process.env.READONLY ? '' :
  132. $('<a class="channel" id="channel-new">').attr('href', `/guild/${guild.id}/new`).append(
  133. $('<img>').attr('src', '/src/channel.svg'),
  134. $('<div>').text('New channel overwrite')
  135. ) )
  136. );
  137. if ( args[3] === 'new' ) {
  138. $('.channel#channel-new').addClass('selected');
  139. createForm($, 'New channel overwrite', Object.assign({}, rows.find( row => !row.channel ), {
  140. patreon: isPatreon,
  141. channel: 'new'
  142. }), guild.channels.filter( channel => {
  143. return hasPerm(channel.permissions, 'VIEW_CHANNEL', 'SEND_MESSAGES');
  144. } )).attr('action', `/guild/${guild.id}`).appendTo('#text');
  145. let body = $.html();
  146. res.writeHead(200, {'Content-Length': body.length});
  147. res.write( body );
  148. return res.end();
  149. }
  150. if ( channellist.some( channel => channel.id === args[3] ) ) {
  151. let channel = channellist.find( channel => channel.id === args[3] );
  152. $(`.channel#channel-${channel.id}`).addClass('selected');
  153. createForm($, `#${channel.name} Settings`, Object.assign({}, rows.find( row => {
  154. return row.channel === channel.id;
  155. } ), {
  156. patreon: isPatreon
  157. }), [channel]).attr('action', `/guild/${guild.id}`).appendTo('#text');
  158. let body = $.html();
  159. res.writeHead(200, {'Content-Length': body.length});
  160. res.write( body );
  161. return res.end();
  162. }
  163. $('.channel#settings').addClass('selected');
  164. createForm($, 'Server-wide Settings', rows.find( row => !row.channel )).attr('action', `/guild/${guild.id}`).appendTo('#text');
  165. let body = $.html();
  166. res.writeHead(200, {'Content-Length': body.length});
  167. res.write( body );
  168. return res.end();
  169. } );
  170. }
  171. function update_settings(user, guild, settings) {
  172. }
  173. module.exports = {
  174. get: dashboard_settings,
  175. post: update_settings
  176. };