問題描述
我剛剛開始使用 Javascript 和 Node.js,所以我真的不知道該怎么做.請耐心等待我.謝謝!
I've just started on Javascript and Node.js, so I don't really know what to do. Please be patient with me. Thanks!
所以我在我的物理服務器上托管了一個 node.js.我想創建一個 Discord Bot,它在我的服務器上的特定時間發送每日消息,例如,我想每天早上 8 點向頻道發送一條消息,說早安".我該怎么做?
So I've hosted a node.js on my physical server. I wanted to create a Discord Bot that sends a daily message on specific timings on my server, for example, I want to send a message to a channel saying "Good Morning" every day at 8 am. How do I do it?
目前,我只有這段代碼可以 ping 機器人(和服務器)
Currently, I only have this code to ping the bot (and the server)
/*
A ping pong bot, whenever you send "ping", it replies "pong".
*/
// Import the discord.js module
const Discord = require('discord.js');
// Create an instance of a Discord client
const client = new Discord.Client();
// The token of your bot - https://discordapp.com/developers/applications/me
const token = 'your bot token here';
// The ready event is vital, it means that your bot will only start reacting to information
// from Discord _after_ ready is emitted
client.on('ready', () => {
console.log('I am ready!');
});
// Create an event listener for messages
client.on('message', message => {
// If the message is "ping"
if (message.content === 'ping') {
// Send "pong" to the same channel
message.channel.send('pong');
}
});
// Log our bot in
client.login(token);
另外,我如何循環這段代碼以確保它每天都發送消息?提前致謝.
Also, how do I loop this code to ensure that it sends a message everyday? Thanks in advance.
推薦答案
所以有一個答案:
有兩種方法可以做到這一點,使用 cron(或不同平臺上的其他東西)和 setInterval
There are two ways to do that, with cron (or something else on different platforms) and setInterval
創建一個新文件,goodmorning.js
,其中:
Create a new file, goodmorning.js
with this:
const Discord = require('discord.js');
const client = new Discord.Client();
client.login("token").then(() => {
console.log("I am ready");
var guild = client.guilds.get('guildid');
if(guild && guild.channels.get('channelid')){
guild.channels.get('channelid').send("Good Morning").then(() => client.destroy());
} else {
console.log("nope");
//if the bot doesn't have guild with the id guildid
// or if the guild doesn't have the channel with id channelid
}
client.destroy();
});
(編輯所有需要的值:token、guildid 和 channelid)
并添加一個 cronjob 以在每天早上 8 點運行.
該腳本將嘗試登錄 Discord 并在成功登錄后繼續查找公會和頻道,然后發送消息,最后注銷 (client.destroy()
).如果沒有找到公會或頻道,直接銷毀即可.
(edit all the needed values: token, guildid and channelid)
And add a cronjob to run everyday at 8am.
This script will attempt to login into Discord and after successful login proceeds to find a guild and a channel, then just send the message, then finally logout (client.destroy()
). If it wasn't found a guild or channel, just simply destroy.
這樣做的第一個問題是您需要在您希望代碼運行的確切時間啟動腳本,或者獲取一個 setTimeout 來啟動 setInterval 以一遍又一遍地重復代碼.
未經測試,但可能需要進行一些調整:
The first problem with this would be that you need to start the script at the exact time you want the code to run, or get a setTimeout to start the setInterval to repeat the code over and over.
untested but should work with possibly some tweaking needed:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', message => {
//...
});
client.on('ready', () => {
setTimeout(function(){ // in leftToEight() milliseconds run this:
sendMessage(); // send the message once
var dayMillseconds = 1000 * 60 * 60 * 24;
setInterval(function(){ // repeat this every 24 hours
sendMessage();
}, dayMillseconds)
}, leftToEight())
})
function leftToEight(){
var d = new Date();
return (-d + d.setHours(8,0,0,0));
}
function sendMessage(){
var guild = client.guilds.get('guildid');
if(guild && guild.channels.get('channelid')){
guild.channels.get('channelid').send("Good Morning");
}
}
client.login("token");
<小時>
我肯定會選擇 cron 選項,不需要您一直運行該進程(除非您已經擁有它)
I would definitely go for the cron option, doesn't require you to have the process running all the time (unless you already have it)
這篇關于發送預定消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!