You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord批量移动频道至分类命令异常问题求助

修改后的Discord批量移动频道命令

以下是调整后的代码,支持批量指定多个频道(通过频道提及或ID)移动到目标分类,分类不存在时自动创建:

const Discord = require("discord.js");

const client = new Discord.Client({
    intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_MEMBERS", "MESSAGE_CONTENT"],
    partials: ["CHANNEL", "MESSAGE"]
});

const token = "your-bot-token-here"; // 替换为你的机器人令牌

client.on('ready', async () => {
    console.log(`客户端已启动!${client.user.username}`);
});

client.on('messageCreate', async (message) => {
    if (!message.content.startsWith('!category')) return;

    // 解析命令参数,过滤空字符串
    const args = message.content.split(' ').filter(arg => arg.trim() !== '');
    if (args.length < 2) {
        return message.channel.send('用法:!category <分类名称> [频道提及/ID1] [频道提及/ID2] ...\n如果不指定频道,默认移动当前频道');
    }

    // 提取分类名称(若需支持带空格的分类名,可改用引号包裹逻辑,此处默认单词分类名)
    const categoryName = args[1];
    // 提取频道参数,若无则使用当前频道
    const channelArgs = args.slice(2).length > 0 ? args.slice(2) : [message.channel.id];

    // 查找或创建目标分类
    let category = message.guild.channels.cache.find(c => c.name === categoryName && c.type === 'GUILD_CATEGORY');
    if (!category) {
        try {
            category = await message.guild.channels.create(categoryName, { type: 'GUILD_CATEGORY' });
            message.channel.send(`分类「${categoryName}」不存在,已自动创建`);
        } catch (err) {
            return message.channel.send(`创建分类失败:${err.message}`);
        }
    }

    // 批量处理频道移动
    const successChannels = [];
    const failedChannels = [];

    for (const channelArg of channelArgs) {
        // 兼容频道提及格式(<#频道ID>)和直接ID
        const channelId = channelArg.match(/<#(\d+)>/) ? channelArg.match(/<#(\d+)>/)[1] : channelArg;
        const channel = message.guild.channels.cache.get(channelId);

        if (!channel) {
            failedChannels.push(channelArg);
            continue;
        }

        // 排除分类频道本身,只处理可移动的频道类型
        if (!['GUILD_TEXT', 'GUILD_VOICE', 'GUILD_NEWS', 'GUILD_STAGE_VOICE'].includes(channel.type)) {
            failedChannels.push(`${channelArg}(非可移动频道类型)`);
            continue;
        }

        try {
            await channel.setParent(category.id);
            successChannels.push(channel.name);
        } catch (err) {
            failedChannels.push(`${channelArg}(移动失败:${err.message})`);
        }
    }

    // 发送最终结果反馈
    let resultMsg = `批量移动完成!\n✅ 成功移动:${successChannels.length > 0 ? successChannels.join(', ') : '无'}`;
    if (failedChannels.length > 0) {
        resultMsg += `\n❌ 移动失败:${failedChannels.join(', ')}`;
    }
    message.channel.send(resultMsg);
});

client.login(token);

核心修改点

  • 参数扩展:支持同时传入多个频道参数,未指定时默认移动当前消息所在频道
  • 频道识别兼容:同时支持#频道提及和纯频道ID两种指定方式
  • 批量处理:循环处理每个目标频道,分别记录成功/失败状态,最后统一反馈
  • 错误防护:添加分类创建失败、频道不存在、频道类型不支持等场景的错误提示
  • 体验优化:明确命令用法提示,分类创建时告知用户,结果清晰展示移动状态

内容的提问来源于stack exchange,提问作者Candy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 12:45:15