当有人申请服务器时,就会在服务器之上创建一个通道。但由于各种不同的因素,我对这些申请的回复顺序并不一致。当我回复他们时,频道就会关闭并移至。当我响应应用程序并将其移动到类别时,该频道就会被放在类别频道列表的顶部,而我想根据应用程序编号对其进行排序。怎么办?
除其他事项外,这是我尝试的最后一件事。还是没用。顺便说一句,我正在用打字稿编码。
function setPosition(client: myClient, channel: TextChannel) {
const regex = /\d{4}$/;
const appnum = Number(regex.exec(channel.name)![0]);
console.log(`[SLASH COMMANDS] Application #${appnum} was responded to.`);
const category = client.channels.cache.get(client.categoryPastApplications);
if (!category || category.type !== ChannelType.GuildCategory) {
console.log(
`[SLASH COMMANDS] An error occured while executing command "application"! Code #6`
);
return;
}
const channelsCategory = category.children.cache.sort((a, b) => a.position - b.position);
channelsCategory.forEach((categoryChannel) => {
if (Number(regex.exec(categoryChannel.name)![0]) > appnum) {
channel.setPosition(categoryChannel.position + 1);
}
});
}
编辑:我尝试将功能切换到此,但仍然不起作用。
async function setPosition(client: myClient, channel: TextChannel) {
const regex = /\d{4}$/;
const appnum = Number(regex.exec(channel.name)![0]);
console.log(`[SLASH COMMANDS] Application #${appnum} was responded to.`);
const category = client.channels.cache.get(client.categoryPastApplications);
if (!category || category.type !== ChannelType.GuildCategory) {
console.log(
`[SLASH COMMANDS] An error occured while executing command "application"! Code #6`
);
return;
}
const channelsCategory = category.children.cache.sort((a, b) => a.position - b.position);
Object.values(channelsCategory).forEach(async (categoryChannel, index) => {
if (Number(regex.exec(categoryChannel.name)![0]) < appnum) {
await channel.setPosition(index + 1);
}
});
}
如果我理解正确的话,您想根据频道数量对频道进行排序吗?如果所有频道都遵循相同的格式,请尝试这个。
const category = client.channels.cache.get(client.categoryPastApplications);
let toSortArr = [];
let elements = category.children.cache.forEach((channel) => {
let channelName = channel.name.substring(channel.name.indexOf("app"));
toSortArr.push(channelName);
});
let sort = toSortArr.sort();
let sorted = category.children.cache.forEach((channel) => {
let channelName = channel.name.substring(channel.name.indexOf("app"));
let index = sort.indexOf(channelName);
channel.setPosition(index)
});