[英]Javascript Discord Bot giving code reference errors when ran
我最近一直在开发一款不和谐的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!")
}
});
当我运行上述代码时,它返回一个错误,(我认为)基本上告诉我, let GeneralChannel = server.channels.find("General", "Bot")
中的“服务器”未定义。 我的问题是,我实际上不知道如何定义服务器。 我假设当我为其定义服务器时,它也将告诉我我需要定义通道并查找,尽管我不确定。
提前致谢 :)
首先,你为什么要同时使用let
和var
? 无论如何,如错误所示, 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!")
}
});
在这种情况下,范围不是一个问题,但是值得注意的是,“让”定义局部变量,而“ var”定义全局变量。 它们是有区别的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.