javascript discord.js 发送预定消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47548081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:27:40  来源:igfitidea点击:

discord.js send scheduled message

javascriptnode.jsdiscorddiscord.js

提问by Samuel Smith

Hi I've just started on Javascript and Node.js, so I don't really know what to do. Please be patient with me.Thanks!

嗨,我刚刚开始使用 Javascript 和 Node.js,所以我真的不知道该怎么做。请耐心等待我。谢谢!

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" everyday at 8am. How do I do it?

所以我在我的物理服务器上托管了一个 node.js。我想创建一个 Discord Bot,它在我的服务器上的特定时间发送每日消息,例如,我想每天早上 8 点向频道发送一条消息,说“早安”。我该怎么做?

Currently, I only have this code to ping the bot(and the server)

目前,我只有这个代码来 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);

Also, how do I loop this code to ensure that it sends a message everyday? Thanks in advance.

另外,我如何循环此代码以确保它每天发送一条消息?提前致谢。

回答by André

so there is an answer for this:

所以有一个答案:

There are two ways to do that, with cron(or something else on different platforms) and setInterval

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

1) Cron

1) 定时任务

Create a new file, goodmorning.jswith this:

创建一个新文件,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();
});

(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.

(编辑所有需要的值:token、guildid 和 channelid)
并添加一个 cronjob,每天早上 8 点运行。
此脚本将尝试登录 Discord,成功登录后继续查找公会和频道,然后发送消息,最后退出 ( client.destroy())。如果没有找到公会或频道,只需将其摧毁即可。

2) setInterval

2)设置间隔

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:

第一个问题是您需要在您希望代码运行的确切时间启动脚本,或者获取 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");


I would definitely go for the cronoption, doesn't require you to have the process running all the time (unless you already have it)

我肯定会选择cron选项,不需要您一直运行该进程(除非您已经拥有它)