eval.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. }
  110. catch ( tryerror ) {
  111. console.log( '- Error while removing the patreon features: ' + tryerror );
  112. return 'removePatreons(guild, msg) – Error while removing the patreon features: ' + tryerror;
  113. }
  114. }
  115. /**
  116. * Removes the settings for deleted guilds and channels.
  117. * @param {Discord.Message} msg - The Discord message.
  118. */
  119. function removeSettings(msg) {
  120. if ( !msg ) return 'removeSettings(msg) – No message provided!';
  121. try {
  122. msg.client.shard.broadcastEval( `[[...this.guilds.cache.keys()], [...this.channels.cache.filter( channel => channel.type === 'text' ).keys()]]` ).then( results => {
  123. var all_guilds = results.map( result => result[0] ).reduce( (acc, val) => acc.concat(val), [] );
  124. var all_channels = results.map( result => result[1] ).reduce( (acc, val) => acc.concat(val), [] );
  125. var guilds = [];
  126. var channels = [];
  127. db.each( 'SELECT guild, channel FROM discord', [], (dberror, row) => {
  128. if ( dberror ) {
  129. console.log( '- Error while getting the setting: ' + dberror );
  130. return dberror;
  131. }
  132. if ( !row.channel && !all_guilds.includes(row.guild) ) {
  133. if ( row.guild in patreons ) msg.client.shard.broadcastEval( `delete global.patreons['${row.guild}']` );
  134. if ( row.guild in voice ) delete voice[row.guild];
  135. return guilds.push(row.guild);
  136. }
  137. if ( row.channel && all_guilds.includes(row.guild) && !all_channels.includes(row.channel) ) return channels.push(row.channel);
  138. }, (error) => {
  139. if ( error ) {
  140. console.log( '- Error while getting the settings: ' + error );
  141. msg.replyMsg( 'I got an error while getting the settings!', {}, true );
  142. return error;
  143. }
  144. if ( guilds.length ) {
  145. db.run( 'DELETE FROM discord WHERE guild IN (' + guilds.map( guild => '?' ).join(', ') + ')', guilds, function (dberror) {
  146. if ( dberror ) {
  147. console.log( '- Error while removing the guilds: ' + dberror );
  148. msg.replyMsg( 'I got an error while removing the guilds!', {}, true );
  149. return dberror;
  150. }
  151. console.log( '- Guilds successfully removed.' );
  152. } );
  153. db.run( 'DELETE FROM verification WHERE guild IN (' + guilds.map( guild => '?' ).join(', ') + ')', guilds, function (dberror) {
  154. if ( dberror ) {
  155. console.log( '- Error while removing the verifications: ' + dberror );
  156. msg.replyMsg( 'I got an error while removing the verifications!', {}, true );
  157. return dberror;
  158. }
  159. console.log( '- Verifications successfully removed.' );
  160. } );
  161. }
  162. if ( channels.length ) db.run( 'DELETE FROM discord WHERE channel IN (' + channels.map( channel => '?' ).join(', ') + ')', channels, function (dberror) {
  163. if ( dberror ) {
  164. console.log( '- Error while removing the channels: ' + dberror );
  165. msg.replyMsg( 'I got an error while removing the channels!', {}, true );
  166. return dberror;
  167. }
  168. console.log( '- Channels successfully removed.' );
  169. } );
  170. if ( !guilds.length && !channels.length ) console.log( '- Settings successfully removed.' );
  171. } );
  172. } );
  173. }
  174. catch ( tryerror ) {
  175. console.log( '- Error while removing the settings: ' + tryerror );
  176. return 'removeSettings(msg) – Error while removing the settings: ' + tryerror;
  177. }
  178. }
  179. module.exports = {
  180. name: 'eval',
  181. everyone: false,
  182. pause: false,
  183. owner: true,
  184. run: cmd_eval
  185. };