无法在获取的频道中发送消息 |不和谐.js

Posted

技术标签:

【中文标题】无法在获取的频道中发送消息 |不和谐.js【英文标题】:Unable to send message in a fetched channel | Discord.js 【发布时间】:2021-05-04 20:44:37 【问题描述】:

目的:向公会所有者发送消息,然后创建邀请,然后将邀请发送到指定频道。

代码

client.on("guildCreate", async guild => 
    // This event triggers when the bot joins a guild.
    console.log(``);
    console.log(greenBright(`[GUILD JOINED] $guild.name | [ID] $guild.id | [ (+) MEMBERCOUNT: $guild.memberCount]`));
    console.log(``);

    const Owner = guild.ownerID;
    const fetchOwner = client.users.fetch(Owner);
    const InviteLogGuild = client.guilds.cache.get("761134473395306507")
    const InviteLogChannel = InviteLogGuild.channels.cache.find(ch => ch.id === "803512574573543454");
    const getChennel = guild.channels.cache.first();

    (await fetchOwner).send(`Thank you for allowing lovell to  be added to your server, $guild.name, $(await fetchOwner).username !\nPrefix:\`$prefix\`\nAll commands are displayed by saying \`$prefixhelp\`\nFeel free to join lovell server: $MainServer`)
        .then(console.log(cyanBright(`[GUILD OWNER MESSAGED]`)))
        .catch(() => 
            console.error(redBright(`[GUILD OWNER MESSAGE DEINIED]`));
        );

    if (!getChennel) 
        return console.error(redBright('[CHANNEL COULD NOT BE FETCHED]'));
     else 
        console.log(yellow('[CHANNEL FETCHED]'))
    

    const link = getChennel.createInvite(
        unique: true,
        maxAge: 0,
        temporary: false,
        maxUses: 100
    )

    if (!link) 
        return console.log(redBright(`[INVITE FAILED]`));
     else 
        console.log(blueBright(`[INVITE INITIATED]`));
    

    if (!InviteLogGuild) 
        console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`))
     else if (InviteLogGuild || !InviteLogChannel) 
        console.error(red(`\n[LOG GUILD IDENTIFIED | CHANNEL UNIDENTIFIED]\n`))
     else if (!InviteLogChannel) 
        console.error(red(`\n[LOG CHANNEL NOT PRESENT]\n`));
     else if (InviteLogChannel) 
        console.log(cyanBright(`[LOGGING INVITE]`));
        const LogChannel = new Discord.MessageEmbed()
            .setTitle(`New Guild Joined:`)
            .setDescription(`**Guild Name:** $guild.name \n\n**MemberCount:** $guild.memberCount \n\n**Onwer:** \`$(await fetchOwner).tag\` \n\n**Invite Link:** $(await link).url`)
            .setThumbnail(`$guild.iconURL( dynamic: true )`);
        InviteLogChannel.send(LogChannel);
        console.log(cyan(`[INVITE LOGGED]\n`));
    

);

输出/错误:

[GUILD JOINED] Ghoulish Hangout | [ID] 618894612769275959 | [ (+) MEMBERCOUNT: 23]

[GUILD OWNER MESSAGED]
[CHANNEL FETCHED]
[INVITE INITIATED]

[LOG GUILD IDENTIFIED | CHANNEL UNIDENTIFIED] <---- Error

错误可能伴随:

(node:12052) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Channel

我尝试了多种方法来获取不和谐频道,但它不会发送消息或识别频道。任何帮助将不胜感激;)

【问题讨论】:

您确定 ID 正确吗? 是的,我非常确定 【参考方案1】:

我不太清楚为什么这会奏效,但我从 if 语句中取出了我的消息命令并且它奏效了。

    if (!InviteLogGuild) 
        return console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`));
     else if (InviteLogGuild) 
        console.log(greenBright(`[LOG GUILD IDENTIFIED]`));
     else if (!InviteLogChannel) 
        return console.error(red(`\n[LOG CHANNEL NOT PRESENT]\n`));
     else if (InviteLogChannel) 
        console.log(cyanBright(`[LOG CHANNEL IDENTIFIED]`));
     

    console.log(cyanBright(`[LOGGING INVITE]`));
    const SendMessage = new Discord.MessageEmbed()
        .setTitle(`New Guild Joined:`)
        .setDescription(`**Guild Name:** $guild.name \n\n**MemberCount:** $guild.memberCount \n\n**Onwer:** \`$(await fetchOwner).tag\` \n\n**Invite Link:** $(await link).url`)
        .setThumbnail(`$guild.iconURL( dynamic: true )`);
    InviteLogChannel.send(SendMessage);
    console.log(cyan(`[INVITE LOGGED]\n`));

结果:

[GUILD JOINED] test | [ID] 2389372187328783 | [ (+) MEMBERCOUNT: 9]

[GUILD OWNER MESSAGED]
[CHANNEL FETCHED]
[INVITE INITIATED]
[LOG GUILD IDENTIFIED]
[LOGGING INVITE]
[INVITE LOGGED]

如果有人可以解释这是为什么,请随意!

【讨论】:

【参考方案2】:

编辑:

你的 if 语句是错误的:

if (!InviteLogGuild) 
  console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`))
 else if (InviteLogGuild || !InviteLogChannel) 

这是错误的,它总是会进入这种情况,因为您正在检查 InviteLogGuild 是否已定义 InviteLogChannel 未定义。由于第一种情况已经检查了要定义的 InviteLogGuild,所以这将始终返回 true。要使其正常工作,您必须将其更改为:

if (!InviteLogGuild) 
  console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`))
 else if (InviteLogGuild && !InviteLogChannel) 

但是您可以完全删除 &amp;&amp;,因为第一种情况已经检查了 InviteLogGuild 是否已定义:

if (!InviteLogGuild) 
  console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`))
 else if (!InviteLogChannel) 

如果您考虑到这些因素,您最终的“if-chain”将如下所示(并且有效):

if (!InviteLogGuild) 
  console.error(red(`\n[LOG GUILD NOT IDENTIFIED]\n`));
 else if (!InviteLogChannel) 
  console.error(red(`\n[LOG GUILD IDENTIFIED | CHANNEL UNIDENTIFIED]\n`));
 else 
  console.log(cyanBright(`[LOGGING INVITE]`));
  const LogChannel = new Discord.MessageEmbed()
    .setTitle(`New Guild Joined:`)
    .setDescription(`**Guild Name:** $guild.name \n\n**MemberCount:** $guild.memberCount \n\n**Onwer:** \`$(await fetchOwner).tag\` \n\n**Invite Link:** $(await link).url`)
    .setThumbnail(`$guild.iconURL( dynamic: true )`);
  InviteLogChannel.send(LogChannel);
  console.log(cyan(`[INVITE LOGGED]\n`));

为避免此类问题,我建议您改用 TypeScript 或开始使用 ESLint,这可以建议您进行不必要的检查。

旧答案

尝试使用guild.channels.resolve(channelId)client.guilds.resolve(guildId) 而不是使用缓存。可能是频道还没有加载到缓存中,找不到。

【讨论】:

仍然出现同样的错误。我尝试了不同的频道 ID,但仍然是同样的错误;(

以上是关于无法在获取的频道中发送消息 |不和谐.js的主要内容,如果未能解决你的问题,请参考以下文章

如何让bot在反应后将消息发送到另一个频道|不和谐.js

如何使不和谐机器人将消息发送到不和谐 Node js 的特定频道机器人

如何在不和谐 js 中使用消息获取?

机器人不发送标签 |不和谐.js

如何使用 dhooks 查找频道中发送的最新消息

尝试向多个 Discord 频道发送消息