为什么 "message.channel.find" 不是函数?
Why is "message.channel.find" not a function?
我正在创建一个 discord 机器人,我想为它创建一个 "warn" 命令,以在 discord 服务器中的用户行为不当时警告他们。但是,当我 运行 代码并尝试命令时,它只进行了大约一半,弹出未处理的承诺拒绝消息,说我没有定义我的 message.channel.find()
函数,即使我已经完全定义了它。
对于某些背景,我在 javascript、node.js 中对这个机器人进行编码以便更具体。我也在使用 discord.js 库。我试过重新启动我的代码,重新输入括号内的内容,甚至尝试重新格式化几次。但是,其中 none 有效。我包含了我的代码以帮助大家更好地理解我的问题。
// 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!");
应该发生的是,机器人将获得消息发送的频道、警告谁、谁警告过那个人、他们的提及以及警告的原因。
然后它将获取该信息,并将其编译成一个 discord RichEmbed
,然后将其发送到 #logs
频道。
但是,实际发生的是当命令为 运行ning 时,它获取所有信息,然后在检查 #logs
通道的地方抛出错误:
UnhandledPromiseRejectionWarning TypeError: message.channel.find is not a function.
我希望能够弄清楚为什么会出现此错误,以及如何修复它。感谢大家阅读本文,我希望你们能尽快帮助我,并度过美好的一天。
那是因为 message.channel 是一个对象,而不是数组或 Map(在 Discord.js 中它被称为 Collection)。
我不太确定你想要完成什么,但如果你想select另一个频道,你需要使用任一
message.guild.channels.find('name', 'logs')
或较新的 Discord.js 版本
message.guild.channels.find(channel => channel.name === 'logs')
我正在创建一个 discord 机器人,我想为它创建一个 "warn" 命令,以在 discord 服务器中的用户行为不当时警告他们。但是,当我 运行 代码并尝试命令时,它只进行了大约一半,弹出未处理的承诺拒绝消息,说我没有定义我的 message.channel.find()
函数,即使我已经完全定义了它。
对于某些背景,我在 javascript、node.js 中对这个机器人进行编码以便更具体。我也在使用 discord.js 库。我试过重新启动我的代码,重新输入括号内的内容,甚至尝试重新格式化几次。但是,其中 none 有效。我包含了我的代码以帮助大家更好地理解我的问题。
// 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!");
应该发生的是,机器人将获得消息发送的频道、警告谁、谁警告过那个人、他们的提及以及警告的原因。
然后它将获取该信息,并将其编译成一个 discord RichEmbed
,然后将其发送到 #logs
频道。
但是,实际发生的是当命令为 运行ning 时,它获取所有信息,然后在检查 #logs
通道的地方抛出错误:
UnhandledPromiseRejectionWarning TypeError: message.channel.find is not a function.
我希望能够弄清楚为什么会出现此错误,以及如何修复它。感谢大家阅读本文,我希望你们能尽快帮助我,并度过美好的一天。
那是因为 message.channel 是一个对象,而不是数组或 Map(在 Discord.js 中它被称为 Collection)。
我不太确定你想要完成什么,但如果你想select另一个频道,你需要使用任一
message.guild.channels.find('name', 'logs')
或较新的 Discord.js 版本
message.guild.channels.find(channel => channel.name === 'logs')