【发布时间】:2021-05-17 08:41:35
【问题描述】:
我试图让我的机器人在更新时向它所在的所有服务器中的频道发送消息。我的想法是,每当我更新我的机器人时,我都会手动触发它。例如,我想让它发送一条消息,例如“将机器人更新到 2.03 版。更新日志:(更新中所做的更改)”。这是我目前拥有的代码:
const Discord = require('discord.js');
const client = new Discord.Client();
var Long = require("long");
const getChannel = (guild) => {
// get "original" default channel
if(guild.channels.cache.has(guild.id))
return guild.channels.cache.get(guild.id)
const jimmyChannel = guild.channels.cache.find(channel => channel.name === "jimmybot");
if (jimmyChannel)
return jimmyChannel;
return guild.channels.cache
.filter(c => c.type === "text" &&
c.permissionsFor(guild.client.user).has("SEND_MESSAGES"))
.sort((a, b) => a.position - b.position ||
Long.fromString(a.id).sub(Long.fromString(b.id)).toNumber())
.first();
}
// I found this as an example but I'm not sure if it will work
client.on("guildMemberAdd", member => {
const channel = getChannel(member.guild);
channel.send(`text`);
});
client.login('token');
我的问题是如何手动调用它而不是在用户加入服务器时发送消息?
【问题讨论】:
-
您可能想为此使用
message事件。检查作者是否是您,然后通过服务器映射并找到您要发送消息的频道。老实说,我不会这样做,因为您的机器人很容易受到速率限制(如果在很多服务器中)也可以发送许多可能导致您的机器人被禁止使用 Discord 的消息。您可以做些什么来刺激您的用户是在您的服务器中添加一个公告频道并要求他们订阅该频道。
标签: discord discord.js