Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

(discord.js) How to set a time limit on a command?

I am trying to make a Judge bot for my server that will give the person who used the command the role "Plaintiff", and give the person that they mentioned the "Defendant" role. Everything works perfectly as intended except the fact that multiple trials can be started at the same time. Like if someone uses the command and it makes them the plaintiff and a 2nd person the defendant, if a 3rd person uses the command on a 4th person they will become a defendant and plaintiff as well. I want to make it so that while the events of the command are playing out that nobody else in the server can use the command until the 1st trial ends. I have tried to give the entire server the role "jury" and make it so that if anyone has the role "jury" they can't use the command but i couldn't ever get that to work. Also I tried to set a time limit for the command and that didn't work at all either. I have no clue how to get this working and any advice is appreciated!! Thanks!!!

client.on("message", message => {
   
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if (command === "sue") {
       let member1 = message.mentions.members.first();
       message.member.roles.add("797738368737345536")
       member1.roles.add("797738235715256331");
       message.guild.members.cache.forEach(member => member.roles.add("797743010346565672"))
       message.channel.send("Court is now in session.")
       client.channels.cache.find(channel => channel.name === 'courtroom').send( + " **All rise.**"); 
       client.channels.cache.find(channel => channel.name === 'courtroom').send("Plaintiff, you have 2 minutes to state your case.");  
       setTimeout(function(){ 
        client.channels.cache.find(channel => channel.name === 'courtroom').send("Thank you Plaintiff. Defendant, you now have 2 minutes to defend yourself."); 
    }, 120000);
       setTimeout(function(){  
        client.channels.cache.find(channel => channel.name === 'courtroom').send("Thank you. The court may be seated as we turn to the jury for their verdict.");   
    }, 240000);

    const Responses = [
        "The Jury hereby finds you.... **GUILTY!** You will be sentanced to 10 minutes in jail.",
        "The Jury hereby finds you.... **NOT GUILTY!** You are free to go. The Jury is dismissed."
    ];

    const Response = Responses[Math.floor(Math.random() * Responses.length)];

    setTimeout(function(){
    client.channels.cache.find(channel => channel.name === 'courtroom').send(Response)
   
    if (Response === "The Jury hereby finds you.... **NOT GUILTY!** You are free to go. The Jury is dismissed." ) {
        message.member.roles.remove("797738368737345536")
        member1.roles.remove("797738235715256331");
        message.guild.members.cache.forEach(member => member.roles.remove("797743010346565672"))
    }

    if (Response === "The Jury hereby finds you.... **GUILTY!** You will be sentanced to 10 minutes in jail.") {
        message.member.roles.remove("797738368737345536")
        member1.roles.add("797738617338069002")
        member1.roles.remove("797738235715256331");
        message.guild.members.cache.forEach(member => member.roles.remove("797743010346565672"))
    }
    }, 250000);

    setTimeout(function(){
        member1.roles.remove("797738617338069002");
    }, 850000);

    }  
    
}); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could create a file, so nobody is able to use the command while the file exists.

const fs = require("fs");

if (command_gets_executed) { // If the command gets executed

 if (fs.existsSync("./commandAlreadyUsed")) { // Checking if the file exists

  // YOUR CODE - When the command should not be usable

 } else {

  fs.writeFileSync("./commandAlreadyUsed"); // Creating the file

  // YOUR CODE - When nobody else used the command before

 }
 

}

And if you want the command usable again just use fs.unlinkSync("./commandAlreadyUsed").


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...