Home > database >  Timed mute stops counting on bot restart, discord.js
Timed mute stops counting on bot restart, discord.js

Time:12-11

I'm trying to have the bot save the end times of the mute, so if he goes offline then online again (Update), it won't just forget about unmuting the muted member and just leave him muted.

I have the timed mute working fine, but I just want to have it remember the end times.

I'm thinking a database or something?

Notes:

  1. I'm using a command handler.
  2. I'm using https://replit.com

My code:

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

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 = "1h"
    
    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', ms(ms(args[1])))
      .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").catch(err => console.log("Error: "   err));

    setTimeout(function() {
      user.roles.remove("916963460540157962").catch(err => console.log("Error: "   err));
    }, ms(time));
  }
}

Thanks.

CodePudding user response:

setTimeout is "deleted" or discarded when the app is restarted

So it'll discard all timeouts and intervals every bot restart, so you should find another way to do what you want to do

I would do some experiments with databases and things

For example, every temp mute is saved to a database, when the bot is restarted it loads temp mutes and calculates it's remaining time, and it continues counting

  • Related