patreon.js 15 KB

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