【问题标题】:Javascript Discord Bot giving code reference errors when ranJavascript Discord Bot 在运行时给出代码参考错误
【发布时间】:2017-07-13 14:41:54
【问题描述】:

我最近一直在研究一个不和谐的机器人,这是我第一次进行一般的编码,我认为 Javascript 会比我可能拥有的其他选项更容易。现在,我正在努力阅读一个又一个错误。

无论如何,让我们来看看手头的问题。目前代码如下:

const Discord = require("discord.js");
const client = new Discord.Client();
const commando = require('discord.js-commando');
const bot = new commando.Client();
const prefix="^";

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  let short = msg.content.toLowerCase()

  let GeneralChannel = server.channels.find("General", "Bot")
if (msg.content.startsWith( prefix + "suggest")) {
  var args = msg.content.substring(8)
  msg.guild.channels.get(GeneralChannel).send("http\n SUGGESTION:" + msg.author.username + " suggested the following: " + args + "")
  msg.delete();
  msg.channel.send("Thank you for your submission!")
  }
});

当我运行上述代码时,它返回了一个错误(我认为)基本上告诉我let GeneralChannel = server.channels.find("General", "Bot") 中的“服务器”未定义。我的问题是,我实际上不知道如何定义服务器。我假设当我为它定义服务器时,它也会告诉我我需要定义频道并找到,尽管我不确定。

提前致谢:)

【问题讨论】:

    标签: javascript bots discord


    【解决方案1】:

    首先,您为什么要同时使用 letvar?无论如何,正如错误所说,server 没有定义。客户端不知道您指的是 什么 服务器。这就是你的msg 对象的来源,它有一个属性guild 是服务器。

    msg.guild;
    

    其次,你想用let GeneralChannel = server.channels.find("General", "Bot") 达到什么目的?数组的find 方法采用一个函数。您是否要查找名称为“General”或其他名称的频道?如果是这样,最好以这种方式使用频道的 ID,您可以使用机器人所在的任何服务器的频道(以防您尝试将所有建议发送到不同服务器上的特定频道)。

    let generalChannel = client.channels.find(chan => {
        return chan.id === "channel_id"
    })
    //generalChannel will be undefined if there is no channel with the id
    

    如果您尝试发送

    按照这个假设,您的代码可以重写为:

    const Discord = require("discord.js");
    const client = new Discord.Client();
    const commando = require('discord.js-commando');
    const bot = new commando.Client();
    const prefix="^";
    
    client.on('ready', () => {
        console.log(`Logged in as ${client.user.tag}!`);
    });
    
    client.on('message', msg => {
        let short = msg.content.toLowerCase();
    
        if (msg.content.startsWith( prefix + "suggest")) {
            let generalChannel = client.channels.find(chan => {
                return chan.id === 'channel_id';
            });
    
            let args = msg.content.substring(8);
    
            generalChannel.send("http\n SUGGESTION: " + msg.author.username + " suggested the following: " + args + "");
            msg.delete();
            msg.channel.send("Thank you for your submission!")
        }
    });
    

    【讨论】:

    • 我认为 op 试图做的是将"SUGGESTION" 消息发送到特定的“家庭”服务器而不是触发它的服务器,本质上是将所有反馈发送到个人服务器。
    • @DanF 那是有道理的。编辑了我的问题以涵盖该问题
    • 我使用letvar的原因是因为两个不同的人在帮助我一点。一个使用var,一个使用let,所以我两个都用了,因为我太懒了把一个变成另一个。另外,谢谢,你帮了大忙。非常感谢。
    • @BellaBlurryface 哦,我明白了,很高兴我能帮上忙!如果你能接受答案,那将不胜感激。
    【解决方案2】:

    在这个例子中,范围不是一个问题,但值得注意的是,'let' 定义了一个局部变量,而 'var' 定义了一个全局变量。有区别。

    【讨论】:

      猜你喜欢
      • 2020-09-27
      • 2022-11-27
      • 2020-03-24
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 2018-05-31
      • 2019-04-27
      • 2018-03-04
      相关资源
      最近更新 更多