簡體   English   中英

如何使 discord js 機器人在某個時間在 discord 的確切文本通道中發送隨機消息(我創建一個列表並發送它)

[英]How to make an discord js bot to send an random message(i make an list and he sends it) at some time in an exact text channel from discord

這就是命令的工作方式在此處輸入圖像描述

這是我的 main.js 在此處輸入圖像描述

請幫忙?我真的需要它,如果你能幫助我,我會非常感激

首先,您需要找到頻道 ID。 最好將 go 進入 Discord 應用程序並右鍵單擊通道和 select “復制 ID”。 它應該看起來像這樣:845346073543326453

現在要將某些內容發送到該特定頻道,您必須執行以下操作:

const channel = client.channels.cache.get(845346073543326453);
channel.send("hello!")

對於隨機消息,您只需創建一個數組並隨機選擇一個:

const random = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min);
}
let randomMsg = [`Howdy`, `Howdily doodily`, `Zoinks`]
channel.send(quotes[random(0, quotes.length - 1)])

在特定時間發送它有很多方法。 我建議使用cron package 並參考這篇文章: 如何每天在特定時間發送消息?

但是,如果您只是想要一種快速且非常省力的方式,您可以使用 setInterval() 並將延遲設置為一個小時。 所以我們最終會得到這樣的結果:

const channel = client.channels.cache.get(845346073543326453);
const randomMsg = [`Howdy`, `Howdily doodily`, `Zoinks`]
const random = (min, max) => {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

const sendRandomMsg = () => {
  var d = new Date();
  var n = d.getHours();
  if (n === 12) {
    channel.send(randomMsg[random(0, quotes.length - 1)])
  }
}

setInterval(function(){ sendRandomMsg() }, 3600000);

您可以在 if 中添加更多功能,以防您在特定時間有更多功能要運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM