diff --git a/package.json b/package.json index 766bfe8..d67f41a 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "@discordjs/rest": "^1.1.0", "@types/node": "^18.7.14", "chalk": "^4.1.2", - "discord.js": "14.3.0", + "discord.js": "14.10.2", "dotenv": "^16.0.2", "mongoose": "^6.5.4" }, diff --git a/src/events/interactionCreate.ts b/src/events/interactionCreate.ts index eb10139..8bf7484 100644 --- a/src/events/interactionCreate.ts +++ b/src/events/interactionCreate.ts @@ -46,6 +46,18 @@ const event : BotEvent = { } catch (error) { console.error(error); } + } else if (interaction.isButton()) { + const command = interaction.client.slashCommands.get(interaction.customId); + if (!command) { + console.error(`No command matching ${interaction.customId} was found.`); + return; + } + try { + if(!command.button) return; + command.button(interaction); + } catch (error) { + console.error(error); + } } } } diff --git a/src/slashCommands/hobby.ts b/src/slashCommands/hobby.ts index ab2cf6d..d449c26 100644 --- a/src/slashCommands/hobby.ts +++ b/src/slashCommands/hobby.ts @@ -9,7 +9,7 @@ const command : SlashCommand = { execute: async (interaction) => { const modal = new ModalBuilder() .setCustomId("hobby") - .setTitle("What is your hobby?") + .setTitle("What is your hobby?"); const hobbiesInput = new TextInputBuilder() .setCustomId('hobbiesInput') diff --git a/src/slashCommands/whoami.ts b/src/slashCommands/whoami.ts new file mode 100644 index 0000000..94734d5 --- /dev/null +++ b/src/slashCommands/whoami.ts @@ -0,0 +1,62 @@ +import { SlashCommandBuilder, ActionRowBuilder, EmbedBuilder, ButtonBuilder, ButtonStyle, GuildMember, User } from "discord.js" +import { SlashCommand } from "../types"; + +const command : SlashCommand = { + command: new SlashCommandBuilder() + .setName("whoami") + .setDescription("Who am I?") + , + execute: async (interaction) => { + await interaction.deferReply({ ephemeral: true }); + + if (!interaction.channel) return + + const embed = new EmbedBuilder() + .setTitle("Who am I?") + .setFields( + { name: " ", value: "Click button below to know who you are!" } + ); + + const button = new ButtonBuilder() + .setCustomId("whoami") + .setLabel("Click Here") + .setStyle(ButtonStyle.Primary); + + const buttonRow = new ActionRowBuilder().addComponents(button); + + await interaction.channel.send({ + embeds: [embed], + components: [buttonRow] + }); + + await interaction.editReply({ content: "Your request has been sent!" }); + }, + button: async (interaction) => { + await interaction.deferReply({ ephemeral: true }); + + if (!interaction.channel || !interaction.guild) return + if (!interaction.user || !(interaction.user instanceof User)) return + if (!interaction.member || !(interaction.member instanceof GuildMember)) return + + const guild = interaction.guild; + const channel = interaction.channel; + const user = interaction.user; + const member = interaction.member; + + const embed = new EmbedBuilder() + .setAuthor({ name: user.username, iconURL: user.avatarURL() || undefined }) + .setTitle(`I am ${user.username}`) + .setFields( + { name: " ", value: `**username**: ${user.username}` }, + { name: " ", value: `**time joined Discord**: ${user.createdAt.toDateString()}` }, + { name: " ", value: `**time joined ${guild.name}**: ${member.joinedAt?.toDateString()}` } + ); + + await channel.send({ embeds: [embed] }); + + await interaction.editReply({ content: "Your request has been sent!" }); + }, + cooldown: 5 +} + +export default command \ No newline at end of file diff --git a/src/types.d.ts b/src/types.d.ts index afc87d1..2016d04 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,11 +1,12 @@ -import { SlashCommandBuilder, CommandInteraction, Collection, PermissionResolvable, Message, AutocompleteInteraction, ChatInputCommandInteraction } from "discord.js" +import { SlashCommandBuilder, CommandInteraction, Collection, PermissionResolvable, Message, AutocompleteInteraction, ChatInputCommandInteraction, ModalSubmitInteractio, ButtonInteraction } from "discord.js" import mongoose from "mongoose" export interface SlashCommand { command: SlashCommandBuilder, execute: (interaction : ChatInputCommandInteraction) => void, autocomplete?: (interaction: AutocompleteInteraction) => void, - modal?: (interaction: ModalSubmitInteraction) => void, + modal?: (interaction: ModalSubmitInteraction) => void, + button?: (interaction: ButtonInteraction) => void, cooldown?: number // in seconds }