Time Delay until next message sent from Discord Bot - discord

In the Discord bot I'm creating, I'd like to make it so that the bot waits lets say 60 seconds before sending another reply to the same command or "includes" word. For instance, if someone says ping below, I want the bot to wait a minute until the next one is sent. I do not want the bot to hold it and put it into a queue or anything of that sort, I just want it to ignore the command for a minute until the next one happens.
if (message.content.includes("ping")) {
message.reply("pong!");
}

If I understand your question correctly, you would want to make a boolean variable and use setTimeout.
The global function setTimeout waits for the provided amount of milliseconds, in this case 60000 (60 seconds), then changes the variable to true again so it can be run.
var pingEnabled = true;
if (ping command run && pingEnabled) {
reply("Pong!");
pingEnabled = false;
setTimeout(() => { pingEnabled = true }, 60000);
}
most of this is pseudocode and you may need to adapt it to your situation.

Related

Avoid rate limit for changing voice channel name discord js 13

I'm trying to create a slash command using discord.js v13 to change the names of voice channels. I am using this code to do this :
module.exports = {
data: new SlashCommandBuilder()
.setName('name')
.setDescription('Set name for your voice channel')
.addStringOption(option => option.setName('name').setDescription('Enter your name').setRequired(true)),
async execute(interaction) {
const name = interaction.options.getString('name');
if (!interaction.member.voice.channel) await interaction.reply('Error not in a voice channel!');
else {
await interaction.member.voice.channel.setName(name);
await interaction.reply('Done!');
}
},
};
This code is fine and makes the job done. But as you know I can change the voice channel's name only 2 times per 10 minutes because of the limit rate. So if a user tries to change the voice channel's name for the third time, I won't get any error on the console, and discord js will queue this request for later and will do it after 10 minutes. But the user gets this error on discord: This interaction failed.
I want to check if there was a rate limit for my request, and if is, don't send the request and just reply to the user. Is this possible?
There is no inherent functionality that is able to handle the situation in the way you want it to, but the problem is soluble using regular old JavaScript. For example, you could use an integer to indicate how many times the command has been used and use setTimeout() to decrement it 10 minutes after the command was called. That way you can check if the int is equal to 2 in which case you skip the .setName().
There are undoubtedly other ways to implement the same or similar behavior, but, to answer your question, unfortunately the discordjs/voice library does not provide any simple way to do it.

How would you loop a command using discord.js v.12?

So I have this code here? client.channels.cache.get("823609622488154143").send( animeEmbed) }, 1 * 1);
This code is attached to a embed I am sending that my bot is fetching from a reddit site, I would like this command to continuously run but I am having difficulties figuring out how I can loop this. Can this be done through an async run or differently.
I guess since you don't want to send it nonstop, you are looking for something like this:
function startTimer(time) {
setTimeout(function() {
//the code that sends the embed
}, time)
}
startTimer(60000) //one minute interval
What this does is, it runs the function that sends your embed every one minute to the channel you specified. You can change the time by replacing the 60000 with another number (1000 is one second).
I hope this is what you are looking for. Have a nice day :)

How do I stop an infinite loop command on a Discord Bot?

Let's say I made an infinite command for my bot, Would there be any way to stop the loop at any time? I want to be able to stop it from the server, not in the actual code.
Example:
if(msg.content === "Just Monika"){
msg.channel.send('Just Monika')
}
})
Is there any way I can type something in chat, and it stops the command? thanks.
Making your bot respond to itself infinitely probably isn't a good idea. But just for learning, it's very possible to do what you wish.
You could make a different phrase (let's call it the stop command) set a boolean variable in your code to true. Then, whenever the looping command gets triggered by a user message or by one of its own, it should check if this boolean telling it to stop is true. If it is, it should set it to false and not send that message, else it should just send that message as per usual.
// The following should be defined in the outmost scope
let loopPhrase = "Just Monika";
let stopPhrase = "Stop Spamming The API";
let triggerStop = false;
// The following should be a part of the message event
if (msg.content === loopPhrase) {
if (!triggerStop) msg.channel.send(loopPhrase);
else triggerStop = false;
} else if (msg.content === stopPhrase) triggerStop = true;

Discord.js - How to make a function only run every 30 seconds

I made a bot that replies to a user's message but I want to limit it so that the bot only responds every 30 seconds and ignores the messages within that time. Do I use SetInterval?
Code:
setTimeout(function() {
main();
}, 30000);
function main(){
***command here***
}
This isn't working as well.
Yes, you should use setInterval() for that.
However, I suggest using client.setInterval() and client.setTimeout() instead of just setInterval() and setTimeout() because according to the docs:
Sets an interval that will be automatically cancelled if the client is destroyed.
Which means that if you restart or stop your bot those timers would be cleared.
client.setInterval(function() {
main();
}, 30000);

discord.js not sending message at specific time

So I'm trying to make the bot send a message at a specific time (Hour, Minute, Second), at a specific channel. However, its really not doing what its programmed to do.
Right now, the time set is 15:25:00, however, its not sending the message on that time.
client.on("message", message => {
var day = new Date();
var hr = day.getHours();
var min = day.getMinutes();
var sec = day.getSeconds();
if (hr == 15) {
if (min == 25){
if (sec == 0) {
client.channels.get("704015571531857950").send("Test")
}
}
}
});
Always look at the parent function :) Your if conditions are in client.on("message", message => { ... }), meaning you only check the time when someone sends a message, only allowing the bot to send the message if someone sends a message exactly at 15:25:00. Also, to send a message to a specific channel, your method is outdated, here is the new solution:
client.channels.fetch("704015571531857950").then((channel) => {
channel.send("Test");
});
setInterval every 10 seconds is bad for prefromance, I would suggest doing some math and using setTimeout, although setTimeout might be a bit off, don't think it would be too much.
like vrecusko said your method is outdated, but using fetch isn't the only/best option.
client.channels.cache.get("704015571531857950").then(channel => {
channel.send("Message");
});
Can you explain what you are doing further? Is there a command where you set the time to send a message? Or is this like a message you want the bot to send every day at the same time?

Resources