patreon.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. } );
  86. } );
  87. if ( args[1] ) args[1] = args[1].replace( /^\\?<@!?(\d+)>$/, '$1' );
  88. if ( args[0] === 'check' ) {
  89. 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) => {
  90. if ( dberror ) {
  91. console.log( '- Error while getting the patreon: ' + dberror );
  92. msg.replyMsg( 'I got an error while searching for you, please try again later.', {}, true );
  93. return dberror;
  94. }
  95. if ( !row ) return msg.replyMsg( 'you can\'t have any server.', {}, true );
  96. var text = 'you can have up to ' + row.count + ' server.\n\n';
  97. if ( row.guilds ) {
  98. msg.client.shard.broadcastEval( `'${row.guilds}'.split(',').map( guild => this.guilds.cache.get(guild)?.name )` ).then( results => {
  99. var guilds = row.guilds.split(',').map( (guild, i) => '`' + guild + '` ' + ( results.find( result => result[i] !== null )?.[i] || '' ) );
  100. text += 'Currently you have ' + guilds.length + ' server:\n' + guilds.join('\n');
  101. msg.replyMsg( text, {}, true );
  102. } );
  103. }
  104. else {
  105. text += '*You don\'t have any server yet.*';
  106. msg.replyMsg( text, {}, true );
  107. }
  108. } );
  109. 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) => {
  110. if ( dberror ) {
  111. console.log( '- Error while getting the patreon: ' + dberror );
  112. msg.replyMsg( 'I got an error while searching for <@' + args[1] + '>, please try again later.', {}, true );
  113. return dberror;
  114. }
  115. if ( !row ) return msg.replyMsg( '<@' + args[1] + '> can\'t have any server.', {}, true );
  116. var text = '<@' + args[1] + '> can have up to ' + row.count + ' server.\n\n';
  117. if ( row.guilds ) {
  118. msg.client.shard.broadcastEval( `'${row.guilds}'.split(',').map( guild => this.guilds.cache.get(guild)?.name )` ).then( results => {
  119. var guilds = row.guilds.split(',').map( (guild, i) => '`' + guild + '` ' + ( results.find( result => result[i] !== null )?.[i] || '' ) );
  120. text += 'Currently they have ' + guilds.length + ' server:\n' + guilds.join('\n');
  121. msg.replyMsg( text, {}, true );
  122. } );
  123. }
  124. else {
  125. text += '*They don\'t have any server yet.*';
  126. msg.replyMsg( text, {}, true );
  127. }
  128. } );
  129. }
  130. 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) => {
  131. if ( dberror ) {
  132. console.log( '- Error while getting the patreon: ' + dberror );
  133. msg.replyMsg( 'I got an error while searching for <@' + args[1] + '>, please try again later.', {}, true );
  134. return dberror;
  135. }
  136. var value = parseInt(args[2], 10);
  137. var count = ( row ? row.count : 0 );
  138. var guilds = ( row && row.guilds ? row.guilds.split(',') : [] );
  139. if ( args[2].startsWith( '+' ) || args[2].startsWith( '-' ) ) count += value;
  140. else count = value;
  141. if ( count <= 0 ) return db.run( 'DELETE FROM patreons WHERE patreon = ?', [args[1]], function (error) {
  142. if ( error ) {
  143. console.log( '- Error while deleting the patreon: ' + error );
  144. msg.replyMsg( 'I got an error while deleting <@' + args[1] + '>, please try again later.', {}, true );
  145. return error;
  146. }
  147. console.log( '- Patreon successfully deleted.' );
  148. if ( !guilds.length ) return msg.replyMsg( '<@' + args[1] + '> is no longer a patreon.', {}, true );
  149. db.each( 'SELECT guild, lang, inline FROM discord WHERE guild IN (' + guilds.map( guild => '?' ).join(', ') + ') AND channel IS NULL', guilds, (eacherror, eachrow) => {
  150. if ( eacherror ) {
  151. console.log( '- Error while getting the guild: ' + eacherror );
  152. msg.replyMsg( 'I couldn\'t disable the patreon features.', {}, true );
  153. return eacherror;
  154. }
  155. db.run( 'UPDATE discord SET lang = ?, inline = ?, prefix = ?, patreon = NULL WHERE guild = ?', [eachrow.lang, eachrow.inline, process.env.prefix, eachrow.guild], function (uperror) {
  156. if ( uperror ) {
  157. console.log( '- Error while updating the guild: ' + uperror );
  158. msg.replyMsg( 'I couldn\'t disable the patreon features for `' + eachrow.guild + '`.', {}, true );
  159. return uperror;
  160. }
  161. console.log( '- Guild successfully updated.' );
  162. msg.client.shard.broadcastEval( `delete global.patreons['${eachrow.guild}']` );
  163. } );
  164. }, (eacherror) => {
  165. if ( eacherror ) {
  166. console.log( '- Error while getting the guilds: ' + eacherror );
  167. msg.replyMsg( 'I couldn\'t disable the patreon features for `' + guilds.join('`, `') + '`.', {}, true );
  168. return eacherror;
  169. }
  170. msg.replyMsg( '<@' + args[1] + '> is no longer a patreon.', {}, true );
  171. } );
  172. 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) => {
  173. if ( eacherror ) {
  174. console.log( '- Error while getting the verifications: ' + eacherror );
  175. return eacherror;
  176. }
  177. var ids = eachrow.configids.split(',').slice(verificationLimit.default);
  178. if ( ids.length ) db.run( 'DELETE FROM verification WHERE guild = ? AND configid IN (' + ids.map( configid => '?' ).join(', ') + ')', [eachrow.guild, ...ids], function (uperror) {
  179. if ( uperror ) {
  180. console.log( '- Error while deleting the verifications: ' + uperror );
  181. return uperror;
  182. }
  183. console.log( '- Verifications successfully deleted.' );
  184. } );
  185. }, (eacherror) => {
  186. if ( eacherror ) {
  187. console.log( '- Error while getting the verifications: ' + eacherror );
  188. return eacherror;
  189. }
  190. } );
  191. } );
  192. if ( !row ) return db.run( 'INSERT INTO patreons(patreon, count) VALUES(?, ?)', [args[1], count], function (error) {
  193. if ( error ) {
  194. console.log( '- Error while adding the patreon: ' + error );
  195. msg.replyMsg( 'I got an error while adding <@' + args[1] + '>, please try again later.', {}, true );
  196. return error;
  197. }
  198. console.log( '- Patreon successfully added.' );
  199. msg.replyMsg( '<@' + args[1] + '> can now have up to ' + count + ' server.', {}, true );
  200. } );
  201. db.run( 'UPDATE patreons SET count = ? WHERE patreon = ?', [count, args[1]], function (error) {
  202. if ( error ) {
  203. console.log( '- Error while updating the patreon: ' + error );
  204. msg.replyMsg( 'I got an error while updating <@' + args[1] + '>, please try again later.', {}, true );
  205. return error;
  206. }
  207. console.log( '- Patreon successfully updated.' );
  208. var text = '<@' + args[1] + '> can now have up to ' + count + ' server.';
  209. if ( count < guilds.length ) text += '\n\n**They are now above their server limit!**';
  210. msg.replyMsg( text, {}, true );
  211. } );
  212. } );
  213. if ( msg.channel.type !== 'text' || !pause[msg.guild.id] ) this.LINK(lang, msg, line, wiki);
  214. }
  215. module.exports = {
  216. name: 'patreon',
  217. everyone: true,
  218. pause: true,
  219. owner: true,
  220. run: cmd_patreon
  221. };