我正在创建一个discord bot,我想为它创建一个“warn”命令,当用户在discord服务器中行为不端时向他们发出警告。但是,当我运行代码并尝试命令时,它只进行了大约一半,弹出一条未处理的promise rejection消息,说我还没有定义message.channel.find()
函数,尽管我已经完全定义了它。
为了了解一些背景知识,我用javascript编写这个bot,更具体的说是Node.js。我也在使用discord.js库。我试过重新启动代码,重新输入括号内的内容,甚至试过几次重新格式化代码。但是,这些都没有奏效。我包含了我的代码,以帮助大家更好地理解我的问题。
// Here is where it starts to get shakey
let firstslice = message.content.slice(6);
let membertag = membertobewarned.tag;
let reason = firstslice.slice(membertag);
message.channel.send("Second debugging check, check!");
// Here is where it stops working, and the line below this text is where I
// am getting unhandled promise rejection errors from.
let warnchannel = message.channel.find(`name`, "logs");
if (message.author.bot) return;
if (!message.author.hasPermission("MANAGE_MESSAGES")) return message.channel.send("Sorry, but you do not have permission to use this command.");
if (!membertobewarned.hasPermission("MANAGE_MESSAGES")) return message.channel.send("Sorry, but you cannot warn this member.");
message.channel.send("Third debugging thing, check!");
let warnembed = new Discord.RichEmbed()
.setDescription("Warning")
.setColor("#FFA500")
.addField("User:", membertobewarned)
.addField("Warned By:", commanduser)
.addField("For Reason:", reason)
.addField("Time:", timewarned);
warnchannel.send(warnembed);
message.channel.send("I'm all done!");
应该发生的情况是,bot将获取消息发送的通道,要警告的人,谁警告了那个人,他们的提及以及警告的原因。
然后它将获取这些信息,并将其编译为不一致的richembed
,然后将其发送到#logs
通道。
但是,实际发生的情况是,当命令运行时,它获取所有信息,然后在检查#logs
通道的位置抛出错误:
UnhandledPromiseRejectionWarning TypeError: message.channel.find is not a function.
我希望能够找出为什么我得到这个错误,以及如何修复它。谢谢大家阅读这篇文章,我希望你们都能尽快帮助我,并且拥有一个美好的剩余的一天。
这是因为message.channel是一个对象,而不是数组或映射(在discord.js中它被称为集合)。
我不太确定你想要完成什么,但是如果你想要选择另一个频道,你需要使用
message.guild.channels.find('name', 'logs')
或更新的discord.js版本
message.guild.channels.find(channel => channel.name === 'logs')