discord.js v12将消息发送到第一个通道

时间:2020-06-08 12:46:15

标签: javascript

嗨,我如何将消息发送到第一个通道,在该通道中bot可以在Discord.js v12中发送消息。 请帮我。 这对我没有用:

client.on("guildCreate", guild => {
    let channelID;
    let channels = guild.channels;
    channelLoop:
    for (let c of channels) {
        let channelType = c[1].type;
        if (channelType === "text") {
            channelID = c[0];
            break channelLoop;
        }
    }


    let channel = client.channels.get(guild.systemChannelID || channelID);
    channel.send(`Thanks for inviting me into this server!`);
});

1 个答案:

答案 0 :(得分:1)

您可以这样做。

const Discord = require("discord.js");
const client = new Discord.Client();

client.on("guildCreate", (guild) => {
  const channel = guild.channels.cache.find(
    (c) => c.type === "text" && c.permissionsFor(guild.me).has("SEND_MESSAGES")
  );
  if (channel) {
    channel.send(`Thanks for inviting me into this server!`);
  } else {
    console.log(`can\`t send welcome message in guild ${guild.name}`);
  }
});
相关问题