【发布时间】:2021-11-03 23:18:16
【问题描述】:
我正在尝试在加入 with 语句时与 bot 发送消息。
//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!") 会出现错误。
如果服务器上不存在系统通道,如何将消息发送到可能的第一个通道?
【问题讨论】:
标签: node.js discord discord.js message channel