admin管理员组文章数量:1024638
I have a problem I almost resolved but i'm now stuck.
I want to make my bot send a message in a channel at mirror hours (00h00, 01h01, 02h02...) for a running gag with my friends and currently I made this:
At the top of my code I have var currentdate = new Date();
And then, later in my source code:
if(currentdate.getMinutes() == currentdate.getHours())
{
bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
}
It's sort of working since the message is sent by the bot in the right channel, but the message is only sent when the bot detects a message, so if during any mirror hour, no one send a message, then the bot will not send anything.
And if there is multiples messages during this interval of time, the bot will also send the message multiple times, of course I want it to send the message only 1 time for exemple at 11:11:00.
Thank you for the help and sorry if my english is bad !
I have a problem I almost resolved but i'm now stuck.
I want to make my bot send a message in a channel at mirror hours (00h00, 01h01, 02h02...) for a running gag with my friends and currently I made this:
At the top of my code I have var currentdate = new Date();
And then, later in my source code:
if(currentdate.getMinutes() == currentdate.getHours())
{
bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
}
It's sort of working since the message is sent by the bot in the right channel, but the message is only sent when the bot detects a message, so if during any mirror hour, no one send a message, then the bot will not send anything.
And if there is multiples messages during this interval of time, the bot will also send the message multiple times, of course I want it to send the message only 1 time for exemple at 11:11:00.
Thank you for the help and sorry if my english is bad !
Share Improve this question edited Feb 17, 2019 at 18:05 Misubata asked Feb 17, 2019 at 17:39 MisubataMisubata 412 silver badges6 bronze badges 3- You can take a look at the node-cron package to set a task at specific times. Else you could probably do it aswell with a setInterval – T. Dirks Commented Feb 17, 2019 at 18:09
-
Could you provide more code? Where are you running this snippet? If you are running it inside a
on('message')
listener, that may answer why your code is not called if a message is not sent. – user9016207 Commented Feb 17, 2019 at 18:09 -
@WillHoskings Yeah it's actually inside the
on('message')
but when I put it outside of it, the bot just do nothing at all. – Misubata Commented Feb 17, 2019 at 18:15
2 Answers
Reset to default 3You need to be checking at some interval whether or not to send a message.
Something like setInterval
would work.
setInterval(function(){
if(currentdate.getMinutes() == currentdate.getHours())
{
bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
}
}, MIN_INTERVAL)
You want MIN_INTERVAL
to be the minimum amount of time in milliseconds to check for sending messages.
If you want to check every minute
const MIN_INTERVAL = 1000 * 60
You have to use if statement and you didn't specified hour or minutes, so the bot can't send message.
async function verifyTime() {
var d = new Date();
if (d.getHours() == d.getMinutes()) {
//your code goes here
} catch (error) {
console.log(error);
}
setTimeout(() => {
verifyTime();
}, 61 * 1000);
} else {
setTimeout(() => {
verifyTime();
}, 1000);
}
}
client.login(token);
//⬇ remember to place this under your client.login
setTimeout(() => {
verifyTime();
}, 5000);
I have a problem I almost resolved but i'm now stuck.
I want to make my bot send a message in a channel at mirror hours (00h00, 01h01, 02h02...) for a running gag with my friends and currently I made this:
At the top of my code I have var currentdate = new Date();
And then, later in my source code:
if(currentdate.getMinutes() == currentdate.getHours())
{
bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
}
It's sort of working since the message is sent by the bot in the right channel, but the message is only sent when the bot detects a message, so if during any mirror hour, no one send a message, then the bot will not send anything.
And if there is multiples messages during this interval of time, the bot will also send the message multiple times, of course I want it to send the message only 1 time for exemple at 11:11:00.
Thank you for the help and sorry if my english is bad !
I have a problem I almost resolved but i'm now stuck.
I want to make my bot send a message in a channel at mirror hours (00h00, 01h01, 02h02...) for a running gag with my friends and currently I made this:
At the top of my code I have var currentdate = new Date();
And then, later in my source code:
if(currentdate.getMinutes() == currentdate.getHours())
{
bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
}
It's sort of working since the message is sent by the bot in the right channel, but the message is only sent when the bot detects a message, so if during any mirror hour, no one send a message, then the bot will not send anything.
And if there is multiples messages during this interval of time, the bot will also send the message multiple times, of course I want it to send the message only 1 time for exemple at 11:11:00.
Thank you for the help and sorry if my english is bad !
Share Improve this question edited Feb 17, 2019 at 18:05 Misubata asked Feb 17, 2019 at 17:39 MisubataMisubata 412 silver badges6 bronze badges 3- You can take a look at the node-cron package to set a task at specific times. Else you could probably do it aswell with a setInterval – T. Dirks Commented Feb 17, 2019 at 18:09
-
Could you provide more code? Where are you running this snippet? If you are running it inside a
on('message')
listener, that may answer why your code is not called if a message is not sent. – user9016207 Commented Feb 17, 2019 at 18:09 -
@WillHoskings Yeah it's actually inside the
on('message')
but when I put it outside of it, the bot just do nothing at all. – Misubata Commented Feb 17, 2019 at 18:15
2 Answers
Reset to default 3You need to be checking at some interval whether or not to send a message.
Something like setInterval
would work.
setInterval(function(){
if(currentdate.getMinutes() == currentdate.getHours())
{
bot.channels.get('SPECIFICCHANNELID').send('Touchez votre nez :nose:');
}
}, MIN_INTERVAL)
You want MIN_INTERVAL
to be the minimum amount of time in milliseconds to check for sending messages.
If you want to check every minute
const MIN_INTERVAL = 1000 * 60
You have to use if statement and you didn't specified hour or minutes, so the bot can't send message.
async function verifyTime() {
var d = new Date();
if (d.getHours() == d.getMinutes()) {
//your code goes here
} catch (error) {
console.log(error);
}
setTimeout(() => {
verifyTime();
}, 61 * 1000);
} else {
setTimeout(() => {
verifyTime();
}, 1000);
}
}
client.login(token);
//⬇ remember to place this under your client.login
setTimeout(() => {
verifyTime();
}, 5000);
本文标签: javascriptMake my discord bot send message at specific hoursStack Overflow
版权声明:本文标题:javascript - Make my discord bot send message at specific hours - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745608606a2158874.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论