发送预定消息

2022-01-10 00:00:00 node.js javascript discord.js

我刚刚开始使用 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)

相关文章