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

Discord机器人addshop命令报错:parent_id为空非snowflake问题

问题:Discord机器人addshop命令触发DiscordAPIError报错

报错信息

[Shopify Sales Tracker/16:28:9]: [ERROR] ➜  uncaughtException => DiscordAPIError: Invalid Form Body
parent_id: Value "" is not snowflake.
    at RequestHandler.execute (C:\Users\Hp\Downloads\Shopify-Live-Sales-Tracker-doener\Shopify-Live-Sales-Tracker-doener\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (C:\Users\Hp\Downloads\Shopify-Live-Sales-Tracker-doener\Shopify-Live-Sales-Tracker-doener\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
    at async TextChannel.edit (C:\Users\Hp\Downloads\Shopify-Live-Sales-Tracker-doener\Shopify-Live-Sales-Tracker-doener\node_modules\discord.js\src\structures\GuildChannel.js:336:21)
    at async Object.module.exports.run (C:\Users\Hp\Downloads\Shopify-Live-Sales-Tracker-doener\Shopify-Live-Sales-Tracker-doener\src\client\commands\shopify\addshop.js:16:4)
    at async Object.module.exports.run (C:\Users\Hp\Downloads\Shopify-Live-Sales-Tracker-doener\Shopify-Live-Sales-Tracker-doener\src\client\events\messageCreate.js:29:3)

相关代码

addshop命令执行逻辑

module.exports.run = async (app, client, message, args) => {
    const shop_url = args[0];

    if (!app.shopify.config.shops.includes(shop_url)) {
        const server = message.guild;

        var webhook;

        if (!app.shopify.config.webhooks[shop_url]) {
            const channel_name = app.config.discord.shop_channel_name(shop_url);

            const channel = await server.channels.create(channel_name, {
                type: 'text',
            });

            await channel.setParent(app.config.discord.products_category);

            webhook = await channel.createWebhook(shop_url);
        }

        await app.utils.shopify.addNewShopToConfig(
            shop_url,
            webhook ? webhook.url : undefined
        );

        const embed = app.utils.discord.createEmbed('info', {
            title: `Shop hinzugefügt [${shop_url}]`,
            description: `\`\`${shop_url}\`\` wurde erfolgreich zur Datenbank hinzugefügt.`,
        });

        await app.utils.shopify.loadProducts();

        return await message.channel.send({ embeds: [embed] });
    } else {
        const embed = app.utils.discord.createEmbed('error', {
            description: 'Du hast diesen Shop bereits hinzugefügt',
        });

        return await message.channel.send({ embeds: [embed] });
    }
};

命令配置

module.exports.conf = {
    name: 'addshop',
    description: 'Füge einen Shop in die db hinzu',
    category: 'Shopify',

    owner: false,
    premium: false,
    admin: false,

    guild: true,
    dm: false,

    disabled: false,

    usage: ['addshop <url>'],
    example: ['addshop hoopsport.de'],

    aliases: ["add"],

    minArgs: 0,
    maxArgs: 0,
};

问题排查与解决方案

报错原因

报错明确指出parent_id: Value "" is not snowflake,说明调用channel.setParent()时传入的app.config.discord.products_category是空字符串,而Discord API要求分类频道的ID必须是有效的Snowflake格式(非空的数字字符串)。

修复步骤

  1. 修正配置值
    检查项目的配置文件,确保app.config.discord.products_category的值是Discord服务器中已存在的分类频道ID(一串纯数字),而非空字符串或无效内容。

  2. 添加参数校验与异常处理
    在设置频道分类前,先校验ID的有效性,避免配置错误导致命令崩溃:

    // 替换原有的channel.setParent代码
    const categoryId = app.config.discord.products_category;
    if (categoryId && /^\d+$/.test(categoryId)) {
        try {
            await channel.setParent(categoryId);
        } catch (err) {
            console.error('设置频道分类失败:', err);
            // 可选:给用户发送错误提示
            await message.channel.send('设置频道分类时出现错误,请检查配置的分类ID是否正确');
        }
    } else {
        console.warn('未配置有效的分类ID,将跳过频道分类设置');
    }
    
  3. 修正命令参数限制
    命令配置中的minArgs: 0和maxArgs: 0与实际需求矛盾(命令需要传入店铺URL),应修改为:

    minArgs: 1,
    maxArgs: 1,
    

    这样可以避免用户不传参数时,shop_url为undefined导致后续逻辑出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 19:18:38