【发布时间】:2019-01-31 09:07:03
【问题描述】:
有人可以帮助我吗?我只是想让机器人在不请求的情况下向指定的频道发送消息,消息应该每 x 分钟发送一次,但我只想知道如何发送。
【问题讨论】:
-
欢迎来到 StackOverflow。到目前为止,您尝试过什么?
标签: node.js bots discord discord.js
有人可以帮助我吗?我只是想让机器人在不请求的情况下向指定的频道发送消息,消息应该每 x 分钟发送一次,但我只想知道如何发送。
【问题讨论】:
标签: node.js bots discord discord.js
您可以使用Client.guilds(客户所属的所有公会列表)和Guild.channels(公会拥有的所有频道列表)。
要指定公会和频道,您可以使用他们的 ID:如果您在 Discord 中启用开发者模式(在用户设置 > 外观 > 高级下),您可以右键单击复制公会、频道、用户等的 ID。
获得 ID 后,您可以使用 Collection.get() 获取它们。或者,您可以按名称.find() 他们,但这并不理想,因为名称可以更改。
这是一个例子:
let guild = client.guilds.get('your guild ID as a string here'), // returns a Guild or undefined
channel;
if (guild) {
channel = guild.channels.get('your channel ID as a string here');
if (channel) setInterval(() => {channel.send("Here you can put the message and stuffs.");}, 10 * 60 * 1000);
else console.log("There's no channel with that ID."),
} else console.log("There's no guild with that ID.");
这只是核心概念,你显然可以修改它。
【讨论】:
let guild = client.guilds.cache.get('your guild ID as a string here'), // 返回 Guild 或 undefined 频道;
如果(公会){ channel = guild.channels.cache.get('你的频道 ID 在这里作为字符串'); if (channel) setInterval(() => {channel.send("这里可以放消息和东西。");}, 10 * 60 * 1000); else console.log("没有该 ID 的频道。"), } else console.log("没有那个ID的公会");
【讨论】: