Home > Net >  Mute role not working with time, UnhandledPromiseRejectionWarning
Mute role not working with time, UnhandledPromiseRejectionWarning

Time:12-10

I'm making a mute and unmute command for our system bot, the mute should be timed by hours, but I have it as seconds just to test it, anyway, my problem is that when the command is used whether there is an args1 which is the time the mute will last in hours or not it sends an error and just uses the default time, anyone knows why?

My code:

const { MessageEmbed } = require('discord.js');

module.exports = {
    name: 'mute',
    category: 'Owner',
    aliases: ["t"],
    description: 'Mute command.',
    usage: 'mute <memeberid> <time>',
    userperms: [],
    botperms: [],
    run: async (client, message, args) => {
    if (!message.guild) return;
    if (message.author.bot) return;

    if (!message.member.roles.cache.has("916785912267034674")) return message.channel.send("You are not a staff member.").then(m => m.delete({timeout: 4000}))
    if (!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("I don't have permission to do this.").then(m => m.delete({timeout: 4000}))


    let time = args[1]
    let reason = args[2]

    if (!reason) reason = "Violated server rules";
    if (!time) time = "1"
    
    const user = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
    const muterole = message.guild.roles.cache.get("916963460540157962");
    const embed = new MessageEmbed()
      .setTitle('Member muted!')
      .addField('User muted', '<@' user '>')
      .addField('muted by', message.author)
      .addField('Reason', reason)
      .addField('For', time   " hour(s)")
      .setFooter('Time muted', client.user.displayAvatarURL())
      .setThumbnail('https://th.bing.com/th/id/R.3e3ee93bca49df93c9751dbb284d7ec8?rik=fKLepuY9WQQnew&riu=http://image.flaticon.com/icons/png/512/25/25632.png&ehk=mdsvAx56LxLhOmmktJkpp5Vbse/xjnaW8mxahrVoQeU=&risl=&pid=ImgRaw&r=0')
      .setTimestamp()

    if (!args[0]) return message.channel.send("Please mention a member or use an ID.")
    if (!user) return message.channel.send("Error: Can't find that user.")
    if (user.user.id == message.author.id) return message.channel.send("Uhh, why don't you just shut up like humans?")
    if (user.user.id == client.user.id) return message.channel.send("You good bro?")
    if (user == message.author.id) return message.channel.send("Uhh, why don't you just shut up like humans?")
    if (user == client.user.id) return message.channel.send("You good bro?")
    if (user.roles.cache.has("916963460540157962")) return message.channel.send("Chill, his already muted!")
    if (user.roles.cache.has("916785912267034674")) return message.channel.send("You can't mute staff, idoit.")

    message.channel.send(embed).catch(err => console.log("Error: "   err));
    user.roles.add("916963460540157962").then(user.roles.remove("916963460540157962") ({timeout: time "00000"})).catch(err => console.log("Error: "   err));
  }
}

Error:

(node:1460) UnhandledPromiseRejectionWarning: TypeError: user.roles.remove(...) is not a function
    at Object.run (/home/runner/DwaCraft-Main-bot/commands/owner/Mute.js:47:87)
    at module.exports (/home/runner/DwaCraft-Main-bot/events/guild/message.js:52:11)
    at Client.emit (events.js:314:20)
    at MessageCreateAction.handle (/home/runner/DwaCraft-Main-bot/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/DwaCraft-Main-bot/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/DwaCraft-Main-bot/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/home/runner/DwaCraft-Main-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/runner/DwaCraft-Main-bot/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/runner/DwaCraft-Main-bot/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:314:20)
(node:1460) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1460) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Thanks !

CodePudding user response:

The syntax seems to be incorrect, try with :

user.roles.add("916963460540157962").then(() => setTimeout(() => user.roles.remove("916963460540157962",  time))

CodePudding user response:

You are using user.roles.remove(id) ({timeout: time "00000"}) which is obviously bad syntax. Also, role removal does not have a built in timeout. You will need to use setTimeout, or a database for larger times

await user.roles.add(id)
setTimeout(() => user.roles.remove(id), time * 1000) // *1000 for seconds

As I said before, it only works well with short times, use a database for longer times

  • Related