【发布时间】:2018-05-12 21:13:41
【问题描述】:
我刚刚开始使用 Javascript 和 Node.js,所以我真的不知道该怎么做。请耐心等待我。谢谢!
所以我在我的物理服务器上托管了一个 node.js。我想创建一个 Discord Bot,它在我的服务器上的特定时间发送每日消息,例如,我想每天早上 8 点向频道发送一条消息,说“早安”。我该怎么做?
目前,我只有这段代码可以 ping 机器人(和服务器)
/*
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);
另外,我如何循环这段代码以确保它每天都发送消息?提前致谢。
【问题讨论】:
-
您可以使用 cron 作业或
setInterval -
感谢乔治!我认为 cron 对我有用。
标签: javascript node.js discord.js