patreon.js 15 KB

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