[英]Send message every x hrs on Discord in every group/guild
目前我正在開發一個事件機器人,該機器人用於 Discord 中的幾個組。 所以現在我有這個代碼:
if (command === "init")
{
//var d = new Date();
//var n = d.getHours();
message.channel.send("BunnBot starting...");
var interval = setInterval (function () {
message.channel.send("123")
//message.channel.send(n);
}, 30 * 1000);
}
問題是,此命令僅適用於當前組,這意味着在任何其他組中我也必須使用 init 命令。
我該如何解決? 如何讓我的 Bot 向每個組發送消息?
編輯:好的,我稍微更改了代碼,我現在正在使用它:好的,這是我的新代碼,現在我遇到了一條錯誤消息,即發送不是函數
client.on("ready", () => {
console.log('Logged in as BunnyBot');
setInterval (function () {
client.guilds.forEach(() => { //for each guild the bot is in
let defaultChannel = "";
client.guilds.forEach((channel) => {
if(channel.type == "text" && defaultChannel == "") {
if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
defaultChannel = channel;
}
}
})
message.defaultChannel.send("Message here"); //send it to whatever channel the bot has permissions to send on
console.log("Sending Messages");
})
}, 1 * 1000);
})
問題:現在我收到一條錯誤消息,即發送不是函數。
暫時先忽略#general-channel
情況,您可以通過執行以下操作來完成所有公會發布:
編輯:
const Discord = require("discord.js");
const bot = new Discord.Client();
bot.on("ready", () => {
bot.guilds.forEach((guild) => { //for each guild the bot is in
let defaultChannel = "";
guild.channels.forEach((channel) => {
if(channel.type == "text" && defaultChannel == "") {
if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
defaultChannel = channel;
}
}
})
setInterval (function () {
defaultChannel.send("Message here") //send it to whatever channel the bot has permissions to send on
}, 30 * 1000);
})
})
此代碼應該發送到您的機器人所在的所有公會,您想要的消息以及您想要的時間間隔,盡管可能不是您想要的頻道。
node.js 版本 12.x 更改了一些內容,要修復您的代碼,您需要編輯一些內容:
bot.guilds.cache.forEach((guild) => { //for each guild the bot is in
let defaultChannel = "";
guild.channels.cache.forEach((channel) => {
if(channel.type == "text" && defaultChannel == "") {
if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
defaultChannel = channel;
}
}
})
setInterval (function () {
defaultChannel.send("Message here") //send it to whatever channel the bot has permissions to send on
}, 5000);})
只是一個簡單的解決方案,可能不是最好的解決方案,但它是一個解決方案。
while(0 == 0) {
channel = client.channels.cache.get('channel-id');
channel.send("message you want to sent")
sleep.sleep(seconds);
}
您將需要稱為 sleep 的 npm 包
npm 安裝睡眠
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.