Javascript Discord Bot在运行时给出代码参考错误
问题描述:
最近我一直在处理不一致的bot,这是我第一次编写代码,我认为Javascript比其他选项更容易。现在,我在错误后通过阅读错误挣扎。Javascript Discord Bot在运行时给出代码参考错误
反正,让我们来谈谈问题。目前,代码如下:
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")
是不确定的。我的问题是,我实际上不知道如何定义服务器。我假设当我定义服务器时,它也会告诉我我需要定义频道并找到,尽管我不确定。
感谢提前:)
答
首先,你为什么要使用都let
和var
?无论如何,如错误所述,server
未定义。客户端不知道你指的是什么服务器。这就是你的msg
对象进来,它有一个属性guild
这是服务器。
msg.guild;
其次,你想用let GeneralChannel = server.channels.find("General", "Bot")
实现什么?数组的find
方法需要一个函数。你是否试图寻找名称为“一般”或什么的渠道?如果是这样,最好以这种方式使用频道的ID,您可以使用任何bot的服务器中的频道(如果您尝试将所有建议发送到不同服务器上的特定频道)。
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!")
}
});
答
不,范围在这种情况下一个关注,但值得注意的是'let'定义了一个局部变量,而'var'定义了一个全局变量。它们是有区别的。
我认为op试图做的是将''SUGGESTION''消息发送到特定的“家庭”服务器,而不是它被触发的那个服务器,实质上是将所有反馈发送到个人服务器。 –
@DanF这是有道理的。编辑我的问题来涵盖 – Wright
我使用'let'和'var'的原因是因为两个不同的人在帮助我一点。一个使用'var'和一个使用'let',所以我都使用了,因为我太懒惰了改变一个到另一个。另外,谢谢你,你做了一个了不起的工作帮助。非常感激。 –