From acf7f85c4c24e2517bb2c5aee65a210c42465530 Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Tue, 29 Aug 2023 20:25:52 +0530 Subject: [PATCH] feat: new messageInfo context menu --- src/Commands/Network/messageInfo.ts | 185 ++++++++++++++++++++++++++++ src/Commands/Network/serverInfo.ts | 62 ---------- src/Commands/Network/userInfo.ts | 45 ------- 3 files changed, 185 insertions(+), 107 deletions(-) create mode 100644 src/Commands/Network/messageInfo.ts delete mode 100644 src/Commands/Network/serverInfo.ts delete mode 100644 src/Commands/Network/userInfo.ts diff --git a/src/Commands/Network/messageInfo.ts b/src/Commands/Network/messageInfo.ts new file mode 100644 index 00000000..4b79a8b8 --- /dev/null +++ b/src/Commands/Network/messageInfo.ts @@ -0,0 +1,185 @@ +import { ActionRowBuilder, ApplicationCommandType, AttachmentBuilder, ButtonBuilder, ButtonStyle, ContextMenuCommandBuilder, EmbedBuilder, MessageContextMenuCommandInteraction } from 'discord.js'; +import { colors, getDb } from '../../Utils/functions/utils'; +import { stripIndents } from 'common-tags'; +import { profileImage } from 'discord-arts'; + +export default { + description: 'Get information about this message, user and server it was sent from!', + data: new ContextMenuCommandBuilder() + .setName('Message Info') + .setType(ApplicationCommandType.Message), + async execute(interaction: MessageContextMenuCommandInteraction) { + const db = getDb(); + const target = interaction.targetMessage; + const emotes = interaction.client.emotes.normal; + const networkMessage = await db.messageData.findFirst({ + where: { channelAndMessageIds: { some: { messageId: target.id } } }, + include: { hub: true }, + }); + + if (!networkMessage) { + await interaction.reply({ + content: 'This message has expired. If not, please wait a few seconds and try again.', + ephemeral: true, + }); + return; + } + + const author = await interaction.client.users.fetch(networkMessage.authorId); + const server = await interaction.client.guilds.fetch(networkMessage.serverId).catch(() => null); + + const embed = new EmbedBuilder() + .setThumbnail(author.displayAvatarURL()) + .setDescription(stripIndents` + ## ${emotes.clipart} Message Info + + **Sent By:** + __${author.username}__${author.discriminator !== '0' ? `#${author.discriminator}` : ''} ${author.bot ? '(Bot)' : ''} + + **Sent From:** + __${server?.name}__ + + **Message ID:** + __${target.id}__ + + **Sent In (Hub):** + __${networkMessage.hub?.name}__ + + **Sent At:** + + `) + .setColor('Random'); + + const buttons = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setCustomId('messageInfo') + .setLabel('Message Info') + .setStyle(ButtonStyle.Secondary) + .setDisabled(true), + new ButtonBuilder() + .setCustomId('serverInfo') + .setLabel('Server Info') + .setStyle(ButtonStyle.Primary), + new ButtonBuilder() + .setCustomId('userInfo') + .setLabel('User Info') + .setStyle(ButtonStyle.Primary), + ); + + const replyMsg = await interaction.reply({ + embeds: [embed], + components: [buttons], + ephemeral: true, + }); + + // create a variable to store the profile card buffer + let customCard: AttachmentBuilder | null = null; + + const collector = replyMsg?.createMessageComponentCollector({ time: 30_000 }); + collector.on('collect', async i => { + if (i.customId === 'serverInfo') { + if (!server) { + interaction.reply('Unable to find server!'); + return; + } + + const owner = await server.fetchOwner(); + const createdAt = Math.round(server.createdTimestamp / 1000); + const guildSetup = await db.connectedList.findFirst({ where: { serverId: networkMessage.serverId } }); + + const serverEmbed = new EmbedBuilder() + .setColor(colors('invisible')) + .setThumbnail(server.iconURL()) + .setImage(server.bannerURL()) + .setDescription(stripIndents` + ## Server Info + ${server.description || 'No Description.'} + + **Name:** + ${server?.name.substring(0, 256)} + + **Owner:** + __${owner.user.username}__${owner.user.discriminator !== '0' ? `#${owner.user.discriminator}` : ''} ${owner.user.bot ? '(Bot)' : ''} + + **Created:** + () + + **Member Count:** + __${server.memberCount}__ + + **Invite:** + __${guildSetup?.invite ? `[\`${guildSetup.invite}\`](https://discord.gg/${guildSetup.invite})` : 'Not Set.'}__`) + .setFooter({ text: `ID: ${server.id}` }); + + const newButtons = ActionRowBuilder.from(buttons); + newButtons.components[0].setDisabled(false).setStyle(ButtonStyle.Primary); + newButtons.components[1].setDisabled(true).setStyle(ButtonStyle.Secondary); + + if (guildSetup?.invite) { + newButtons.addComponents(new ButtonBuilder() + .setStyle(ButtonStyle.Link) + .setURL(`https://discord.gg/${guildSetup?.invite}`) + .setEmoji(interaction.client.emotes.icons.join) + .setLabel('Join')); + } + + await i.update({ embeds: [serverEmbed], components: [newButtons], files: [] }); + } + + + else if (i.customId === 'userInfo') { + await i.deferUpdate(); + + const user = await interaction.client.users.fetch(networkMessage.authorId, { force: true }).catch(() => null); + if (!user) { + interaction.reply(`${emotes.no} Unable to fetch user details.`); + return; + } + + const createdAt = Math.round(user.createdTimestamp / 1000); + + const userEmbed = new EmbedBuilder() + .setThumbnail(user.displayAvatarURL()) + .setColor('Random') + .setImage(user.bannerURL() ?? null) + .setDescription(stripIndents` + ## User Info + + **User Name:** + __${user.username}__${user.discriminator !== '0' ? `#${user.discriminator}` : ''} ${user.bot ? '(Bot)' : ''} + + **ID:** + __${user.id}__ + + **Created:** + () + + **Display Name:** + __${user.globalName || 'Not Set.'}__ + + **Hubs Owned:** + __${await db.hubs.count({ where: { ownerId: user.id } })}__ + `) + .setImage('attachment://customCard.png') // link to image that will be generated afterwards + .setTimestamp(); + + // disable the user info button + const newButtons = ActionRowBuilder.from(buttons); + newButtons.components[0].setDisabled(false).setStyle(ButtonStyle.Primary); + newButtons.components[2].setDisabled(true).setStyle(ButtonStyle.Secondary); + + // generate the profile card + if (!customCard) customCard = new AttachmentBuilder(await profileImage(user.id), { name: 'customCard.png' }); + + await interaction.editReply({ + embeds: [userEmbed], + files: [customCard], + components: [newButtons], + }); + } + else if (i.customId === 'messageInfo') { + await i.update({ embeds: [embed], components: [buttons], files: [] }); + } + }); + }, +}; \ No newline at end of file diff --git a/src/Commands/Network/serverInfo.ts b/src/Commands/Network/serverInfo.ts deleted file mode 100644 index 6a8667f1..00000000 --- a/src/Commands/Network/serverInfo.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { - ActionRowBuilder, - ApplicationCommandType, - ButtonBuilder, - ButtonStyle, - ContextMenuCommandBuilder, - EmbedBuilder, - MessageContextMenuCommandInteraction, -} from 'discord.js'; -import { colors, getDb } from '../../Utils/functions/utils'; -import { stripIndent } from 'common-tags'; - -export default { - description: 'Get information about a server in the network!', - data: new ContextMenuCommandBuilder() - .setName('Server Info') - .setType(ApplicationCommandType.Message), - async execute(interaction: MessageContextMenuCommandInteraction) { - const target = interaction.targetId; - - const db = getDb(); - const messageInDb = await db.messageData.findFirst({ - where: { channelAndMessageIds: { some: { messageId: target } } }, - }); - - if (!messageInDb) return interaction.reply({ content: 'This message has expired! Please try another message.', ephemeral: true }); - const server = await interaction.client.guilds.fetch(messageInDb.serverId).catch(() => null); - if (!server) return interaction.reply('Unable to find server!'); - - const owner = await server.fetchOwner(); - const createdAt = Math.round(server.createdTimestamp / 1000); - const guildSetup = await db.connectedList.findFirst({ where: { serverId: messageInDb.serverId } }); - - const embed = new EmbedBuilder() - .setTitle(server?.name.substring(0, 256)) - .setColor(colors('invisible')) - .setThumbnail(server.iconURL()) - .setImage(server.bannerURL()) - .setDescription(stripIndent` - ${server.description || 'No Description.'} - - **Owner:** @${owner.user.username} - **Created:** () - **Members:** ${server.memberCount} - **Invite:** ${guildSetup?.invite ? `[\`${guildSetup.invite}\`](https://discord.gg/${guildSetup.invite})` : 'Not Set.'}`) - .setFooter({ text: `ID: ${server.id}` }); - - let buttons; - if (guildSetup?.invite) { - buttons = new ActionRowBuilder().addComponents(new ButtonBuilder() - .setStyle(ButtonStyle.Link) - .setURL(`https://discord.gg/${guildSetup?.invite}`) - .setEmoji(interaction.client.emotes.icons.join) - .setLabel('Join')); - } - await interaction.reply({ - embeds: [embed], - components: buttons ? [buttons] : undefined, - ephemeral: true, - }); - }, -}; diff --git a/src/Commands/Network/userInfo.ts b/src/Commands/Network/userInfo.ts deleted file mode 100644 index 38f44c9e..00000000 --- a/src/Commands/Network/userInfo.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { ApplicationCommandType, AttachmentBuilder, ContextMenuCommandBuilder, EmbedBuilder, MessageContextMenuCommandInteraction } from 'discord.js'; -import { badgeToEmoji, colors, getDb } from '../../Utils/functions/utils'; -import { profileImage } from 'discord-arts'; - -export default { - description: 'Get information about a user in the network!', - data: new ContextMenuCommandBuilder() - .setName('User Info') - .setType(ApplicationCommandType.Message), - async execute(interaction: MessageContextMenuCommandInteraction) { - const db = getDb(); - const target = interaction.targetId; - const emoji = interaction.client.emotes.normal; - const messageInDb = await db.messageData.findFirst({ - where: { channelAndMessageIds: { some: { messageId: target } } }, - }); - - if (!messageInDb) return interaction.reply({ content: 'This message has expired! Please try another message.', ephemeral: true }); - const user = await interaction.client.users.fetch(messageInDb.authorId, { force: true }).catch(() => null); - if (!user) return interaction.reply(`${emoji.no} Unable to fetch user details.`); - - const userBadgesRaw = await db.userBadges.findFirst({ where: { userId: user.id } }); - const createdAt = Math.round(user.createdTimestamp / 1000); - - const userBadges = userBadgesRaw?.badges ? badgeToEmoji(userBadgesRaw?.badges).join(' ') : 'No Badges.'; - - - const embed = new EmbedBuilder() - .setAuthor({ name: user.username, iconURL: user.avatarURL() ?? user.defaultAvatarURL }) - .setDescription(userBadges) - .addFields({ - name: 'General Info', - value: `**Name:** ${user.username}\n**Bot Account:** ${user.bot}\n**Created:** ()`, - }) - .setImage('attachment://customCard.png') // link to image that will be generated afterwards - .setFooter({ text:`ID: ${user.id}` }) - .setColor(colors('invisible')); - - await interaction.reply({ content: `${emoji.loading} Generating profile card...`, embeds: [embed], ephemeral: true }); - - // generate the profile card - const customCard = new AttachmentBuilder(await profileImage(user.id), { name: 'customCard.png' }); - await interaction.editReply({ content: '', files: [customCard] }); - }, -};