Timeout Error

Hello! After this message you can see my current Code. I have a problem, that firstly, i get the error right here:
if (timeMember.permissions.has(PermissionsBitField.Flags.Administrator)) return await interaction.reply({ content: `Du kannst keinen User mit Administrator Rechten timeouten!`, ephermeral: true })

TypeError: Cannot read properties of undefined (reading 'has')
if (timeMember.permissions.has(PermissionsBitField.Flags.Administrator)) return await interaction.reply({ content: `Du kannst keinen User mit Administrator Rechten timeouten!`, ephermeral: true })

TypeError: Cannot read properties of undefined (reading 'has')
And secondly, if i just remove this, i get the error, from the
if (!timeMember.kickable) return await interaction.reply({....
if (!timeMember.kickable) return await interaction.reply({....
but the user i test it on is not above the bot at all.
7 Replies
d.js toolkit
d.js toolkit15mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Knabbiii ♡ ˚₊‧⁺˖
const { SlashCommandBuilder } = require('@discordjs/builders');
const { Permissions, MessageActionRow, MessageButton, PermissionFlagsBits, PermissionsBitField } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('timeout')
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers)
.setDescription('Timeoute einen User.')
.addUserOption(option =>
option.setName('user')
.setDescription('User, der getimeoutet wird.')
.setRequired(true))
.addStringOption(option =>
option.setName('duration')
.setDescription('Dauer des Timeoutes (z.B. 30m, 2h, 3d)')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('Grund des Timeoutes.')
.setRequired(false))
.addAttachmentOption(option =>
option.setName('proof')
.setDescription('Beweisbild (Screenshot) hinzufügen.')
.setRequired(false))
.addBooleanOption(option =>
option.setName('dm')
.setDescription('User wird per dm benachrichtigt.')
.setRequired(false)),

async execute(interaction, client) {
const durationInput = interaction.options.getString('duration');
const reason = interaction.options.getString('reason') || 'Kein Grund angegeben.';
const dm = interaction.options.getBoolean('dm');
const timeUser = interaction.options.getUser('user');
const timeMember = interaction.guild.members.fetch(timeUser.id);
const proof = interaction.options.getAttachment('proof');

// Duration
function calculateDurationInMinutes(input) {
const regex = /(\d+)([mhd])/;
const match = input.match(regex);

if (!match) return null;

const value = parseInt(match[1]);
const unit = match[2];

switch (unit) {
case 'm':
return value;
case 'h':
return value * 60;
case 'd':
return value * 60 * 24;
default:
return null;
}
}

const durationInMinutes = calculateDurationInMinutes(durationInput);

if (!durationInMinutes) {
await interaction.reply('Ungültiges Format für die Dauer. Verwende z.B. "30m" für Minuten, "2h" für Stunden oder "3d" für Tage.');
return;
}

// Logical Statements
if (!interaction.member.permissions.has(PermissionsBitField.Flags.ModerateMembers)) return await interaction.reply({ content: `Du brauchst die "Moderate Member" Permission um zu timeouten.`, ephermeral: true })
if (!timeMember) return await interaction.reply({ content: `Der User ist nicht auf dem Server`, ephermeral: true });
if (!timeMember.kickable) return await interaction.reply({ content: `Ich kann diesen User nicht timeouten. Das kann daran liegen, dass der User oder die Rolle des Users über meiner ist.`, ephermeral: true });
if (interaction.member.id === timeMember.id) return await interaction.reply({ content: 'Du kannst dich nicht selbst timeouten!', ephermeral: true });
if (timeMember.permissions.has(PermissionsBitField.Flags.Administrator)) return await interaction.reply({ content: `Du kannst keinen User mit Administrator Rechten timeouten!`, ephermeral: true })

// Timeout member
await timeMember.timeout(durationInMinutes * 60 * 1000, reason)

await interaction.reply('Timeout done.')

}
}
const { SlashCommandBuilder } = require('@discordjs/builders');
const { Permissions, MessageActionRow, MessageButton, PermissionFlagsBits, PermissionsBitField } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('timeout')
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers)
.setDescription('Timeoute einen User.')
.addUserOption(option =>
option.setName('user')
.setDescription('User, der getimeoutet wird.')
.setRequired(true))
.addStringOption(option =>
option.setName('duration')
.setDescription('Dauer des Timeoutes (z.B. 30m, 2h, 3d)')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('Grund des Timeoutes.')
.setRequired(false))
.addAttachmentOption(option =>
option.setName('proof')
.setDescription('Beweisbild (Screenshot) hinzufügen.')
.setRequired(false))
.addBooleanOption(option =>
option.setName('dm')
.setDescription('User wird per dm benachrichtigt.')
.setRequired(false)),

async execute(interaction, client) {
const durationInput = interaction.options.getString('duration');
const reason = interaction.options.getString('reason') || 'Kein Grund angegeben.';
const dm = interaction.options.getBoolean('dm');
const timeUser = interaction.options.getUser('user');
const timeMember = interaction.guild.members.fetch(timeUser.id);
const proof = interaction.options.getAttachment('proof');

// Duration
function calculateDurationInMinutes(input) {
const regex = /(\d+)([mhd])/;
const match = input.match(regex);

if (!match) return null;

const value = parseInt(match[1]);
const unit = match[2];

switch (unit) {
case 'm':
return value;
case 'h':
return value * 60;
case 'd':
return value * 60 * 24;
default:
return null;
}
}

const durationInMinutes = calculateDurationInMinutes(durationInput);

if (!durationInMinutes) {
await interaction.reply('Ungültiges Format für die Dauer. Verwende z.B. "30m" für Minuten, "2h" für Stunden oder "3d" für Tage.');
return;
}

// Logical Statements
if (!interaction.member.permissions.has(PermissionsBitField.Flags.ModerateMembers)) return await interaction.reply({ content: `Du brauchst die "Moderate Member" Permission um zu timeouten.`, ephermeral: true })
if (!timeMember) return await interaction.reply({ content: `Der User ist nicht auf dem Server`, ephermeral: true });
if (!timeMember.kickable) return await interaction.reply({ content: `Ich kann diesen User nicht timeouten. Das kann daran liegen, dass der User oder die Rolle des Users über meiner ist.`, ephermeral: true });
if (interaction.member.id === timeMember.id) return await interaction.reply({ content: 'Du kannst dich nicht selbst timeouten!', ephermeral: true });
if (timeMember.permissions.has(PermissionsBitField.Flags.Administrator)) return await interaction.reply({ content: `Du kannst keinen User mit Administrator Rechten timeouten!`, ephermeral: true })

// Timeout member
await timeMember.timeout(durationInMinutes * 60 * 1000, reason)

await interaction.reply('Timeout done.')

}
}
ShompiFlen
ShompiFlen15mo ago
You are getting a user object from your slash command options, you have to get a MEMBER object to access their permissions, roles, and other methods like kick or timeout, etc
d.js docs
d.js docs15mo ago
Despite sounding similar there is a distinct difference between users and members in Discord: - User: global Discord user data (global avatar, username, tag, id) - GuildMember: user data associated to a guild (guild, nickname, roles, voice, guild avatar, etc.) - Conversion: User ➞ GuildMember | GuildMember ➞ User * Note: Events received in cached guilds will often have both the member and user available, eg. interaction.user and interaction.member
ShompiFlen
ShompiFlen15mo ago
instead of options.getUser() use options.getMember() and you should also check if the command is being used in a guild and not a dm or register this command with .setDMPermission(false) to not register it on DMS
Knabbiii ♡ ˚₊‧⁺˖
thank you, now i get the error from:
if (!timeMember.kickable) return await interaction.reply
if (!timeMember.kickable) return await interaction.reply
and the user is not above me i did that, seems like the error is gone, but now i got the new one, like i said
ShompiFlen
ShompiFlen15mo ago
the error is?
Want results from more Discord servers?
Add your server