patreon.js 15 KB

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