patreon.js 12 KB

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