get.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. const {MessageEmbed, Util, ShardClientUtil: {shardIdForGuildId}, Permissions: {FLAGS}} = require('discord.js');
  2. const {defaultSettings, defaultPermissions} = require('../util/default.json');
  3. const {escapeFormatting} = require('../util/functions.js');
  4. var db = require('../util/database.js');
  5. /**
  6. * Processes the "get" command.
  7. * @param {import('../util/i18n.js')} lang - The user language.
  8. * @param {import('discord.js').Message} msg - The Discord message.
  9. * @param {String[]} args - The command arguments.
  10. * @param {String} line - The command as plain text.
  11. * @param {import('../util/wiki.js')} wiki - The wiki for the message.
  12. * @async
  13. */
  14. async function cmd_get(lang, msg, args, line, wiki) {
  15. var id = args.join().replace( /^\\?<(?:@!?|#)(\d+)>$/, '$1' );
  16. if ( !/^\d+$/.test(id) ) {
  17. if ( !msg.channel.isGuild() || !pause[msg.guildId] ) this.LINK(lang, msg, line, wiki);
  18. return;
  19. }
  20. try {
  21. var guild = await msg.client.shard.broadcastEval( (discordClient, evalData) => {
  22. if ( discordClient.guilds.cache.has(evalData.id) ) {
  23. var guild = discordClient.guilds.cache.get(evalData.id);
  24. return {
  25. name: guild.name, id: guild.id, memberCount: guild.memberCount,
  26. ownerId: guild.ownerId, owner: discordClient.users.cache.get(guild.ownerId)?.tag,
  27. channel: guild.publicUpdatesChannelId, icon: guild.iconURL({dynamic:true}),
  28. permissions: guild.me.permissions.missing(evalData.defaultPermissions),
  29. pause: global.pause.hasOwnProperty(guild.id), voice: global.voice.hasOwnProperty(guild.id),
  30. shardId: process.env.SHARDS
  31. };
  32. }
  33. }, {
  34. context: {id, defaultPermissions},
  35. shard: shardIdForGuildId(id, msg.client.shard.count)
  36. } );
  37. if ( guild ) {
  38. var guildname = ['Guild:', escapeFormatting(guild.name) + ' `' + guild.id + '`' + ( guild.pause ? '\\*' : '' )];
  39. var guildowner = ['Owner:', ( guild.owner ? escapeFormatting(guild.owner) + ' ' : '' ) + '`' + guild.ownerId + '` <@' + guild.ownerId + '>'];
  40. var guildsize = ['Size:', guild.memberCount + ' members'];
  41. var guildshard = ['Shard:', guild.shardId];
  42. var guildpermissions = ['Missing permissions:', ( guild.permissions.length ? '`' + guild.permissions.join('`, `') + '`' : '*none*' )];
  43. var guildchannel = ['Updates channel:', '`' + guild.channel + '`'];
  44. var guildsettings = ['Settings:', '*unknown*'];
  45. return db.query( 'SELECT channel, wiki, lang, role, inline, prefix FROM discord WHERE guild = $1 ORDER BY channel ASC NULLS FIRST', [guild.id] ).then( ({rows}) => {
  46. if ( rows.length ) {
  47. let row = rows.find( row => !row.channel );
  48. row.patreon = patreons.hasOwnProperty(guild.id);
  49. row.voice = guild.voice;
  50. guildsettings[1] = '```json\n' + JSON.stringify( rows, null, '\t' ) + '\n```';
  51. }
  52. else guildsettings[1] = '*default*';
  53. }, dberror => {
  54. console.log( '- Error while getting the settings: ' + dberror );
  55. } ).then( () => {
  56. if ( msg.showEmbed() ) {
  57. var embed = new MessageEmbed().setThumbnail( guild.icon ).addField( guildname[0], guildname[1] ).addField( guildowner[0], guildowner[1] ).addField( guildsize[0], guildsize[1], true ).addField( guildshard[0], guildshard[1], true ).addField( guildpermissions[0], guildpermissions[1] );
  58. if ( guild.channel ) embed.addField( guildchannel[0], guildchannel[1] );
  59. var split = Util.splitMessage( guildsettings[1], {char:',\n',maxLength:1000,prepend:'```json\n',append:',\n```'} );
  60. if ( split.length > 5 ) {
  61. msg.sendChannel( {embeds: [embed]}, true );
  62. Util.splitMessage( guildsettings.join(' '), {
  63. char: ',\n',
  64. maxLength: 2000,
  65. prepend: '```json\n',
  66. append: ',\n```'
  67. } ).forEach( textpart => msg.sendChannel( textpart, true ) );
  68. }
  69. else {
  70. split.forEach( textpart => embed.addField( guildsettings[0], textpart ) );
  71. msg.sendChannel( {embeds: [embed]}, true );
  72. }
  73. }
  74. else {
  75. var text = guildname.join(' ') + '\n' + guildowner.join(' ') + '\n' + guildsize.join(' ') + '\n' + guildshard.join(' ') + '\n' + guildpermissions.join(' ') + ( guild.channel ? '\n' + guildchannel.join(' ') : '' ) + '\n' + guildsettings.join(' ');
  76. Util.splitMessage( text, {
  77. char: ',\n',
  78. maxLength: 2000,
  79. prepend: '```json\n',
  80. append: ',\n```'
  81. } ).forEach( textpart => msg.sendChannel( textpart, true ) );
  82. }
  83. } );
  84. }
  85. var channel = await msg.client.shard.broadcastEval( (discordClient, evalData) => {
  86. if ( discordClient.channels.cache.filter( channel => channel.isGuild() || channel.type === 'GUILD_CATEGORY' ).has(evalData.id) ) {
  87. var channel = discordClient.channels.cache.get(evalData.id);
  88. return {
  89. name: channel.name, id: channel.id, type: channel.type, parentId: channel.parentId,
  90. isThread: channel.isThread(), threadParentId: channel.parent?.parentId,
  91. guild: channel.guild.name, guildId: channel.guildId,
  92. permissions: channel.guild.me.permissionsIn(channel.id).missing(evalData.defaultPermissions),
  93. pause: global.pause.hasOwnProperty(channel.guildId),
  94. shardId: process.env.SHARDS
  95. };
  96. }
  97. }, {context: {id, defaultPermissions}} ).then( results => results.find( result => result ) );
  98. if ( channel ) {
  99. var channelguild = ['Guild:', escapeFormatting(channel.guild) + ' `' + channel.guildId + '`' + ( channel.pause ? '\\*' : '' )];
  100. var channelname = ['Channel:', '#' + escapeFormatting(channel.name) + ' `' + channel.id + '` <#' + channel.id + '>'];
  101. var channeldetails = ['Details:', '`' + channel.type + '`' + ( channel.parentId ? ' – `' + channel.parentId + '` <#' + channel.parentId + '>' + ( channel.isThread ? ' – `' + channel.threadParentId + '` <#' + channel.threadParentId + '>' : '' ) : '' )];
  102. var channelpermissions = ['Missing permissions:', ( channel.permissions.length ? '`' + channel.permissions.join('`, `') + '`' : '*none*' )];
  103. var channellang = ['Language:', '*unknown*'];
  104. var channelwiki = ['Default Wiki:', '*unknown*'];
  105. var channelrole = ['Minimal Role:', '*unknown*'];
  106. var channelinline = ['Inline commands:', '*unknown*'];
  107. let sqlargs = [channel.guildId];
  108. if ( channel.isThread ) sqlargs.push(channel.parentId, '#' + channel.threadParentId);
  109. else sqlargs.push(channel.id, '#' + ( channel.type === 'GUILD_CATEGORY' ? channel.id : channel.parentId ));
  110. return db.query( 'SELECT wiki, lang, role, inline FROM discord WHERE guild = $1 AND (channel = $2 OR channel = $3 OR channel IS NULL) ORDER BY channel DESC NULLS LAST LIMIT 1', sqlargs ).then( ({rows:[row]}) => {
  111. if ( row ) {
  112. channellang[1] = row.lang;
  113. channelwiki[1] = row.wiki;
  114. channelrole[1] = ( row.role ? '`' + row.role + '` <@&' + row.role + '>' : '@everyone' );
  115. channelinline[1] = ( row.inline ? 'disabled' : 'enabled' );
  116. }
  117. else {
  118. channellang[1] = defaultSettings.lang;
  119. channelwiki[1] = defaultSettings.wiki;
  120. channelrole[1] = '@everyone';
  121. channelinline[1] = 'enabled';
  122. }
  123. }, dberror => {
  124. console.log( '- Error while getting the settings: ' + dberror );
  125. } ).then( () => {
  126. var text = null;
  127. var embed = null;
  128. if ( msg.showEmbed() ) {
  129. embed = new MessageEmbed().addField( channelguild[0], channelguild[1] ).addField( channelname[0], channelname[1] ).addField( channeldetails[0], channeldetails[1] ).addField( channelpermissions[0], channelpermissions[1] ).addField( channellang[0], channellang[1] ).addField( channelwiki[0], channelwiki[1] ).addField( channelrole[0], channelrole[1] ).addField( channelinline[0], channelinline[1] );
  130. }
  131. else {
  132. text = channelguild.join(' ') + '\n' + channelname.join(' ') + '\n' + channeldetails.join(' ') + '\n' + channelpermissions.join(' ') + '\n' + channellang.join(' ') + '\n' + channelwiki[0] + ' <' + channelwiki[1] + '>\n' + channelrole.join(' ') + '\n' + channelinline.join(' ');
  133. }
  134. msg.sendChannel( {content: text, embeds: [embed]}, true );
  135. } );
  136. }
  137. var user = await msg.client.users.fetch(id, false).catch( () => {} );
  138. if ( user ) {
  139. var username = ['User:', escapeFormatting(user.tag) + ' `' + user.id + '` <@' + user.id + '>'];
  140. var guildlist = ['Guilds:', '*none*'];
  141. var guilds = await msg.client.shard.broadcastEval( (discordClient, evalData) => {
  142. return discordClient.guilds.cache.filter( guild => guild.members.cache.has(evalData.user) ).map( guild => {
  143. var member = guild.members.cache.get(evalData.user);
  144. return {
  145. name: guild.name,
  146. id: guild.id,
  147. isAdmin: member.permissions.has(evalData.MANAGE_GUILD),
  148. shardId: process.env.SHARDS
  149. }
  150. } );
  151. }, {context: {user: user.id, MANAGE_GUILD: FLAGS.MANAGE_GUILD.toString()}} ).then( results => {
  152. return results.reduce( (acc, val) => acc.concat(val), [] ).map( user_guild => {
  153. return escapeFormatting(user_guild.name) + ' `' + user_guild.id + '`' + ( user_guild.isAdmin ? '\\*' : '' );
  154. } );
  155. } );
  156. if ( guilds.length ) guildlist[1] = guilds.join('\n');
  157. if ( guildlist[1].length > 1000 ) guildlist[1] = guilds.length.toLocaleString();
  158. var text = null;
  159. var embed = null;
  160. if ( msg.showEmbed() ) embed = new MessageEmbed().setThumbnail( user.displayAvatarURL({dynamic:true}) ).addField( username[0], username[1] ).addField( guildlist[0], guildlist[1] );
  161. else text = username.join(' ') + '\n' + guildlist.join('\n');
  162. return msg.sendChannel( {content: text, embeds: [embed]}, true );
  163. }
  164. msg.replyMsg( 'I couldn\'t find a result for `' + id + '`', true );
  165. } catch ( error ) {
  166. log_error(error);
  167. msg.reactEmoji('error');
  168. }
  169. }
  170. module.exports = {
  171. name: 'get',
  172. everyone: false,
  173. pause: false,
  174. owner: true,
  175. run: cmd_get
  176. };