eval.js 8.9 KB

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