Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Discord.JS:如何获得公会的第一频道?_Node.js_Discord.js - Fatal编程技术网

Node.js Discord.JS:如何获得公会的第一频道?

Node.js Discord.JS:如何获得公会的第一频道?,node.js,discord.js,Node.js,Discord.js,我正在创建一个不和谐机器人。我想按公会ID创建邀请。创建邀请需要一个频道。我想选择第一个频道。我该怎么做 我使用了这个,但不起作用: guild.channels[Object.key(guild.channels)[0]] //返回未定义的 我不知道你说的第一频道是什么意思,但你可以用这个: const randomChannel = (guild) => { guild.channels.random().then(channel => { if (cha

我正在创建一个不和谐机器人。我想按公会ID创建邀请。创建邀请需要一个频道。我想选择第一个频道。我该怎么做

我使用了这个,但不起作用:

guild.channels[Object.key(guild.channels)[0]]
//返回未定义的

我不知道你说的第一频道是什么意思,但你可以用这个:

const randomChannel = (guild) => {
    guild.channels.random().then(channel => {
        if (channel.type === 'text') return channel;
        else return randomChannel(guild);
    }
}

我不知道你说的第一频道是什么意思,但你可以用这个:

const randomChannel = (guild) => {
    guild.channels.random().then(channel => {
        if (channel.type === 'text') return channel;
        else return randomChannel(guild);
    }
}

如果你的意思是首先按位置,你可以使用帮会频道集合按类型和位置查找频道

const channel = guild.channels.filter(c => c.type === 'text').find(x => x.position == 0);

如果你的意思是首先按位置,你可以使用帮会频道集合按类型和位置查找频道

const channel = guild.channels.filter(c => c.type === 'text').find(x => x.position == 0);

要获得创建邀请的频道,您可能应该只使用Bot在中具有权限的频道,例如:

 const invitechannels = guild.channels.filter(c=> c.permissionsFor(guild.me).has('CREATE_INSTANT_INVITE'));
 invitechannels.random().createInvite()
     .then(invite=> console.log('Create Invite:\n' + invite.code))
或者,您也可以检查现有邀请:

guild.fetchInvites()
    .then(invites => console.log('Found Invites:\n' + invites.map(invite => invite.code).join('\n')))

要获得创建邀请的频道,您可能应该只使用Bot在中具有权限的频道,例如:

 const invitechannels = guild.channels.filter(c=> c.permissionsFor(guild.me).has('CREATE_INSTANT_INVITE'));
 invitechannels.random().createInvite()
     .then(invite=> console.log('Create Invite:\n' + invite.code))
或者,您也可以检查现有邀请:

guild.fetchInvites()
    .then(invites => console.log('Found Invites:\n' + invites.map(invite => invite.code).join('\n')))

每个通道都有一个
calculatedPosition
属性,可用于获取第一个通道

const channel=Array.from(guild.channels).sort((a,b)=>a.calculatedPosition-b.calculatedPosition)[0];
好的,让我们分析一下上面代码中发生的事情:

Array.from(guild.channels)//返回GuildChannels数组(Textchannels和Voicechannels)
.sort((a,b)=>a.calculatedPosition-b.calculatedPosition)//按其calculatedPosition属性升序对集合中的元素进行排序,并返回数组
[0]//返回数组的第一个元素

每个频道都有一个
calculatedPosition
属性,您可以使用该属性获取第一个频道

const channel=Array.from(guild.channels).sort((a,b)=>a.calculatedPosition-b.calculatedPosition)[0];
好的,让我们分析一下上面代码中发生的事情:

Array.from(guild.channels)//返回GuildChannels数组(Textchannels和Voicechannels)
.sort((a,b)=>a.calculatedPosition-b.calculatedPosition)//按其calculatedPosition属性升序对集合中的元素进行排序,并返回数组
[0]//返回数组的第一个元素
对于v12,请尝试

var chx = guild.channels.cache.filter(chx => chx.type === "text").find(x => x.position === 0);
对于v12,请尝试

var chx = guild.channels.cache.filter(chx => chx.type === "text").find(x => x.position === 0);

这不是用户要求的,他希望访问左侧频道列表中显示最高的频道,而不是随机频道。这不是用户要求的,他希望访问左侧频道列表中显示最高的频道,而不是随机频道。