当我回复申请时,如何根据编号重新排序discord频道?

问题描述 投票:0回答:1

当有人申请服务器时,就会在服务器之上创建一个通道as shown here。但由于各种不同的因素,我对这些申请的回复顺序并不一致。当我回复他们时,频道就会关闭并移至a category Past Channels。当我响应应用程序并将其移动到类别时,该频道就会被放在类别频道列表的顶部,而我想根据应用程序编号对其进行排序。怎么办?

除其他事项外,这是我尝试的最后一件事。还是没用。顺便说一句,我正在用打字稿编码。

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);
        }
    });
}
typescript discord.js
1个回答
0
投票

如果我理解正确的话,您想根据频道数量对频道进行排序吗?如果所有频道都遵循相同的格式,请尝试这个。

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)
});
© www.soinside.com 2019 - 2024. All rights reserved.