繁体   English   中英

如何让机器人在不使用命令的情况下向特定频道中的特定公会发送消息

[英]How do you make a bot send a message to an specific guild in a specific channel without using commands

有人可以帮助我吗? 我只是想让机器人在没有请求的情况下将消息发送到指定的频道,消息应该每 x 分钟发送一次,但我只想知道如何发送它。

您可以使用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 或未定义的频道;

if (guild) { channel = guild.channels.cache.get('你的频道 ID 作为一个字符串在这里'); if (channel) setInterval(() => {channel.send("这里你可以放消息和东西。");}, 10 * 60 * 1000); else console.log("没有这个ID的频道。"), } else console.log("没有这个ID的公会。");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM