I am trying to create a temporary mute command, that will unmute the muted user in the given time.
Here’s my code:
JavaScript
x
46
46
1
const Discord = require("discord.js");
2
const ms = require("ms");
3
4
module.exports = {
5
name: 'mute',
6
description: 'mute a specific user',
7
usage: '[tagged user] [mute time]',
8
async execute(message, embed, args) {
9
let tomute = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
10
if (!tomute) return message.reply("Couldn't find user.");
11
const reason = args.slice(1).join(' ');
12
if (tomute.hasPermission("MANAGE_MESSAGES")) return message.reply("Can't mute them!");
13
const muterole = message.guild.roles.cache.find(muterole => muterole.name === "muted");
14
if (!muterole) {
15
try {
16
muterole = await message.guild.roles.create({
17
name: "muted",
18
color: "#000000",
19
permissions: []
20
})
21
message.guild.channels.cache.forEach(async (channel, id) => {
22
await channel.overwritePermissions(muterole, {
23
SEND_MESSAGES: false,
24
ADD_REACTIONS: false
25
});
26
});
27
} catch (e) {
28
console.log(e.stack);
29
}
30
}
31
const mutetime = args.slice(2).join(' ');
32
//here is the start of the error
33
34
await (tomute.roles.add(muterole.id));
35
message.reply(`<@${tomute.id}> has been muted for ${ms(ms(mutetime))} `);
36
37
setTimeout(function() {
38
tomute.roles.remove(muterole.id);
39
message.channel.send(`<@${tomute.id}> has been unmuted!`);
40
}, ms(mutetime));
41
42
43
44
}
45
}
46
Currently getting the following error: Cannot read property 'slice' of undefined
Do you have any idea on how to fix the command?
edit
this after a year and this is for future people who come here
the issue was here
JavaScript
1
2
1
async execute(message, embed, args) {
2
i never passed embed from my main file so args was undefined embed part was where the args should have been, I was stupid at that time and was new to coding, but as I now have some experience, I decided to edit this to show what wrong
Advertisement
Answer
It means that somewhere in your code, you have a value which is undefined
and you try to use the string/array function slice on it but undefined
does not have this function : so it is a error.