【问题标题】:TypeError: channel.join is not a function类型错误:channel.join 不是函数
【发布时间】:2021-03-28 10:23:39
【问题描述】:

您好,我有点卡在这里,我试图让我的机器人加入 VC,但它返回:

(node:847) UnhandledPromiseRejectionWarning: TypeError: channel.join is not a function

我们将不胜感激:)

client.on("ready", () => {
  const channel = client.channels.cache.get("757004209701912587");
  if (!channel) return console.error("The channel does not exist!");
  channel.join().then(connection => {
    // Yay, it worked!
    console.log("Successfully connected.");
  }).catch(e => {
    // Oh no, it errored! Let's log it to console :)
    console.error(e);
  });
});

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    为了获得一个有效的频道,你首先需要从你的客户那里获得一个公会。

    const guild = client.guilds.cache.get("YOUR_GUILD_ID");
    

    然后您可以将代码更改为:

      const channel = guild.channels.cache.get("757004209701912587");
    

    那么它应该可以工作了。

    【讨论】:

    • “初始化前无法访问‘公会’”client.on("ready", () => { const channel = guild.channels.cache.get("757004209701912587"); if (!channel) return console.error("The channel does not exist!"); const guild = client.guilds.cache.get("750063752149205222"); channel.join().then(connection => { console.log("Successfully connected."); }).catch(e => { console.error(e); }); });
    • 你没有声明公会变量
    【解决方案2】:

    显然它真的不是一个函数。 在遇到同样的问题并搜索了一段时间无济于事后,我偶然发现了这个示例代码:https://github.com/discordjs/voice/tree/main/examples/radio-bot

    它使用完全不同的原理,但通过将其中的一部分集成到我的代码中,我成功地创建了语音连接。

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    猜你喜欢
    • 2022-01-22
    • 2016-02-26
    • 2013-04-01
    • 2016-03-06
    • 2018-04-28
    相关资源
    最近更新 更多