【问题标题】:Send scheduled message发送预定消息
【发布时间】: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


【解决方案1】:

所以有一个答案:

有两种方法可以做到这一点,使用 cron(或不同平台上的其他东西)和 setInterval

1) 定时任务

用这个创建一个新文件goodmorning.js

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())。如果没有找到公会或频道,直接销毁即可。

2) 设置间隔

这样做的第一个问题是您需要在您希望代码运行的确切时间启动脚本,或者获取一个 setTimeout 来启动 setInterval 以一遍又一遍地重复代码。
未经测试,但可能需要进行一些调整:

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 选项,不需要您一直运行该进程(除非您已经拥有它)

【讨论】:

  • 请注意您是否在上午 8 点后运行此选项 2,因为在这种情况下,它将立即触发,然后从该点开始 24 小时。由于 d.setHours 会将时间戳设置为当前时间之前,leftToEight() 的结果将为负数。当 setTimeout 接收到负输入时,它将使用最小值(我猜是 1 毫秒?)
  • cron 选项是否会干扰正在进行的运行代码,例如我的 main.js,它在 24/7 上
  • @Ryuujo 不会的。
  • 它的 cron 部分在哪里?你说的cron表达式运行的程序在哪里?
  • @avivgood2 这里不提了。 Cron 是一个单独的调度程序,您可以使用它来调用脚本。即,每当您运行脚本时,它都会发送相应的消息。 Cron 负责在指定时间运行。
猜你喜欢
  • 2013-01-08
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 2022-12-23
  • 2015-08-18
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多