简体   繁体   中英

discord.js can't mute a member for 10m if there's no mute time

how can my bot mute someone for 10m in case someone didn't specify a time but what happens here is that there's no time for the mute, the time ends immediately without a time:

client.on('message', async message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    if(command === 'mute') {
        // !mute @user 1s/m/h/d
        message.delete().catch();
        const tomute = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
        const rule = message.guild.roles.find(r => r.name === 'member');
        const mutetime = args[1];
        let muterole = message.guild.roles.find(c => c.name === 'muted');

        if(!muterole) {
            try{
                muterole = await message.guild.createRole({
                    name: 'muted',
                    color: '#000000',
                    permissions:['MUTE_MEMBERS', 'MANAGE_ROLES_OR_PERMISSIONS', 'SEND_MESSAGES', 'ADD_REACTIONS'],
                });
                message.guild.channels.forEach(async (channel) => {
                    await channel.overwritePermissions(muterole, {
                        SEND_MESSAGES: false,
                        ADD_REACTIONS: false,
                        MANAGE_ROLES_OR_PERMISSIONS: false,
                    });
                });
            }
            catch(e) {
                console.log(e.stack);
            }
        }
        if(!mutetime) {
            tomute.removeRole(rule.id);
            setTimeout(function() {
                tomute.removeRole(muterole.id).then(() => {
                    tomute.addRole(rule.id);
                    message.channel.send('```Mute time has ended for ' + `${tomute.user.tag} with id : ${tomute.id}` + '```');
                });
            }, ms(600000));
        }
... the rest of the code

Thanks

Just use the OR operator

// check if args[1] is valid
const mutetime = args[1] && ms(args[1]) || 600000;

You did not show the code if there is a mute time but I'm guessing its something similar to the if statement, instead now you don't even need an if statement

tomute.removeRole(rule.id);
setTimeout(function () {
    tomute.removeRole(muterole.id).then(() => {
        tomute.addRole(rule.id);
        message.channel.send('```Mute time has ended for ' + `${tomute.user.tag} with id : ${tomute.id}` + '```');
    });
}, mutetime);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM