我正尝试使用下面显示的代码检查频道主题,但它一直给我TypeError:无法读取未定义的属性“ includes”,我希望有人帮助我。谢谢!
我尝试过更改功能并研究include方法,但找不到与discord.js相关的任何内容
const guild = message.guild;
const channel = message.guild.channels;
function checking(channel) {
channel.topic.includes(message.author.id)
}
const channelOpen = guild.channels.some(checking)
if(channelOpen === true) return message.channel.send("You already have a ticket open!")```
Check the channel topic for the author of the message's ID and if it finds their ID in a topic, return with a message.
答案 0 :(得分:0)
message.guild.channels
没有topic
属性,这就是为什么出现TypeError的原因。您可以改用find()
方法,如下所示:
const checking = message.guild.channels.find(channel => channel.topic === message.author.id) || null
if (checking) return message.channel.send("You already have a ticket open!")
[...]
答案 1 :(得分:0)
因此,没有足够的信息来完全回答这个问题,但是我可以告诉您。它告诉您您正在尝试访问未定义对象上的属性。公会arr根本没有属性(message.author.id),这意味着它是未定义的。由于不存在该属性,因此无法使其具有任何类型的include属性。
您需要的是访问数组中的对象,而不是数组本身。诸如guild [0] .something.something之类的东西将引用实例化的对象。这仅适用于阵列。如果要在对象上执行此操作,则需要使用object1.hasOwnProperty('* Item以检查*') https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
const object1 = new Object();
object1.property1 = 42;
console.log(object1.hasOwnProperty('property1'));
// expected output: true
console.log(object1.hasOwnProperty('toString'));
// expected output: false
console.log(object1.hasOwnProperty('hasOwnProperty'));
const channel = message.guild.channels;
// You should log the guild object* to see Data. Please show data when
//posting question. For example message[0].guild.channel ?
function checking(channel) {
channel.topic.includes(message.author.id)
// I would write it like
// return channel.topic.hasOwnProperty(message.author.id)
}
//Checking is declared but never called ?
const channelOpen = guild.channels.some(checking)
//you did not post the guild object or arr, so not sure why you are calling
//some with the checking function ?
//I think what your trying to do is channelOpen = checking(channel) ?
if(channelOpen) {
// you dont need == true
return message.channel.send("You already have a ticket open!")
}
答案 2 :(得分:0)
var count = 0
message.guild.channels.forEach(i => {
if(count == 1) return; //If ID is already found.
if(i.type == 'voice') return; //If it's voice channel, return.
if(i.topic == message.author.id){ count++; return message.reply('You already have a ticket open!') }
//If ID is already found, increase the number to stop code from executing and message author about ticket.
});
我用的是: forEach loop
Increment上的 count ,您可以仅更改 var 和 let 变量。>
有关变量的更多信息:here。
如果我帮助过您,请将其标记为答案,这样我可以获得较高的声誉。谢谢<3