Cannot read properties of undefined (reading 'Flags')

My Code:
const { SlashCommandBuilder } = require("@discordjs/builders")
const { EmbedBuilder, PermissionsBitFieldBitField } = require("discord.js")

module.exports = {
data: new SlashCommandBuilder()
.setName("kick")
.setDescription("Kicke einen User vom Server!")
.addUserOption(option => option.setName("user").setDescription("Der entsprechende User!").setRequired(true))
.addStringOption(option => option.setName("reason").setDescription("Begründung für den Kick!")),
async execute (interaction, client )
{
const user = interaction.options.getUser("user")
const kickMember = await interaction.guild.members.fetch(user.id)
const channel = interaction.channel

if (!interaction.member.permissions.has(PermissionsBitFieldBitField.Flags.kickMember)) return await interaction.reply({ content: "Du hast nicht die Berechtigung dazu, Member zu kicken!"})
if (!kickMember) return await interaction.reply({ content: "User nicht gefunden!"})
if (!kickMember.kickable) return await interaction.reply({ content: "Ich kann diesen User nicht kicken!"})

let reason = interaction.options.getString("reason")

if (!reason) reason = "No reason given!"

const dmEmbed = new EmbedBuilder()
.setColor("Blue")
.setDescription(`Du wurdest von **${interaction.guild.name} gekickt! | ${reason}`)

const embed = new EmbedBuilder()
.setColor("Blue")
.setDescription(`${user.tag} has been kicked! | ${reason}`)

await kickMember.send({ embeds: [dmEmbed]}).catch(err => {
return
})

await kickMember.kick({ reason: reason}).catch(err => {
interaction.reply({ content: "There was an error"})
})

await interaction.reply({ embeds: [embed]})

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

module.exports = {
data: new SlashCommandBuilder()
.setName("kick")
.setDescription("Kicke einen User vom Server!")
.addUserOption(option => option.setName("user").setDescription("Der entsprechende User!").setRequired(true))
.addStringOption(option => option.setName("reason").setDescription("Begründung für den Kick!")),
async execute (interaction, client )
{
const user = interaction.options.getUser("user")
const kickMember = await interaction.guild.members.fetch(user.id)
const channel = interaction.channel

if (!interaction.member.permissions.has(PermissionsBitFieldBitField.Flags.kickMember)) return await interaction.reply({ content: "Du hast nicht die Berechtigung dazu, Member zu kicken!"})
if (!kickMember) return await interaction.reply({ content: "User nicht gefunden!"})
if (!kickMember.kickable) return await interaction.reply({ content: "Ich kann diesen User nicht kicken!"})

let reason = interaction.options.getString("reason")

if (!reason) reason = "No reason given!"

const dmEmbed = new EmbedBuilder()
.setColor("Blue")
.setDescription(`Du wurdest von **${interaction.guild.name} gekickt! | ${reason}`)

const embed = new EmbedBuilder()
.setColor("Blue")
.setDescription(`${user.tag} has been kicked! | ${reason}`)

await kickMember.send({ embeds: [dmEmbed]}).catch(err => {
return
})

await kickMember.kick({ reason: reason}).catch(err => {
interaction.reply({ content: "There was an error"})
})

await interaction.reply({ embeds: [embed]})

}
}
Error: TypeError: Cannot read properties of undefined (reading 'Flags') What´s wrong?
14 Replies
d.js toolkit
d.js toolkit17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers.
duck
duck17mo ago
PermissionsBitFieldBitField
that's one too many BitFields
Serial Designation N
Ok but now it says DiscordAPIError[10062]: Unknown interaction
d.js docs
d.js docs17mo ago
Common causes of DiscordAPIError[10062]: Unknown interaction: - Initial response took more than 3 seconds ➞ defer the response *. - Wrong interaction object inside a collector. * Note: you cannot defer modal or autocomplete value responses
Serial Designation N
Can i use 0x0000000000000002 instead of PermissionsBitField.Flags.kickMember in
if (!interaction.member.permissions.has(PermissionsBitField.Flags.kickMember))
if (!interaction.member.permissions.has(PermissionsBitField.Flags.kickMember))
or how can i use the integer value?
duck
duck17mo ago
1. it's PascalCase, so it'd be KickMember with a capital K 2. permission flags are bigints 3. why would you prefer to make your code less readable? 4. you know that 0x0000000000000002 is just 2 right?
Serial Designation N
1. Ok 2. ok 3. idk. i just want to know if it's possible 4. yeah...
duck
duck17mo ago
well then the answer is you can, but it's recommended to use the enums so you can read your code
Serial Designation N
Ok, but why comes this error when i try to kick someone without having kick perms? RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined.
duck
duck17mo ago
oh right it should be KickMembers plural
Serial Designation N
this makes no difference?
const KickMember = await interaction.guild.members.fetch(user.id)
const KickMember = await interaction.guild.members.fetch(user.id)
duck
duck17mo ago
no I mean PermissionsBitField.Flags.KickMembers your variables can be named whatever you want
Serial Designation N
Oh bruh. You are right. XD it works now XDDDD Thanks again. XDD
duck
duck17mo ago
👍
Want results from more Discord servers?
Add your server