patreon.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. const {defaultPermissions, limit: {verification: verificationLimit, rcgcdw: rcgcdwLimit}} = require('../util/default.json');
  2. var db = require('../util/database.js');
  3. /**
  4. * Processes the "patreon" command.
  5. * @param {import('../util/i18n.js')} lang - The user language.
  6. * @param {import('discord.js').Message} msg - The Discord message.
  7. * @param {String[]} args - The command arguments.
  8. * @param {String} line - The command as plain text.
  9. * @param {import('../util/wiki.js')} wiki - The wiki for the message.
  10. */
  11. function cmd_patreon(lang, msg, args, line, wiki) {
  12. if ( !( process.env.channel.split('|').includes( msg.channel.id ) && args.join('') ) ) {
  13. if ( !msg.channel.isGuild() || !pause[msg.guild.id] ) this.LINK(lang, msg, line, wiki);
  14. return;
  15. }
  16. if ( args[0] === 'enable' && /^\d+$/.test(args.slice(1).join(' ')) ) return msg.client.shard.broadcastEval( `this.guilds.cache.get('${args[1]}')?.name` ).then( results => {
  17. var guild = results.find( result => result !== null );
  18. if ( guild === undefined ) return msg.client.generateInvite({
  19. permissions: defaultPermissions,
  20. guild: args[1]
  21. }).then( invite => {
  22. msg.replyMsg( 'I\'m not on a server with the id `' + args[1] + '`.\n<' + invite + '>', {}, true )
  23. }, log_error );
  24. if ( args[1] in patreons ) return msg.replyMsg( '"' + guild + '" has the patreon features already enabled.', {}, true );
  25. db.get( 'SELECT count, COUNT(guild) guilds FROM patreons LEFT JOIN discord ON discord.patreon = patreons.patreon WHERE patreons.patreon = ? GROUP BY patreons.patreon', [msg.author.id], (dberror, row) => {
  26. if ( dberror ) {
  27. console.log( '- Error while getting the patreon: ' + dberror );
  28. msg.replyMsg( 'I got an error while searching for you, please try again later.', {}, true );
  29. return dberror;
  30. }
  31. if ( !row ) return msg.replyMsg( 'you can\'t have any server.', {}, true );
  32. if ( row.count <= row.guilds ) return msg.replyMsg( 'you already reached your maximal server count.', {}, true );
  33. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
  34. db.run( 'UPDATE discord SET patreon = ? WHERE guild = ? AND channel IS NULL', [msg.author.id, args[1]], function (error) {
  35. if ( error ) {
  36. console.log( '- Error while updating the guild: ' + error );
  37. msg.replyMsg( 'I got an error while updating the server, please try again later.', {}, true );
  38. return error;
  39. }
  40. if ( !this.changes ) return db.run( 'INSERT INTO discord(main, guild, patreon) VALUES(?, ?, ?)', [args[1], args[1], msg.author.id], function (inserror) {
  41. if ( inserror ) {
  42. console.log( '- Error while adding the guild: ' + inserror );
  43. msg.replyMsg( 'I got an error while updating the server, please try again later.', {}, true );
  44. return inserror;
  45. }
  46. console.log( '- Guild successfully added.' );
  47. msg.client.shard.broadcastEval( `global.patreons['${args[1]}'] = '${process.env.prefix}'` );
  48. msg.replyMsg( 'the patreon features are now enabled on "' + guild + '".', {}, true );
  49. } );
  50. console.log( '- Guild successfully updated.' );
  51. msg.client.shard.broadcastEval( `global.patreons['${args[1]}'] = '${process.env.prefix}'` );
  52. msg.replyMsg( 'the patreon features are now enabled on "' + guild + '".', {}, true );
  53. } );
  54. } );
  55. } );
  56. if ( args[0] === 'disable' && /^\d+$/.test(args.slice(1).join(' ')) ) return msg.client.shard.broadcastEval( `this.guilds.cache.get('${args[1]}')?.name` ).then( results => {
  57. var guild = results.find( result => result !== null );
  58. if ( guild === undefined ) return msg.replyMsg( 'I\'m not on a server with the id `' + args[1] + '`.', {}, true );
  59. if ( !( args[1] in patreons ) ) return msg.replyMsg( '"' + guild + '" doesn\'t have the patreon features enabled.', {}, true );
  60. db.get( 'SELECT lang, inline FROM discord WHERE guild = ? AND patreon = ?', [args[1], msg.author.id], (dberror, row) => {
  61. if ( dberror ) {
  62. console.log( '- Error while getting the guild: ' + dberror );
  63. msg.replyMsg( 'I got an error while searching for the server, please try again later.', {}, true );
  64. return dberror;
  65. }
  66. if ( !row ) return msg.replyMsg( 'you didn\'t enable the patreon features for "' + guild + '"!', {}, true );
  67. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
  68. db.run( 'UPDATE discord SET lang = ?, inline = ?, prefix = ?, patreon = NULL WHERE guild = ?', [row.lang, row.inline, process.env.prefix, args[1]], function (error) {
  69. if ( error ) {
  70. console.log( '- Error while updating the guild: ' + error );
  71. msg.replyMsg( 'I got an error while updating the server, please try again later.', {}, true );
  72. return error;
  73. }
  74. console.log( '- Guild successfully updated.' );
  75. msg.client.shard.broadcastEval( `delete global.patreons['${args[1]}']` );
  76. msg.replyMsg( 'the patreon features are now disabled on "' + guild + '".', {}, true );
  77. } );
  78. db.all( 'SELECT configid FROM verification WHERE guild = ? ORDER BY configid ASC', [args[1]], (dberror, rows) => {
  79. if ( dberror ) {
  80. console.log( '- Error while getting the verifications: ' + dberror );
  81. return dberror;
  82. }
  83. var ids = rows.slice(verificationLimit.default).map( row => row.configid );
  84. if ( ids.length ) db.run( 'DELETE FROM verification WHERE guild = ? AND configid IN (' + ids.map( configid => '?' ).join(', ') + ')', [args[1], ...ids], function (error) {
  85. if ( error ) {
  86. console.log( '- Error while deleting the verifications: ' + error );
  87. return error;
  88. }
  89. console.log( '- Verifications successfully deleted.' );
  90. } );
  91. } );
  92. db.all( 'SELECT webhook FROM rcgcdw WHERE guild = ? ORDER BY configid ASC', [args[1]], (dberror, rows) => {
  93. if ( dberror ) {
  94. console.log( '- Error while getting the RcGcDw: ' + dberror );
  95. return dberror;
  96. }
  97. var webhooks = rows.slice(rcgcdwLimit.default).map( row => row.webhook );
  98. if ( webhooks.length ) db.run( 'DELETE FROM rcgcdw WHERE webhook IN (' + webhooks.map( webhook => '?' ).join(', ') + ')', webhooks, function (error) {
  99. if ( error ) {
  100. console.log( '- Error while deleting the RcGcDw: ' + error );
  101. return error;
  102. }
  103. console.log( '- RcGcDw successfully deleted.' );
  104. webhooks.forEach( hook => msg.client.fetchWebhook(...hook.split('/')).then( webhook => {
  105. webhook.delete('Removed extra recent changes webhook').catch(log_error);
  106. }, log_error ) );
  107. } );
  108. } );
  109. db.run( 'UPDATE rcgcdw SET display = ? WHERE guild = ? AND display > ?', [rcgcdwLimit.display, args[1], rcgcdwLimit.display], function (dberror) {
  110. if ( dberror ) {
  111. console.log( '- Error while updating the RcGcDw: ' + dberror );
  112. return dberror;
  113. }
  114. console.log( '- RcGcDw successfully updated.' );
  115. } );
  116. } );
  117. } );
  118. if ( args[1] ) args[1] = args[1].replace( /^\\?<@!?(\d+)>$/, '$1' );
  119. if ( args[0] === 'check' ) {
  120. if ( !args.slice(1).join('') ) return db.get( 'SELECT count, GROUP_CONCAT(guild) guilds FROM patreons LEFT JOIN discord ON discord.patreon = patreons.patreon WHERE patreons.patreon = ? GROUP BY patreons.patreon', [msg.author.id], (dberror, row) => {
  121. if ( dberror ) {
  122. console.log( '- Error while getting the patreon: ' + dberror );
  123. msg.replyMsg( 'I got an error while searching for you, please try again later.', {}, true );
  124. return dberror;
  125. }
  126. if ( !row ) return msg.replyMsg( 'you can\'t have any server.', {}, true );
  127. var text = 'you can have up to ' + row.count + ' server.\n\n';
  128. if ( row.guilds ) {
  129. msg.client.shard.broadcastEval( `'${row.guilds}'.split(',').map( guild => this.guilds.cache.get(guild)?.name )` ).then( results => {
  130. var guilds = row.guilds.split(',').map( (guild, i) => '`' + guild + '` ' + ( results.find( result => result[i] !== null )?.[i] || '' ) );
  131. text += 'Currently you have ' + guilds.length + ' server:\n' + guilds.join('\n');
  132. msg.replyMsg( text, {}, true );
  133. } );
  134. }
  135. else {
  136. text += '*You don\'t have any server yet.*';
  137. msg.replyMsg( text, {}, true );
  138. }
  139. } );
  140. if ( msg.isOwner() && /^\d+$/.test(args.slice(1).join(' ')) ) return db.get( 'SELECT count, GROUP_CONCAT(guild) guilds FROM patreons LEFT JOIN discord ON discord.patreon = patreons.patreon WHERE patreons.patreon = ? GROUP BY patreons.patreon', [args[1]], (dberror, row) => {
  141. if ( dberror ) {
  142. console.log( '- Error while getting the patreon: ' + dberror );
  143. msg.replyMsg( 'I got an error while searching for <@' + args[1] + '>, please try again later.', {}, true );
  144. return dberror;
  145. }
  146. if ( !row ) return msg.replyMsg( '<@' + args[1] + '> can\'t have any server.', {}, true );
  147. var text = '<@' + args[1] + '> can have up to ' + row.count + ' server.\n\n';
  148. if ( row.guilds ) {
  149. msg.client.shard.broadcastEval( `'${row.guilds}'.split(',').map( guild => this.guilds.cache.get(guild)?.name )` ).then( results => {
  150. var guilds = row.guilds.split(',').map( (guild, i) => '`' + guild + '` ' + ( results.find( result => result[i] !== null )?.[i] || '' ) );
  151. text += 'Currently they have ' + guilds.length + ' server:\n' + guilds.join('\n');
  152. msg.replyMsg( text, {}, true );
  153. } );
  154. }
  155. else {
  156. text += '*They don\'t have any server yet.*';
  157. msg.replyMsg( text, {}, true );
  158. }
  159. } );
  160. }
  161. if ( args[0] === 'edit' && msg.isOwner() && /^\d+ [\+\-]?\d+$/.test(args.slice(1).join(' ')) ) return db.get( 'SELECT count, GROUP_CONCAT(guild) guilds FROM patreons LEFT JOIN discord ON discord.patreon = patreons.patreon WHERE patreons.patreon = ? GROUP BY patreons.patreon', [args[1]], (dberror, row) => {
  162. if ( dberror ) {
  163. console.log( '- Error while getting the patreon: ' + dberror );
  164. msg.replyMsg( 'I got an error while searching for <@' + args[1] + '>, please try again later.', {}, true );
  165. return dberror;
  166. }
  167. var value = parseInt(args[2], 10);
  168. var count = ( row ? row.count : 0 );
  169. var guilds = ( row && row.guilds ? row.guilds.split(',') : [] );
  170. if ( args[2].startsWith( '+' ) || args[2].startsWith( '-' ) ) count += value;
  171. else count = value;
  172. if ( process.env.READONLY ) return msg.replyMsg( lang.get('general.readonly') + '\n' + process.env.invite, {}, true );
  173. if ( count <= 0 ) return db.run( 'DELETE FROM patreons WHERE patreon = ?', [args[1]], function (error) {
  174. if ( error ) {
  175. console.log( '- Error while deleting the patreon: ' + error );
  176. msg.replyMsg( 'I got an error while deleting <@' + args[1] + '>, please try again later.', {}, true );
  177. return error;
  178. }
  179. console.log( '- Patreon successfully deleted.' );
  180. if ( !guilds.length ) return msg.replyMsg( '<@' + args[1] + '> is no longer a patreon.', {}, true );
  181. db.each( 'SELECT guild, lang, inline FROM discord WHERE guild IN (' + guilds.map( guild => '?' ).join(', ') + ') AND channel IS NULL', guilds, (eacherror, eachrow) => {
  182. if ( eacherror ) {
  183. console.log( '- Error while getting the guild: ' + eacherror );
  184. msg.replyMsg( 'I couldn\'t disable the patreon features.', {}, true );
  185. return eacherror;
  186. }
  187. db.run( 'UPDATE discord SET lang = ?, inline = ?, prefix = ?, patreon = NULL WHERE guild = ?', [eachrow.lang, eachrow.inline, process.env.prefix, eachrow.guild], function (uperror) {
  188. if ( uperror ) {
  189. console.log( '- Error while updating the guild: ' + uperror );
  190. msg.replyMsg( 'I couldn\'t disable the patreon features for `' + eachrow.guild + '`.', {}, true );
  191. return uperror;
  192. }
  193. console.log( '- Guild successfully updated.' );
  194. msg.client.shard.broadcastEval( `delete global.patreons['${eachrow.guild}']` );
  195. } );
  196. }, (eacherror) => {
  197. if ( eacherror ) {
  198. console.log( '- Error while getting the guilds: ' + eacherror );
  199. msg.replyMsg( 'I couldn\'t disable the patreon features for `' + guilds.join('`, `') + '`.', {}, true );
  200. return eacherror;
  201. }
  202. msg.replyMsg( '<@' + args[1] + '> is no longer a patreon.', {}, true );
  203. } );
  204. db.each( 'SELECT a.guild, GROUP_CONCAT(DISTINCT a.configid) configids FROM verification a LEFT JOIN verification b ON a.guild = b.guild WHERE a.guild IN (' + guilds.map( guild => '?' ).join(', ') + ') GROUP BY a.guild', guilds, (eacherror, eachrow) => {
  205. if ( eacherror ) {
  206. console.log( '- Error while getting the verifications: ' + eacherror );
  207. return eacherror;
  208. }
  209. var ids = eachrow.configids.split(',').slice(verificationLimit.default);
  210. if ( ids.length ) db.run( 'DELETE FROM verification WHERE guild = ? AND configid IN (' + ids.map( configid => '?' ).join(', ') + ')', [eachrow.guild, ...ids], function (uperror) {
  211. if ( uperror ) {
  212. console.log( '- Error while deleting the verifications: ' + uperror );
  213. return uperror;
  214. }
  215. console.log( '- Verifications successfully deleted.' );
  216. } );
  217. }, (eacherror) => {
  218. if ( eacherror ) {
  219. console.log( '- Error while getting the verifications: ' + eacherror );
  220. return eacherror;
  221. }
  222. } );
  223. db.each( 'SELECT GROUP_CONCAT(DISTINCT a.webhook) webhooks FROM rcgcdw a LEFT JOIN verification b ON a.guild = b.guild WHERE a.guild IN (' + guilds.map( guild => '?' ).join(', ') + ') GROUP BY a.guild', guilds, (eacherror, eachrow) => {
  224. if ( eacherror ) {
  225. console.log( '- Error while getting the RcGcDw: ' + eacherror );
  226. return eacherror;
  227. }
  228. var webhooks = eachrow.webhooks.split(',').slice(rcgcdwLimit.default);
  229. if ( webhooks.length ) db.run( 'DELETE FROM rcgcdw WHERE webhook IN (' + webhooks.map( webhook => '?' ).join(', ') + ')', webhooks, function (uperror) {
  230. if ( uperror ) {
  231. console.log( '- Error while deleting the RcGcDw: ' + uperror );
  232. return uperror;
  233. }
  234. console.log( '- RcGcDw successfully deleted.' );
  235. webhooks.forEach( hook => msg.client.fetchWebhook(...hook.split('/')).then( webhook => {
  236. webhook.delete('Removed extra recent changes webhook').catch(log_error);
  237. }, log_error ) );
  238. } );
  239. }, (eacherror) => {
  240. if ( eacherror ) {
  241. console.log( '- Error while getting the RcGcDw: ' + eacherror );
  242. return eacherror;
  243. }
  244. } );
  245. db.run( 'UPDATE rcgcdw SET display = ? WHERE guild IN (' + guilds.map( guild => '?' ).join(', ') + ') AND display > ?', [rcgcdwLimit.display, ...guilds, rcgcdwLimit.display], function (uperror) {
  246. if ( uperror ) {
  247. console.log( '- Error while updating the RcGcDw: ' + uperror );
  248. return uperror;
  249. }
  250. console.log( '- RcGcDw successfully updated.' );
  251. } );
  252. } );
  253. if ( !row ) return db.run( 'INSERT INTO patreons(patreon, count) VALUES(?, ?)', [args[1], count], function (error) {
  254. if ( error ) {
  255. console.log( '- Error while adding the patreon: ' + error );
  256. msg.replyMsg( 'I got an error while adding <@' + args[1] + '>, please try again later.', {}, true );
  257. return error;
  258. }
  259. console.log( '- Patreon successfully added.' );
  260. msg.replyMsg( '<@' + args[1] + '> can now have up to ' + count + ' server.', {}, true );
  261. } );
  262. db.run( 'UPDATE patreons SET count = ? WHERE patreon = ?', [count, args[1]], function (error) {
  263. if ( error ) {
  264. console.log( '- Error while updating the patreon: ' + error );
  265. msg.replyMsg( 'I got an error while updating <@' + args[1] + '>, please try again later.', {}, true );
  266. return error;
  267. }
  268. console.log( '- Patreon successfully updated.' );
  269. var text = '<@' + args[1] + '> can now have up to ' + count + ' server.';
  270. if ( count < guilds.length ) text += '\n\n**They are now above their server limit!**';
  271. msg.replyMsg( text, {}, true );
  272. } );
  273. } );
  274. if ( !msg.channel.isGuild() || !pause[msg.guild.id] ) this.LINK(lang, msg, line, wiki);
  275. }
  276. module.exports = {
  277. name: 'patreon',
  278. everyone: true,
  279. pause: true,
  280. owner: true,
  281. run: cmd_patreon
  282. };