久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

發送預定消息

Send scheduled message(發送預定消息)
本文介紹了發送預定消息的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我剛剛開始使用 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Using discord.js to detect image and respond(使用 discord.js 檢測圖像并響應)
Check if user ID exists in Discord server(檢查 Discord 服務器中是否存在用戶 ID)
Guild Member Add does not work (discordjs)(公會成員添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 創建我的第一個機器人,但總是錯誤 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何為我的 Discord.js 機器人編寫事件/命令處理程序?)
How to find a User ID from a Username in Discord.js?(如何從 Discord.js 中的用戶名中查找用戶 ID?)
主站蜘蛛池模板: 久久乐国产精品 | 亚洲免费一区二区 | 亚洲成人一区二区 | www亚洲精品 | 欧美精品一区二区三区在线播放 | 欧美日本久久 | 狠狠躁躁夜夜躁波多野结依 | 亚洲手机视频在线 | 久久久久一区二区三区 | 一级黄色影片在线观看 | 久久久人| 狠狠干美女 | 91成人在线 | 国产精品免费一区二区三区 | 四虎影院美女 | 国产乱人伦| 91精品国产综合久久精品图片 | 亚洲国产成人精品久久久国产成人一区 | 精精国产xxxx视频在线播放 | pacopacomama在线 | 午夜久久久 | 男人天堂免费在线 | 午夜电影一区二区 | 欧美欧美欧美 | 男女羞羞视频在线观看 | 亚洲精品美女在线观看 | 日韩精品久久一区二区三区 | 欧美成年黄网站色视频 | 一区二区三区日韩 | 亚洲精品乱码 | 一级片免费在线观看 | 天天操天天插天天干 | 香蕉大人久久国产成人av | 中文字幕在线看 | 成人精品鲁一区一区二区 | 国产综合精品一区二区三区 | 国产一区二区在线视频 | 亚洲啪啪 | 成人激情视频网 | h视频免费观看 | 久久国产精品久久久久久久久久 |