eval.js 8.7 KB

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