如果没有静音时间,discord.js不能将成员静音10m

时间:2020-06-16 10:28:25

标签: javascript discord.js

如果有人没有指定时间,我的机器人如何使某人静音1000万 但是这里发生的是没有时间静音,时间立即结束而没有时间:

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

谢谢

1 个答案:

答案 0 :(得分:0)

只需使用OR运算符

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

如果没有 静音时间,您没有显示代码,但是我猜它类似于if语句,但是现在您甚至不需要if语句

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);