eval.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. const util = require('util');
  2. util.inspect.defaultOptions = {compact:false,breakLength:Infinity};
  3. const Discord = require('discord.js');
  4. const {limit: {verification: verificationLimit, rcgcdw: rcgcdwLimit}} = require('../util/default.json');
  5. const newMessage = require('../util/newMessage.js');
  6. var db = require('../util/database.js');
  7. /**
  8. * Processes the "eval" command.
  9. * @param {import('../util/i18n.js')} lang - The user language.
  10. * @param {Discord.Message} msg - The Discord message.
  11. * @param {String[]} args - The command arguments.
  12. * @param {String} line - The command as plain text.
  13. * @param {String} wiki - The wiki for the message.
  14. * @async
  15. */
  16. async function cmd_eval(lang, msg, args, line, wiki) {
  17. try {
  18. var text = util.inspect( await eval( args.join(' ') ) );
  19. } catch ( error ) {
  20. var text = error.toString();
  21. }
  22. if ( isDebug ) console.log( '--- EVAL START ---\n' + text + '\n--- EVAL END ---' );
  23. if ( text.length > 2000 ) msg.reactEmoji('✅', true);
  24. else msg.sendChannel( '```js\n' + text + '\n```', {split:{prepend:'```js\n',append:'\n```'},allowedMentions:{}}, true );
  25. /**
  26. * Runs a command with admin permissions.
  27. * @param {String} cmdline - The message text.
  28. */
  29. function backdoor(cmdline) {
  30. msg.evalUsed = true;
  31. newMessage(msg, lang, wiki, patreons[msg.guild.id], msg.noInline, cmdline);
  32. return cmdline;
  33. }
  34. }
  35. /**
  36. * Runs database queries.
  37. * @param {String} sql - The SQL command.
  38. * @param {String[]} [sqlargs] - The command arguments.
  39. */
  40. function database(sql, sqlargs = []) {
  41. return new Promise( function (resolve, reject) {
  42. db.all( sql, sqlargs, (error, rows) => {
  43. if (error) reject(error);
  44. resolve(rows);
  45. } );
  46. } );
  47. }
  48. /**
  49. * Update the list of all sites.
  50. * @returns {Promise<Object[]>}
  51. */
  52. function updateAllSites() {
  53. return require('../util/allSites.js').update();
  54. }
  55. /**
  56. * Removes the patreon features for a guild.
  57. * @param {String} guild - The guild ID.
  58. * @param {Discord.Message} msg - The Discord message.
  59. */
  60. function removePatreons(guild, msg) {
  61. try {
  62. if ( !( typeof guild === 'string' || msg instanceof Discord.Message ) ) {
  63. return 'removePatreons(guild, msg) – No guild or message provided!';
  64. }
  65. db.get( 'SELECT lang, inline FROM discord WHERE guild = ? AND channel IS NULL', [guild], (dberror, row) => {
  66. try {
  67. if ( dberror ) {
  68. console.log( '- Error while getting the guild: ' + dberror );
  69. msg.replyMsg( 'I got an error while searching for the guild!', {}, true );
  70. return dberror;
  71. }
  72. if ( !row ) {
  73. msg.replyMsg( 'that guild doesn\'t exist!', {}, true );
  74. return;
  75. }
  76. db.run( 'UPDATE discord SET lang = ?, inline = ?, prefix = ?, patreon = NULL WHERE guild = ?', [row.lang, row.inline, process.env.prefix, guild], function (error) {
  77. try {
  78. if ( error ) {
  79. console.log( '- Error while updating the guild: ' + error );
  80. msg.replyMsg( 'I got an error while updating the guild!', {}, true );
  81. return error;
  82. }
  83. console.log( '- Guild successfully updated.' );
  84. msg.client.shard.broadcastEval( `delete global.patreons['${guild}']`);
  85. msg.replyMsg( 'the patreon features are now disabled on that guild.', {}, true );
  86. }
  87. catch ( tryerror ) {
  88. console.log( '- Error while removing the patreon features: ' + tryerror );
  89. }
  90. } );
  91. }
  92. catch ( tryerror ) {
  93. console.log( '- Error while removing the patreon features: ' + tryerror );
  94. }
  95. } );
  96. db.all( 'SELECT configid FROM verification WHERE guild = ? ORDER BY configid ASC', [guild], (dberror, rows) => {
  97. if ( dberror ) {
  98. console.log( '- Error while getting the verifications: ' + dberror );
  99. return dberror;
  100. }
  101. var ids = rows.slice(verificationLimit.default).map( row => row.configid );
  102. if ( ids.length ) db.run( 'DELETE FROM verification WHERE guild = ? AND configid IN (' + ids.map( configid => '?' ).join(', ') + ')', [guild, ...ids], function (error) {
  103. if ( error ) {
  104. console.log( '- Error while deleting the verifications: ' + error );
  105. return error;
  106. }
  107. console.log( '- Verifications successfully deleted.' );
  108. } );
  109. } );
  110. db.all( 'SELECT webhook FROM rcgcdw WHERE guild = ? ORDER BY configid ASC', [guild], (dberror, rows) => {
  111. if ( dberror ) {
  112. console.log( '- Error while getting the RcGcDw: ' + dberror );
  113. return dberror;
  114. }
  115. var webhooks = rows.slice(rcgcdwLimit.default).map( row => row.webhook );
  116. if ( webhooks.length ) db.run( 'DELETE FROM rcgcdw WHERE webhook IN (' + webhooks.map( webhook => '?' ).join(', ') + ')', webhooks, function (error) {
  117. if ( error ) {
  118. console.log( '- Error while deleting the RcGcDw: ' + error );
  119. return error;
  120. }
  121. console.log( '- RcGcDw successfully deleted.' );
  122. webhooks.forEach( hook => guild.client.fetchWebhook(...hook.split('/')).then( webhook => {
  123. webhook.delete('Removed extra recent changes webhook').catch(log_error);
  124. }, log_error ) );
  125. } );
  126. } );
  127. db.run( 'UPDATE rcgcdw SET display = ? WHERE guild = ? AND display > ?', [rcgcdwLimit.display, guild, rcgcdwLimit.display], function (dberror) {
  128. if ( dberror ) {
  129. console.log( '- Error while updating the RcGcDw: ' + dberror );
  130. return dberror;
  131. }
  132. console.log( '- RcGcDw successfully updated.' );
  133. } );
  134. }
  135. catch ( tryerror ) {
  136. console.log( '- Error while removing the patreon features: ' + tryerror );
  137. return 'removePatreons(guild, msg) – Error while removing the patreon features: ' + tryerror;
  138. }
  139. }
  140. /**
  141. * Removes the settings for deleted guilds and channels.
  142. * @param {Discord.Message} msg - The Discord message.
  143. */
  144. function removeSettings(msg) {
  145. if ( !msg ) return 'removeSettings(msg) – No message provided!';
  146. try {
  147. msg.client.shard.broadcastEval( `[[...this.guilds.cache.keys()], [...this.channels.cache.filter( channel => channel.type === 'text' ).keys()]]` ).then( results => {
  148. var all_guilds = results.map( result => result[0] ).reduce( (acc, val) => acc.concat(val), [] );
  149. var all_channels = results.map( result => result[1] ).reduce( (acc, val) => acc.concat(val), [] );
  150. var guilds = [];
  151. var channels = [];
  152. db.each( 'SELECT guild, channel FROM discord', [], (dberror, row) => {
  153. if ( dberror ) {
  154. console.log( '- Error while getting the setting: ' + dberror );
  155. return dberror;
  156. }
  157. if ( !row.channel && !all_guilds.includes(row.guild) ) {
  158. if ( row.guild in patreons ) msg.client.shard.broadcastEval( `delete global.patreons['${row.guild}']` );
  159. if ( row.guild in voice ) delete voice[row.guild];
  160. return guilds.push(row.guild);
  161. }
  162. if ( row.channel && all_guilds.includes(row.guild) && !all_channels.includes(row.channel) ) return channels.push(row.channel);
  163. }, (error) => {
  164. if ( error ) {
  165. console.log( '- Error while getting the settings: ' + error );
  166. msg.replyMsg( 'I got an error while getting the settings!', {}, true );
  167. return error;
  168. }
  169. if ( guilds.length ) {
  170. db.run( 'DELETE FROM discord WHERE guild IN (' + guilds.map( guild => '?' ).join(', ') + ')', guilds, function (dberror) {
  171. if ( dberror ) {
  172. console.log( '- Error while removing the guilds: ' + dberror );
  173. msg.replyMsg( 'I got an error while removing the guilds!', {}, true );
  174. return dberror;
  175. }
  176. console.log( '- Guilds successfully removed.' );
  177. } );
  178. db.run( 'DELETE FROM verification WHERE guild IN (' + guilds.map( guild => '?' ).join(', ') + ')', guilds, function (dberror) {
  179. if ( dberror ) {
  180. console.log( '- Error while removing the verifications: ' + dberror );
  181. msg.replyMsg( 'I got an error while removing the verifications!', {}, true );
  182. return dberror;
  183. }
  184. console.log( '- Verifications successfully removed.' );
  185. } );
  186. db.run( 'DELETE FROM rcgcdw WHERE guild IN (' + guilds.map( guild => '?' ).join(', ') + ')', guilds, function (dberror) {
  187. if ( dberror ) {
  188. console.log( '- Error while removing the RcGcDw: ' + dberror );
  189. msg.replyMsg( 'I got an error while removing the RcGcDw!', {}, true );
  190. return dberror;
  191. }
  192. console.log( '- Verifications successfully removed.' );
  193. } );
  194. }
  195. if ( channels.length ) db.run( 'DELETE FROM discord WHERE channel IN (' + channels.map( channel => '?' ).join(', ') + ')', channels, function (dberror) {
  196. if ( dberror ) {
  197. console.log( '- Error while removing the channels: ' + dberror );
  198. msg.replyMsg( 'I got an error while removing the channels!', {}, true );
  199. return dberror;
  200. }
  201. console.log( '- Channels successfully removed.' );
  202. } );
  203. if ( !guilds.length && !channels.length ) console.log( '- Settings successfully removed.' );
  204. } );
  205. } );
  206. }
  207. catch ( tryerror ) {
  208. console.log( '- Error while removing the settings: ' + tryerror );
  209. return 'removeSettings(msg) – Error while removing the settings: ' + tryerror;
  210. }
  211. }
  212. module.exports = {
  213. name: 'eval',
  214. everyone: false,
  215. pause: false,
  216. owner: true,
  217. run: cmd_eval
  218. };