settings.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. const cheerio = require('cheerio');
  2. const {MessageEmbed} = require('discord.js');
  3. const {defaultSettings} = require('../util/default.json');
  4. const Lang = require('../util/i18n.js');
  5. const allLangs = Lang.allLangs();
  6. const Wiki = require('../util/wiki.js');
  7. var db = require('../util/database.js');
  8. var allSites = [];
  9. const getAllSites = require('../util/allSites.js');
  10. getAllSites.then( sites => allSites = sites );
  11. /**
  12. * Processes the "settings" command.
  13. * @param {Lang} lang - The user language.
  14. * @param {import('discord.js').Message} msg - The Discord message.
  15. * @param {String[]} args - The command arguments.
  16. * @param {String} line - The command as plain text.
  17. * @param {Wiki} wiki - The wiki for the message.
  18. */
  19. function cmd_settings(lang, msg, args, line, wiki) {
  20. if ( !allSites.length ) getAllSites.update();
  21. if ( !msg.isAdmin() ) return msg.reactEmoji('❌');
  22. db.all( 'SELECT channel, wiki, lang, role, inline, prefix FROM discord WHERE guild = ? ORDER BY channel DESC', [msg.guild.id], (error, rows) => {
  23. if ( error ) {
  24. console.log( '- Error while getting the settings: ' + error );
  25. msg.reactEmoji('error', true);
  26. return error;
  27. }
  28. var guild = rows.find( row => !row.channel );
  29. if ( !guild ) guild = Object.assign({
  30. role: null, inline: null,
  31. prefix: process.env.prefix
  32. }, defaultSettings);
  33. var prefix = guild.prefix;
  34. var inlinepage = ( lang.localNames.page || 'page' );
  35. var text = lang.get('settings.missing', '`' + prefix + 'settings lang`', '`' + prefix + 'settings wiki`');
  36. if ( rows.length ) {
  37. text = lang.get('settings.current');
  38. text += `\n<${new URL(`/guild/${msg.guild.id}/settings`, process.env.dashboard).href}>`;
  39. text += '\n' + lang.get('settings.currentlang') + ' `' + allLangs.names[guild.lang] + '` - `' + prefix + 'settings lang`';
  40. if ( msg.guild.id in patreons ) text += '\n' + lang.get('settings.currentprefix') + ' `' + prefix + '` - `' + prefix + 'settings prefix`';
  41. text += '\n' + lang.get('settings.currentrole') + ' ' + ( guild.role ? `<@&${guild.role}>` : '@everyone' ) + ' - `' + prefix + 'settings role`';
  42. text += '\n' + lang.get('settings.currentinline') + ' ' + ( guild.inline ? '~~' : '' ) + '`[[' + inlinepage + ']]`' + ( guild.inline ? '~~' : '' ) + ' - `' + prefix + 'settings inline`';
  43. text += '\n' + lang.get('settings.currentwiki') + ' ' + guild.wiki + ' - `' + prefix + 'settings wiki`';
  44. text += '\n' + lang.get('settings.currentchannel') + ' `' + prefix + 'settings channel`\n';
  45. if ( rows.length === 1 ) text += lang.get('settings.nochannels');
  46. else text += rows.filter( row => row !== guild ).map( row => '<#' + row.channel.replace( /^#/, '' ) + '>: ' + ( msg.guild.id in patreons ? '`' + allLangs.names[row.lang] + '` - ' : '' ) + '<' + row.wiki + '>' + ( msg.guild.id in patreons ? ' - ' + ( row.role ? `<@&${row.role}>` : '@everyone' ) + ' - ' + ( row.inline ? '~~' : '' ) + '`[[' + inlinepage + ']]`' + ( row.inline ? '~~' : '' ) : '' ) ).join('\n');
  47. }
  48. if ( !args.length ) {
  49. return msg.replyMsg( text, {split:true}, true );
  50. }
  51. var prelang = '';
  52. args[0] = args[0].toLowerCase();
  53. if ( args[0] === 'channel' ) {
  54. prelang = 'channel ';
  55. if ( !rows.length ) return msg.replyMsg( text, {split:true}, true );
  56. var channel = rows.find( row => row.channel === msg.channel.id );
  57. if ( !channel ) channel = Object.assign({channel:msg.channel.id}, guild);
  58. text = lang.get('settings.' + prelang + 'current');
  59. text += `\n<${new URL(`/guild/${msg.guild.id}/settings/${msg.channel.id}`, process.env.dashboard).href}>`;
  60. if ( msg.guild.id in patreons ) {
  61. text += '\n' + lang.get('settings.currentlang') + ' `' + allLangs.names[channel.lang] + '` - `' + prefix + 'settings channel lang`';
  62. text += '\n' + lang.get('settings.currentrole') + ' ' + ( channel.role ? `<@&${channel.role}>` : '@everyone' ) + ' - `' + prefix + 'settings channel role`';
  63. text += '\n' + lang.get('settings.currentinline') + ' ' + ( channel.inline ? '~~' : '' ) + '`[[' + inlinepage + ']]`' + ( channel.inline ? '~~' : '' ) + ' - `' + prefix + 'settings channel inline`';
  64. }
  65. text += '\n' + lang.get('settings.currentwiki') + ' ' + channel.wiki + ' - `' + prefix + 'settings channel wiki`';
  66. if ( !args[1] ) return msg.replyMsg( text, {}, true );
  67. args[0] = args[1].toLowerCase();
  68. args[1] = args.slice(2).join(' ').toLowerCase().trim().replace( /^<\s*(.*)>$/, '$1' );
  69. }
  70. else args[1] = args.slice(1).join(' ').toLowerCase().trim().replace( /^<\s*(.*)>$/, '$1' );
  71. if ( args[0] === 'wiki' ) {
  72. prelang += 'wiki';
  73. var wikihelp = '\n' + lang.get('settings.wikihelp', prefix + 'settings ' + prelang);
  74. if ( !args[1] ) {
  75. if ( !rows.length ) return msg.replyMsg( lang.get('settings.wikimissing') + wikihelp, {}, true );
  76. else return msg.replyMsg( lang.get('settings.' + prelang) + ' ' + ( channel || guild ).wiki + wikihelp, {}, true );
  77. }
  78. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
  79. var isForced = false;
  80. if ( /^<?(?:https?:)?\/\//.test(args[1]) ) {
  81. args[1] = args[1].replace( /^<?(?:https?:)?\/\//, 'https://' );
  82. let value = [];
  83. [args[1], ...value] = args[1].split(/>? /);
  84. if ( value.join(' ') === '--force' ) isForced = true;
  85. }
  86. var wikinew = Wiki.fromInput(args[1]);
  87. if ( !wikinew ) {
  88. var text = lang.get('settings.wikiinvalid') + wikihelp;
  89. var sites = allSites.filter( site => site.wiki_display_name.toLowerCase().includes( args[1] ) );
  90. if ( 0 < sites.length && sites.length < 21 ) {
  91. text += '\n\n' + lang.get('settings.foundwikis') + '\n' + sites.map( site => site.wiki_display_name + ': `' + site.wiki_domain + '`' ).join('\n');
  92. }
  93. return msg.replyMsg( text, {split:true}, true );
  94. }
  95. if ( wikinew.isGamepedia() && !isForced ) {
  96. let site = allSites.find( site => site.wiki_domain === wikinew.hostname );
  97. if ( site ) wikinew = new Wiki('https://' + ( site.wiki_crossover || site.wiki_domain ) + '/');
  98. }
  99. return msg.reactEmoji('⏳', true).then( reaction => {
  100. got.get( wikinew + 'api.php?&action=query&meta=allmessages|siteinfo&ammessages=custom-GamepediaNotice|custom-FandomMergeNotice&amenableparser=true&siprop=general|extensions&format=json' ).then( response => {
  101. if ( !isForced && response.statusCode === 404 && typeof response.body === 'string' ) {
  102. let api = cheerio.load(response.body)('head link[rel="EditURI"]').prop('href');
  103. if ( api ) {
  104. wikinew = new Wiki(api.split('api.php?')[0], wikinew);
  105. return got.get( wikinew + 'api.php?action=query&meta=allmessages|siteinfo&ammessages=custom-GamepediaNotice|custom-FandomMergeNotice&amenableparser=true&siprop=general|extensions&format=json' );
  106. }
  107. }
  108. return response;
  109. } ).then( response => {
  110. var body = response.body;
  111. if ( response.statusCode !== 200 || !body?.query?.allmessages || !body?.query?.general || !body?.query?.extensions ) {
  112. console.log( '- ' + response.statusCode + ': Error while testing the wiki: ' + body?.error?.info );
  113. if ( reaction ) reaction.removeEmoji();
  114. msg.reactEmoji('nowiki', true);
  115. return msg.replyMsg( lang.get('settings.wikiinvalid') + wikihelp, {}, true );
  116. }
  117. if ( !isForced ) wikinew.updateWiki(body.query.general);
  118. if ( wikinew.isGamepedia() && !isForced ) {
  119. let site = allSites.find( site => site.wiki_domain === wikinew.hostname );
  120. if ( site ) wikinew = new Wiki('https://' + ( site.wiki_crossover || site.wiki_domain ) + '/');
  121. }
  122. else if ( wikinew.isFandom(false) && !isForced ) {
  123. let crossover = '';
  124. if ( body.query.allmessages[0]['*'] ) {
  125. crossover = 'https://' + body.query.allmessages[0]['*'] + '.gamepedia.com/';
  126. }
  127. else if ( body.query.allmessages[1]['*'] ) {
  128. let merge = body.query.allmessages[1]['*'].split('/');
  129. crossover = 'https://' + merge[0] + '.fandom.com/' + ( merge[1] ? merge[1] + '/' : '' );
  130. }
  131. if ( crossover ) wikinew = new Wiki(crossover);
  132. }
  133. var embed;
  134. if ( !wikinew.isFandom() ) {
  135. var notice = [];
  136. if ( body.query.general.generator.replace( /^MediaWiki 1\.(\d\d).*$/, '$1' ) < 30 ) {
  137. console.log( '- This wiki is using ' + body.query.general.generator + '.' );
  138. notice.push({
  139. name: 'MediaWiki',
  140. value: lang.get('test.MediaWiki', '[MediaWiki 1.30](https://www.mediawiki.org/wiki/MediaWiki_1.30)', body.query.general.generator)
  141. });
  142. }
  143. if ( !body.query.extensions.some( extension => extension.name === 'TextExtracts' ) ) {
  144. console.log( '- This wiki is missing Extension:TextExtracts.' );
  145. notice.push({
  146. name: 'TextExtracts',
  147. value: lang.get('test.TextExtracts', '[TextExtracts](https://www.mediawiki.org/wiki/Extension:TextExtracts)')
  148. });
  149. }
  150. if ( !body.query.extensions.some( extension => extension.name === 'PageImages' ) ) {
  151. console.log( '- This wiki is missing Extension:PageImages.' );
  152. notice.push({
  153. name: 'PageImages',
  154. value: lang.get('test.PageImages', '[PageImages](https://www.mediawiki.org/wiki/Extension:PageImages)')
  155. });
  156. }
  157. if ( notice.length ) {
  158. embed = new MessageEmbed().setAuthor( body.query.general.sitename ).setTitle( lang.get('test.notice') ).addFields( notice );
  159. }
  160. }
  161. var sql = 'UPDATE discord SET wiki = ? WHERE guild = ? AND wiki = ?';
  162. var sqlargs = [wikinew.href, msg.guild.id, guild.wiki];
  163. if ( !rows.length ) {
  164. sql = 'INSERT INTO discord(wiki, guild, main) VALUES(?, ?, ?)';
  165. sqlargs[2] = msg.guild.id;
  166. }
  167. else if ( channel ) {
  168. sql = 'UPDATE discord SET wiki = ? WHERE guild = ? AND channel = ?';
  169. sqlargs[2] = msg.channel.id;
  170. if ( !rows.includes( channel ) ) {
  171. if ( channel.wiki === wikinew.href ) {
  172. if ( reaction ) reaction.removeEmoji();
  173. return msg.replyMsg( lang.get('settings.' + prelang + 'changed') + ' ' + channel.wiki + wikihelp, {embed}, true );
  174. }
  175. sql = 'INSERT INTO discord(wiki, guild, channel, lang, role, inline, prefix) VALUES(?, ?, ?, ?, ?, ?, ?)';
  176. sqlargs.push(guild.lang, guild.role, guild.inline, guild.prefix);
  177. }
  178. }
  179. return db.run( sql, sqlargs, function (dberror) {
  180. if ( dberror ) {
  181. console.log( '- Error while editing the settings: ' + dberror );
  182. msg.replyMsg( lang.get('settings.save_failed'), {embed}, true );
  183. if ( reaction ) reaction.removeEmoji();
  184. return dberror;
  185. }
  186. console.log( '- Settings successfully updated.' );
  187. if ( channel ) channel.wiki = wikinew.href;
  188. else {
  189. rows.forEach( row => {
  190. if ( row.channel && row.wiki === guild.wiki ) row.wiki = wikinew.href;
  191. } );
  192. guild.wiki = wikinew.href;
  193. }
  194. if ( channel || !rows.some( row => row.channel === msg.channel.id ) ) wiki = new Wiki(wikinew);
  195. if ( reaction ) reaction.removeEmoji();
  196. msg.replyMsg( lang.get('settings.' + prelang + 'changed') + ' ' + wikinew + wikihelp, {embed}, true );
  197. var channels = rows.filter( row => row.channel && row.lang === guild.lang && row.wiki === guild.wiki && row.prefix === guild.prefix && row.role === guild.role && row.inline === guild.inline ).map( row => row.channel );
  198. if ( channels.length ) db.run( 'DELETE FROM discord WHERE channel IN (' + channels.map( row => '?' ).join(', ') + ')', channels, function (delerror) {
  199. if ( delerror ) {
  200. console.log( '- Error while removing the settings: ' + delerror );
  201. return delerror;
  202. }
  203. console.log( '- Settings successfully removed.' );
  204. } );
  205. } );
  206. }, ferror => {
  207. console.log( '- Error while testing the wiki: ' + ferror );
  208. if ( reaction ) reaction.removeEmoji();
  209. msg.reactEmoji('nowiki', true);
  210. return msg.replyMsg( lang.get('settings.wikiinvalid') + wikihelp, {}, true );
  211. } );
  212. } );
  213. }
  214. if ( args[0] === 'lang' ) {
  215. if ( channel && !( msg.guild.id in patreons ) ) return msg.replyMsg( lang.get('general.patreon') + '\n<' + process.env.patreon + '>', {}, true );
  216. prelang += 'lang';
  217. var langhelp = '\n' + lang.get('settings.langhelp', prefix + 'settings ' + prelang) + ' `' + Object.values(allLangs.names).join('`, `') + '`';
  218. if ( !args[1] ) {
  219. return msg.replyMsg( lang.get('settings.' + prelang) + ' `' + allLangs.names[( channel || guild ).lang] + '`' + langhelp, {files:( msg.uploadFiles() ? [`./i18n/widgets/${( channel || guild ).lang}.png`] : [] )}, true );
  220. }
  221. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
  222. if ( !( args[1] in allLangs.map ) ) {
  223. return msg.replyMsg( lang.get('settings.langinvalid') + langhelp, {}, true );
  224. }
  225. var sql = 'UPDATE discord SET lang = ? WHERE guild = ? AND lang = ?';
  226. var sqlargs = [allLangs.map[args[1]], msg.guild.id, guild.lang];
  227. if ( !rows.length ) {
  228. sql = 'INSERT INTO discord(lang, guild, main) VALUES(?, ?, ?)';
  229. sqlargs[2] = msg.guild.id;
  230. }
  231. else if ( channel ) {
  232. sql = 'UPDATE discord SET lang = ? WHERE guild = ? AND channel = ?';
  233. sqlargs[2] = msg.channel.id;
  234. if ( !rows.includes( channel ) ) {
  235. if ( channel.lang === allLangs.map[args[1]] ) {
  236. return msg.replyMsg( lang.get('settings.' + prelang + 'changed') + ' `' + allLangs.names[channel.lang] + '`' + langhelp, {files:( msg.uploadFiles() ? [`./i18n/widgets/${channel.lang}.png`] : [] )}, true );
  237. }
  238. sql = 'INSERT INTO discord(lang, guild, channel, wiki, role, inline, prefix) VALUES(?, ?, ?, ?, ?, ?, ?)';
  239. sqlargs.push(guild.wiki, guild.role, guild.inline, guild.prefix);
  240. }
  241. }
  242. return db.run( sql, sqlargs, function (dberror) {
  243. if ( dberror ) {
  244. console.log( '- Error while editing the settings: ' + dberror );
  245. msg.replyMsg( lang.get('settings.save_failed'), {}, true );
  246. return dberror;
  247. }
  248. console.log( '- Settings successfully updated.' );
  249. if ( channel ) channel.lang = allLangs.map[args[1]];
  250. else {
  251. rows.forEach( row => {
  252. if ( row.channel && row.lang === guild.lang ) row.lang = allLangs.map[args[1]];
  253. } );
  254. guild.lang = allLangs.map[args[1]];
  255. if ( msg.guild.id in voice ) voice[msg.guild.id] = guild.lang;
  256. }
  257. if ( channel || !( msg.guild.id in patreons ) || !rows.some( row => row.channel === msg.channel.id ) ) lang = new Lang(allLangs.map[args[1]]);
  258. msg.replyMsg( lang.get('settings.' + prelang + 'changed') + ' `' + allLangs.names[allLangs.map[args[1]]] + '`\n' + lang.get('settings.langhelp', prefix + 'settings ' + prelang) + ' `' + Object.values(allLangs.names).join('`, `') + '`', {files:( msg.uploadFiles() ? [`./i18n/widgets/${allLangs.map[args[1]]}.png`] : [] )}, true );
  259. var channels = rows.filter( row => row.channel && row.lang === guild.lang && row.wiki === guild.wiki && row.prefix === guild.prefix && row.role === guild.role && row.inline === guild.inline ).map( row => row.channel );
  260. if ( channels.length ) db.run( 'DELETE FROM discord WHERE channel IN (' + channels.map( row => '?' ).join(', ') + ')', channels, function (delerror) {
  261. if ( delerror ) {
  262. console.log( '- Error while removing the settings: ' + delerror );
  263. return delerror;
  264. }
  265. console.log( '- Settings successfully removed.' );
  266. } );
  267. } );
  268. }
  269. if ( args[0] === 'role' ) {
  270. if ( channel && !( msg.guild.id in patreons ) ) return msg.replyMsg( lang.get('general.patreon') + '\n<' + process.env.patreon + '>', {}, true );
  271. prelang += 'role';
  272. var rolehelp = '\n' + lang.get('settings.rolehelp', prefix + 'settings ' + prelang);
  273. if ( !args[1] ) {
  274. return msg.replyMsg( lang.get('settings.' + prelang) + ' ' + ( ( channel || guild ).role ? `<@&${( channel || guild ).role}>` : '@everyone' ) + rolehelp, {}, true );
  275. }
  276. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
  277. var role = null;
  278. if ( /^\d+$/.test(args[1]) ) role = msg.guild.roles.cache.get(args[1]);
  279. if ( !role ) role = msg.guild.roles.cache.find( gc => gc.name.toLowerCase() === args[1].replace( /^@/, '' ) );
  280. if ( !role && ['everyone', 'here', 'none', 'all'].includes( args[1].replace( /^@/, '' ) ) ) {
  281. role = msg.guild.roles.cache.get(msg.guild.id);
  282. }
  283. if ( !role ) {
  284. return msg.replyMsg( lang.get('settings.roleinvalid') + rolehelp, {}, true );
  285. }
  286. role = ( role.id === msg.guild.id ? null : role.id );
  287. var sql = 'UPDATE discord SET role = ? WHERE guild = ?';
  288. var sqlargs = [role, msg.guild.id];
  289. if ( !rows.length ) {
  290. sql = 'INSERT INTO discord(role, guild, main) VALUES(?, ?, ?)';
  291. sqlargs.push(msg.guild.id);
  292. }
  293. else if ( channel ) {
  294. sql = 'UPDATE discord SET role = ? WHERE guild = ? AND channel = ?';
  295. sqlargs.push(msg.channel.id);
  296. if ( !rows.includes( channel ) ) {
  297. if ( channel.role === role ) {
  298. return msg.replyMsg( lang.get('settings.' + prelang + 'changed') + ' ' + ( channel.role ? `<@&${channel.role}>` : '@everyone' ) + rolehelp, {}, true );
  299. }
  300. sql = 'INSERT INTO discord(role, guild, channel, wiki, lang, inline, prefix) VALUES(?, ?, ?, ?, ?, ?, ?)';
  301. sqlargs.push(guild.wiki, guild.lang, guild.inline, guild.prefix);
  302. }
  303. }
  304. else if ( guild.role ) {
  305. sql += ' AND role = ?';
  306. sqlargs.push(guild.role);
  307. }
  308. else sql += ' AND role IS NULL';
  309. return db.run( sql, sqlargs, function (dberror) {
  310. if ( dberror ) {
  311. console.log( '- Error while editing the settings: ' + dberror );
  312. msg.replyMsg( lang.get('settings.save_failed'), {}, true );
  313. return dberror;
  314. }
  315. console.log( '- Settings successfully updated.' );
  316. if ( channel ) channel.role = role;
  317. else {
  318. rows.forEach( row => {
  319. if ( row.channel && row.role === guild.role ) row.role = role;
  320. } );
  321. guild.role = role;
  322. }
  323. msg.replyMsg( lang.get('settings.' + prelang + 'changed') + ' ' + ( role ? `<@&${role}>` : '@everyone' ) + rolehelp, {}, true );
  324. var channels = rows.filter( row => row.channel && row.lang === guild.lang && row.wiki === guild.wiki && row.prefix === guild.prefix && row.role === guild.role && row.inline === guild.inline ).map( row => row.channel );
  325. if ( channels.length ) db.run( 'DELETE FROM discord WHERE channel IN (' + channels.map( row => '?' ).join(', ') + ')', channels, function (delerror) {
  326. if ( delerror ) {
  327. console.log( '- Error while removing the settings: ' + delerror );
  328. return delerror;
  329. }
  330. console.log( '- Settings successfully removed.' );
  331. } );
  332. } );
  333. }
  334. if ( args[0] === 'prefix' && !channel ) {
  335. if ( !( msg.guild.id in patreons ) ) {
  336. return msg.replyMsg( lang.get('general.patreon') + '\n<' + process.env.patreon + '>', {}, true );
  337. }
  338. var prefixhelp = '\n' + lang.get('settings.prefixhelp', prefix + 'settings prefix');
  339. args[1] = args[1].replace( /(?<!\\)_$/, ' ' ).replace( /\\([_\W])/g, '$1' );
  340. if ( !args[1].trim() ) {
  341. return msg.replyMsg( lang.get('settings.prefix') + ' `' + prefix + '`' + prefixhelp, {}, true );
  342. }
  343. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
  344. if ( args[1].includes( '`' ) || args[1].includes( '\\' ) || args[1].length > 100 ) {
  345. return msg.replyMsg( lang.get('settings.prefixinvalid') + prefixhelp, {}, true );
  346. }
  347. if ( args[1] === 'reset' || args[1] === 'default' ) args[1] = process.env.prefix;
  348. var sql = 'UPDATE discord SET prefix = ? WHERE guild = ?';
  349. var sqlargs = [args[1], msg.guild.id];
  350. if ( !rows.length ) {
  351. sql = 'INSERT INTO discord(prefix, guild, main) VALUES(?, ?, ?)';
  352. sqlargs.push(msg.guild.id);
  353. }
  354. return db.run( sql, sqlargs, function (dberror) {
  355. if ( dberror ) {
  356. console.log( '- Error while editing the settings: ' + dberror );
  357. msg.replyMsg( lang.get('settings.save_failed'), {}, true );
  358. return dberror;
  359. }
  360. console.log( '- Settings successfully updated.' );
  361. guild.prefix = args[1];
  362. msg.client.shard.broadcastEval( `global.patreons['${msg.guild.id}'] = '${args[1]}'` );
  363. msg.replyMsg( lang.get('settings.prefixchanged') + ' `' + args[1] + '`\n' + lang.get('settings.prefixhelp', args[1] + 'settings prefix'), {}, true );
  364. } );
  365. }
  366. if ( args[0] === 'inline' ) {
  367. if ( channel && !( msg.guild.id in patreons ) ) return msg.replyMsg( lang.get('general.patreon') + '\n<' + process.env.patreon + '>', {}, true );
  368. prelang += 'inline';
  369. var toggle = 'inline ' + ( ( channel || guild ).inline ? 'disabled' : 'enabled' );
  370. var inlinehelp = '\n' + lang.get('settings.' + toggle + '.help', prefix + 'settings ' + prelang + ' toggle', inlinepage);
  371. if ( args[1] !== 'toggle' ) {
  372. return msg.replyMsg( lang.get('settings.' + toggle + '.' + prelang) + inlinehelp, {}, true );
  373. }
  374. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
  375. var value = ( ( channel || guild ).inline ? null : 1 );
  376. var sql = 'UPDATE discord SET inline = ? WHERE guild = ?';
  377. var sqlargs = [value, msg.guild.id];
  378. if ( !rows.length ) {
  379. sql = 'INSERT INTO discord(inline, guild, main) VALUES(?, ?, ?)';
  380. sqlargs.push(msg.guild.id);
  381. }
  382. else if ( channel ) {
  383. sql = 'UPDATE discord SET inline = ? WHERE guild = ? AND channel = ?';
  384. sqlargs.push(msg.channel.id);
  385. if ( !rows.includes( channel ) ) {
  386. sql = 'INSERT INTO discord(inline, guild, channel, wiki, lang, role, prefix) VALUES(?, ?, ?, ?, ?, ?, ?)';
  387. sqlargs.push(guild.wiki, guild.lang, guild.role, guild.prefix);
  388. }
  389. }
  390. return db.run( sql, sqlargs, function (dberror) {
  391. if ( dberror ) {
  392. console.log( '- Error while editing the settings: ' + dberror );
  393. msg.replyMsg( lang.get('settings.save_failed'), {}, true );
  394. return dberror;
  395. }
  396. console.log( '- Settings successfully updated.' );
  397. if ( channel ) channel.inline = value;
  398. else {
  399. rows.forEach( row => {
  400. if ( row.channel && row.inline === guild.inline ) row.inline = value;
  401. } );
  402. guild.inline = value;
  403. }
  404. toggle = 'inline ' + ( ( channel || guild ).inline ? 'disabled' : 'enabled' );
  405. msg.replyMsg( lang.get('settings.' + toggle + '.' + prelang + 'changed') + '\n' + lang.get('settings.' + toggle + '.help', prefix + 'settings ' + prelang + ' toggle', inlinepage), {}, true );
  406. var channels = rows.filter( row => row.channel && row.lang === guild.lang && row.wiki === guild.wiki && row.prefix === guild.prefix && row.role === guild.role && row.inline === guild.inline ).map( row => row.channel );
  407. if ( channels.length ) db.run( 'DELETE FROM discord WHERE channel IN (' + channels.map( row => '?' ).join(', ') + ')', channels, function (delerror) {
  408. if ( delerror ) {
  409. console.log( '- Error while removing the settings: ' + delerror );
  410. return delerror;
  411. }
  412. console.log( '- Settings successfully removed.' );
  413. } );
  414. } );
  415. }
  416. return msg.replyMsg( text, {split:true}, true );
  417. } );
  418. }
  419. module.exports = {
  420. name: 'settings',
  421. everyone: true,
  422. pause: true,
  423. owner: false,
  424. run: cmd_settings
  425. };