Javascript Discord Bot在运行

时间:2017-06-21 07:20:23

标签: javascript bots discord

我最近一直在研究discord bot,这是我第一次编码,我认为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!")
  }
});

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

提前致谢:)

2 个答案:

答案 0 :(得分: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!")
    }
});

答案 1 :(得分:0)

在这种情况下,并非范围是一个问题,但值得注意的是'let'定义局部变量,而'var'定义全局变量。有区别。