【问题标题】:Discord Bot Node JS simple error .sendDiscord Bot Node JS简单错误.send
【发布时间】:2018-01-28 02:28:49
【问题描述】:

我在学习本教程时遇到了一个奇怪的问题。构建一个 JS Discord 机器人,实际上只有 33 行代码,并且它会抛出关于 .send 未定义的错误。我在 Google 上四处搜索,找不到任何有助于解决这个问题的方法。

const fs = require("fs");
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");

client.login(config.token);

client.on("ready", () => {
  client.user.setGame(`on ${client.guilds.size} servers`);
  console.log(`Ready to serve on ${client.guilds.size} servers, for ${client.users.size} users.`);
});

client.on("guildMemberAdd", (member) => {
    console.log(`New User ${member.user.username} has joined ${member.guild.name}` );
    member.guild.defaultChannel.send(`${member.user} has joined this server`);
});

client.on("message", (message) => {

    if (!message.content.startsWith(config.prefix) || message.author.bot) return;

    if (message.content.startsWith(config.prefix + "ping")) {
        message.channel.send("pong!");
    } else

    if (message.content.startsWith(config.prefix + "foo")) {
        message.channel.send("bar!");
    }
});

  client.on("error", (e) => console.error(e));
  client.on("warn", (e) => console.warn(e));
  client.on("debug", (e) => console.info(e));

运行时,console.log 可以正常工作,但默认通道的消息会在 PowerShell 中引发以下错误

C:\Users\super\Desktop\autoslap\mybot.js:18
        member.guild.defaultChannel.send(`${member.user} has joined this server`);
                                   ^

TypeError: Cannot read property 'send' of undefined

任何帮助都将不胜感激,对可能如此简单的事情感到沮丧。

【问题讨论】:

    标签: javascript discord


    【解决方案1】:

    如果服务器的默认频道已被删除,您将收到该错误。以前,您无法删除默认频道,但现在可以了。可以肯定的是,创建一个新服务器并在那里尝试。

    【讨论】:

    • 我正在开发一个全新的服务器,只有我和机器人
    • 你确定这与服务器上的权限无关?
    • 我认为这实际上与我的安装在一些额外的测试后被破坏有关。
    【解决方案2】:

    我知道这是一个迟到的回复,你可能已经想出了如何做到这一点,但我仍会向可能需要帮助的人解释。

    截至 2017 年 3 月 8 日,公会中不再有默认频道 不和谐。 #general 默认频道可以删除, guild.defaultChannel 属性不再起作用 - 从 https://anidiots.guide/frequently-asked-questions.html

    如果您想要一个替代方案,https://anidiots.guide/frequently-asked-questions.html 的代码可能会成功。只需进入网站并向下滚动,直到看到默认频道!

    如果您的机器人具有管理员权限,则其“第一个可写通道”位于顶部。它可以是任何频道,因此如果他们的默认频道被删除,您可能会惹恼很多人。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-07-16
      • 2021-04-12
      • 2022-09-28
      • 2018-08-10
      • 1970-01-01
      • 2022-12-01
      • 2017-11-05
      • 2019-11-10
      • 2018-05-02
      相关资源
      最近更新 更多