Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord.js member.timeout is not a function

I try to make a /timeout discord command and therefore need a GuildMember instead of a User, but discord only allows Users to be an option in a slash command so I can’t directly ask for a member. So I have the user and use guild.members.fetch({target, force: true}) to fetch him from the guild members. Afterwards I check if member != null to make sure the member exists (= is in the guild).
At the end I call member.timeout(durationInMs, `${reason} | Von ${interaction.user.username}`); to timeout the member, but I get TypeError: member.timeout is not a function.
Full code:

const { SlashCommandBuilder, EmbedBuilder, Colors, PermissionFlagsBits } = require('discord.js');
const ms = require('ms');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('timeout')
        .setDescription('Versetze einen Nutzer für eine bestimmte Zeit in den Timeout')
        .addUserOption(option =>
            option.setName('target')
                .setDescription('Der Nutzer, den du timeouten möchtest')
                .setRequired(true))
        .addStringOption(option =>
            option.setName('duration')
                .setDescription('Die Dauer (bspw. 30m für 30 Minuten oder 2h für 2 Stunden), nach der der Timeout aufgehoben wird')
                .setRequired(true))
        .addStringOption(option =>
            option.setName('reason')
                .setDescription('Der Grund, aus dem du den Nutzer timeouten möchtest'))
        .setDefaultMemberPermissions(PermissionFlagsBits.BanMembers),
    async execute(interaction) {
        const {guild} = interaction;
        const target = interaction.options.getUser('target');
        const member = guild.members.fetch({target, force: true});
        const reason = interaction.options.getString('reason') ?? 'Kein Grund angegeben';
        const duration = interaction.options.getString('duration');
        const durationInMs = ms(duration);


        if(member == null){
            const embed = new EmbedBuilder()
                .setColor(Colors.Red)
                .setTitle('Aktion')
                .addFields(
                    { name: '<:scroll:1133359100252651591> Ergebnis', value: 'Fehlschlag', inline: true },
                    { name: '\u200B', value: '\u200B', inline:true},
                    { name: '<:info:1133359642664251492> Aktion', value: 'Timeout', inline: true },
                    { name: '<:clipboard:1133357741394645084> Nachricht', value: `Der Nutzer ist nicht Teil dieses Servers.`, inline: true },
                )
                .setTimestamp();
            await interaction.reply({embeds: });
            return;
        }

        if(durationInMs == null){
            const embed = new EmbedBuilder()
                .setColor(Colors.Red)
                .setTitle('Aktion')
                .addFields(
                    { name: '<:scroll:1133359100252651591> Ergebnis', value: 'Fehlschlag', inline: true },
                    { name: '\u200B', value: '\u200B', inline:true},
                    { name: '<:info:1133359642664251492> Aktion', value: 'Timeout', inline: true },
                    { name: '<:clipboard:1133357741394645084> Nachricht', value: `Die angegebene Dauer ist falsch.`, inline: true },
                )
                .setTimestamp();
            await interaction.reply({embeds: });
            return;
        }

        if(durationInMs > 2419200000 ){
            const embed = new EmbedBuilder()
                .setColor(Colors.Red)
                .setTitle('Aktion')
                .addFields(
                    { name: '<:scroll:1133359100252651591> Ergebnis', value: 'Fehlschlag', inline: true },
                    { name: '\u200B', value: '\u200B', inline:true},
                    { name: '<:info:1133359642664251492> Aktion', value: 'Timeout', inline: true },
                    { name: '<:clipboard:1133357741394645084> Nachricht', value: `Die Dauer darf 28 Tage nicht überschreiten.`, inline: true },
                )
                .setTimestamp();
            await interaction.reply({embeds: });
            return;
        }

        if(durationInMs < 1000){
            const embed = new EmbedBuilder()
                .setColor(Colors.Red)
                .setTitle('Aktion')
                .addFields(
                    { name: '<:scroll:1133359100252651591> Ergebnis', value: 'Fehlschlag', inline: true },
                    { name: '\u200B', value: '\u200B', inline:true},
                    { name: '<:info:1133359642664251492> Aktion', value: 'Timeout', inline: true },
                    { name: '<:clipboard:1133357741394645084> Nachricht', value: `Die Dauer darf eine Sekunde nicht unterschreiten.`, inline: true },
                )
                .setTimestamp();
            await interaction.reply({embeds: });
            return;
        }

        const formattedDuration = ms(durationInMs);

        const embed = new EmbedBuilder()
            .setColor(Colors.DarkRed)
            .setTitle('Aktion')
            .addFields(
                { name: '<:scroll:1133359100252651591> Ergebnis', value: 'Erfolg', inline: true },
                { name: '\u200B', value: '\u200B', inline:true},
                { name: '<:info:1133359642664251492> Aktion', value: 'Timeout', inline: true },
                { name: '<:clipboard:1133357741394645084> Grund', value: `${reason}`, inline: true },
                { name: '\u200B', value: '\u200B', inline:true},
                { name: '<:moderator:1133359611852886047> Moderator', value: `${interaction.user}`, inline: true },
                { name: '\u200B', value: '\u200B', inline:true},
                { name: ':alarm_clock: Dauer', value: `${formattedDuration}`, inline: true },
                { name: '<:check:1133357168993783910> Ausgeführt gegen', value: `ㅤㅤ<:arrow:1133359137091227648> ${target}`, inline: false },
            )
            .setTimestamp();
        await interaction.reply({embeds: });
        member.timeout(durationInMs, `${reason} | Von ${interaction.user.username}`);
    },
};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Did you try to await your member from your guild ?

const member = await guild.members.fetch({target, force: true});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading