临时静音命令返回错误“无法读取未定义的属性”切片”

时间:2020-08-08 06:42:18

标签: javascript node.js discord discord.js

我正在尝试创建一个临时的静音命令,该命令将在给定时间内取消静音用户的声音。

这是我的代码:

const Discord = require("discord.js");
const ms = require("ms");

module.exports = {
    name: 'mute',
    description: 'mute a specific user',
    usage: '[tagged user] [mute time]',
    async execute(message, embed, args) {
        let tomute = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
        if (!tomute) return message.reply("Couldn't find user.");
        const reason = args.slice(1).join(' ');
        if (tomute.hasPermission("MANAGE_MESSAGES")) return message.reply("Can't mute them!");
        const muterole = message.guild.roles.cache.find(muterole => muterole.name === "muted");
        if (!muterole) {
            try {
                muterole = await message.guild.roles.create({
                    name: "muted",
                    color: "#000000",
                    permissions: []
                })
                message.guild.channels.cache.forEach(async (channel, id) => {
                    await channel.overwritePermissions(muterole, {
                        SEND_MESSAGES: false,
                        ADD_REACTIONS: false
                    });
                });
            } catch (e) {
                console.log(e.stack);
            }
        }
        const mutetime = args.slice(2).join(' ');
        //here is the start of the error 

        await (tomute.roles.add(muterole.id));
        message.reply(`<@${tomute.id}> has been muted for ${ms(ms(mutetime))} `);

        setTimeout(function() {
            tomute.roles.remove(muterole.id);
            message.channel.send(`<@${tomute.id}> has been unmuted!`);
        }, ms(mutetime));



    }
}

当前出现以下错误:Cannot read property 'slice' of undefined

您对如何修复命令有任何想法吗?

1 个答案:

答案 0 :(得分:0)

这意味着在代码中的某个地方,您有一个值为undefined的值,并尝试在其上使用字符串/数组函数切片,但是undefined没有此函数:错误。