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?
Related
Well i need to do this:
on a channel a bot announces "btc" price (not real)
im trying to get the price and send the price to a specificed channel
My code
sa.on("message", (message) => {
if (message.content.startsWith("🟠")) {
if (message.author.id == "974187708933083157") {
client.channels.get('955536998989447249').send(`${message.content}`);
} else message.channel.send(``);
}
})
So if a message that starts with 🟠the bot needs to get the price and send it to the channel (955536998989447249)
But it not working my bot is working fine but not getting price and sendimg it
Firstly don't try sending empty message message.channel.send(``);, it will just throw an error.
The main problem, is that client.channels (ChannelManager) doesn't have a "get" method.
Assuming you are using djs v13, to get a channel, you can get it from cache with:
client.channels.cache.get('955536998989447249').send("sth")
however this might not always work if the channel is not currently cached.
Other option is to fetch it, though it is an asynchronous operation, so it looks a bit different:
client.channels.fetch('955536998989447249').then(channel => channel.send("sth"))
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 :)
I'm currently in the middle of writing a Quiz Bot and added a little set to make sure the quiz taker doesn't restart mid-quiz.
So I have basically added the set, and upon messaging the bot 'start', it will add the user to the set and won't remove him until 10 minutes later, which is the time span that you have to complete the quiz by.
While running the bot itself, the set doesn't really seem to affect the bot at all, and if you do happen to message him "start" mid-quiz he will restart it without any issues - I have also tested this by attempting at typing 'hello' in the console if the user attempts to do so, but nothing happens yet again.
Any kind of help would be appreciated!
client.on('message', async message => {
const midQuiz = new Set()
const filter = m => m.author.id === message.author.id
let mistakes = 0;
if (message.content === 'start') {
if (message.channel.type == "dm") {
if (midQuiz.has(message.author.id)) {
console.log('hello')
return message.author.send('You're currently mid-quiz! You may not restart it now.')
} else {
midQuiz.add(message.author.id)
setTimeout(() => {
midQuiz.delete(message.author.id)
message.author.send('10 minutes have gone by and you have not completed the quiz, therefore you have failed.')
}, 600000)
const Embed = new Discord.MessageEmbed()
.setTitle("Verification Quiz")
.setDescription("Question #1: What is the owner's name?")
.setColor(3426654)
.setFooter("You may type 'cancel' at any time to stop the quiz.")
message.author.send(Embed)
You're declaring midQuiz inside the message event, making it so that the set gets redeclared and reset every single message.
// outside of the event
const midQuiz = new Set();
client.on('message', ...);
I was wondering if there is a way to block the sending of multiple messages at the same time from different people in a channel discord. Whether his silk with discord.js or discord.py. In itself, I want the bot to take into account the first message sent and ignore others sent at the same time
cordially,
Quentin S.
One solution would be to track the timestamp of the last message listened to, and then ignore any messages sent before a set amount of time has elapsed. See this example...
let lastMessageTime;
const sec = 2; // Seconds to wait between messages.
client.on('message', message => {
if (!message.guild || message.author.bot) return;
if (lastMessageTime && message.createdTimestamp - (sec * 1000) < lastMessageTime) return;
else lastMessageTime = message.createdTimestamp;
// Continue with 'message' event.
});
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.