skip to Main Content

I wanted to try some things out with a discord bot. My idea was to get informations about the user who is selected in the /-command on discord.
But I do always get the same error and I couldn’t find a solution.
`TypeError: Cannot read properties of undefined (reading ‘getMember’)

Does someone have an idea? I tried many things but neither of them worked.

index.json

require("dotenv").config()
const fs = require("fs")
const { Client, Collection, GatewayIntentBits, ActivityType } = require("discord.js")

const client = new Client({intents: [GatewayIntentBits.Guilds]})
client.commands = new Collection()

const commandFiles = fs.readdirSync("./src/commands").filter(file => file.endsWith(".js"))


commandFiles.forEach(commandFile => {
    const command = require(`./commands/${commandFile}`)
    client.commands.set(command.data.name, command)
})

client.once("ready", () => {
    console.log(`Starting... Logged in as ${client.user.tag}! I'm on ${client.guilds.cache.size} server(s)!`)
    client.user.setActivity({name: "Highschool DXD", type: ActivityType.Watching})
})




client.on("interactionCreate", async (interaction) => {
    if(!interaction.isCommand()) return

    const command = client.commands.get(interaction.commandName)

    if(command) {

        try {
            await command.execute(interaction)
        } catch(error) {
            console.error(error)

            if(interaction.deferred || interaction.replied) {
                interaction.editReply("Error, Fehler beim Command ausführen")
            }else {
                interaction.reply("Error, Fehler beim Command ausführen")
            }
        }
    }
})

client.login(process.env.DISCORD_BOT_TOKEN)

info.js

const { SlashCommandBuilder } = require("@discordjs/builders")
const { EmbedBuilder } = require("discord.js")


module.exports = {
    data: new SlashCommandBuilder()
    .setName("info")
    .setDescription("Server/user Informationen")
    .addSubcommand(subCommand=> subCommand.setName("server").setDescription("Serverinfos"))
    .addSubcommand(subCommand=> subCommand.setName("member").setDescription("Memberinfos")
    .addUserOption(option=> option.setName("member").setDescription("Der Member").setRequired(true))),
    async execute(interaction) {
        switch(interaction.options.getSubcommand()) {
            case "server": {
                interaction.reply({embeds: [
                    new EmbedBuilder()
                    .setTitle(`Infos über den Server ${interaction.guild.name}`)
                    .addFields([
                        {
                            name: "Channels",
                            value: `${interaction.guild.channels.cache.size} Channels`
                        },
                        {
                            name: "Erstellt",
                            value: `<t:${Math.round(interaction.guild.createdTimestamp/1000)}>`,
                            inline:true
                        }
                    ])
                ]})

                break
            }

            case "member": {
                const member = interaction.option.getMember("member")
                interaction.reply({embeds: [
                    new EmbedBuilder()
                    .setTitle(`Infos über ${member.user.tag}`)
                    .setThumbnail(member.user.avaterURL({dynamic: true})) 
                    .addFields([
                        {
                            name: "Account erstellt",
                            value: `<t:${Math.round(member.user.createdTimestamp/1000)}>`,
                        },
                        {
                            name: "Joined Server",
                            value: `<t:${Math.round(member.joinedTimestamp/1000)}>`,
                            inline:true
                        }
                    ])
                ]})

                break
            }
        }

    }
}

I hope y’all can understand it as some parts are in german but they should all be just outputs from the bot iself on discord

I’ve searched in the internet but I couldn’t really find a solution for my exact problem or maybe I’m just stupid haha

2

Answers


  1. The error message is saying that you are trying to access getMember on undefined. It means that interaction.option (without an S) is undefined.
    A few lines above, you are doing switch(interaction.options.getSubcommand()) (with an S)

    option should be replaced by options on this line:

    const member = interaction.options.getMember("member")
    
    Login or Signup to reply.
  2. Yes, it´s a typing error in

    interaction.options.getMember("member")
    

    https://discordjs.guide/slash-commands/parsing-options.html#subcommands

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search