【问题标题】:discord.js how to send message when bot joins to a new server without system message channel?当bot加入没有系统消息通道的新服务器时,discord.js如何发送消息?
【发布时间】: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


    【解决方案1】:

    您只需访问第一个缓存的文本通道,您可以通过访问缓存和过滤文本通道来实现此目的 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!")
    })
    

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 2021-01-09
      • 1970-01-01
      • 2021-11-12
      • 2021-11-28
      • 2021-08-18
      • 2012-07-15
      • 2020-06-13
      • 2018-07-27
      相关资源
      最近更新 更多