久久久久久久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?)
主站蜘蛛池模板: 99热最新 | 成人观看视频 | 做爰xxxⅹ性生交 | 一本一道久久a久久精品蜜桃 | 一级中国毛片 | 超碰麻豆 | 天天干夜夜艹 | 欧美www | 中文字幕黄色片 | 在线中文字幕 | 麻豆国产一区二区三区四区 | 亚洲成人国产 | 开心激情站 | 亚洲福利视频一区 | 国产精品911 | 99视频网站 | 国产免费高清视频 | 国产91一区 | 亚洲一区在线看 | 精品一二三| 欧美一级特黄视频 | 亚洲久草| 日本中文在线观看 | 男女啪啪免费 | 在线看黄色片 | 91精品久久久久久久久久 | 久婷婷| 午夜免费剧场 | 黄色一集片 | 婷婷综合五月天 | 精品一区av | 毛片在线视频 | 五月激情网站 | 欧美日韩国产中文 | 日韩精品成人 | 99热在线观看 | 91午夜精品亚洲一区二区三区 | 私人午夜影院 | 国产91丝袜在线播放 | 中文字幕手机在线观看 | 日韩少妇av |