settings.js 11 KB

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