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

wsxa1bj1  于 2023-01-08  发布在  Java
关注(0)|答案(3)|浏览(138)

你好,我如何发送消息到第一个通道,机器人可以在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!`);
});
yrdbyhpb

yrdbyhpb1#

你可以这样做。

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}`);
  }
});
jecbmhm3

jecbmhm32#

更新了不一致版本13
请注意,c.type已从text更改为GUILD_TEXT

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

client.on("guildCreate", (guild) => {
  const channel = guild.channels.cache.find(
    (c) => c.type === "GUILD_TEXT" && c.permissionsFor(guild.me).has("SEND_MESSAGES")
  );
  // Do something with the channel
});
kr98yfug

kr98yfug3#

从v13更新为v14
guildCreate的事件现在为Events.GuildCreate
c.typeGUILD_TEXT更改为ChannelType.GuildText
guild.me现在是guild.members.me

const { Events, ChannelType } = require('discord.js')

const client = new Client({
//your intents here
intents: []
})

client.on(Events.GuildCreate, guild => {
    const channel = guild.channels.cache.find(c =>
        c.type === ChannelType.GuildText &&
        c.permissionsFor(guild.members.me).has('SEND_MESSAGES')
    )
    //do stuff with the channel
})

相关问题