[英]discord.js how to send message when bot joins to a new server without system message channel?
我正在尝试在加入声明后与机器人一起发送消息。
//Will post message when bot joined new server because system channel exists.
if(guild.systemChannelId != null) return guild.systemChannel.send("Thank you for invite!"), console.log('Bot Joined new server!')
//Send post join message when there is no system channel on the server to possible channel (not working)
client.guilds.cache.forEach((channel) => {
if(channel.type == "text") {
if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
channel.send("Thanks for inviting me")
console.log('Bot Joined new server!')
}
}
})
guild.systemChannel.send("Thank you for invite!")
这个工作并将消息发送到“默认系统通道”。 问题是如果服务器没有系统通道,这将显示为错误。 所以我必须在上面创建语句并仅在系统通道存在时使用此请求。
错误: Unhandled rejection TypeError: Cannot read property 'send' of null
-> 在没有语句的代码中仅使用guild.systemChannel.send("Thank you for invite!")
时会出现错误。
如果服务器上不存在系统通道,如何将消息发送到第一个通道?
您只需访问第一个缓存的文本通道,您可以通过访问缓存和过滤文本通道来实现此目的 bot 有权发送消息,如下所示:
client.on('guildCreate', (g) => {
const channel = g.channels.cache.find(channel => channel.type === 'GUILD_TEXT' && channel.permissionsFor(g.me).has('SEND_MESSAGES'))
channel.send("Thank you for inviting me!")
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.