简体   繁体   English

DIscord.js UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body

[英]DIscord.js UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body

I have an issue with my code.我的代码有问题。

if (command === 'channel'){
    if (args.length == 0 ){
        return message.channel.send(Aucun argument trouvé)
    }
    await message.guild.channels
    .create(args[0] ,{
        type : 'text' ,
    })

    .then((chan)=>{
    var data = fs.readFileSync('test.json')
    var parsedata = JSON.parse(data)



    var test = 0
    console.log(parsedata['category'])

    try {
        chan.setParent( parsedata['category'])
    }catch{

        message.channel.send("il s'emblerais que la commande category est mal été configuré ")
        channel.delete()
        console.log("end")
        return

    }


    });
    message.channel.send("channel "+args[0]+" crée :)")

};

I'm trying to create a channel and move it to a category.我正在尝试创建一个频道并将其移至一个类别。 The category ID is stored in a file called test.json .类别 ID 存储在名为test.json的文件中。

My problem is if the ID stored does not exist then the try-catch should stop the error and execute the code in the catch block.我的问题是,如果存储的 ID 不存在,那么 try-catch 应该停止错误并执行catch块中的代码。 but that is not the case.但事实并非如此。

Here is the error:这是错误:

node:15548) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
parent_id: Category does not exist
    at RequestHandler.execute (c:\Users\etoile\Desktop\ts bot js\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async RequestHandler.push (c:\Users\etoile\Desktop\ts bot js\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
    at async TextChannel.edit (c:\Users\etoile\Desktop\ts bot js\node_modules\discord.js\src\structures\GuildChannel.js:355:21)
(Use node --trace-warnings ... to show where the warning was created)
<node_internals>/internal/process/warning.js:42
(node:15548) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
<node_internals>/internal/process/warning.js:42
(node:15548) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Thank for your help in advance提前感谢您的帮助

You receive the error as the parent category doesn't exist.您收到错误,因为父类别不存在。 If you want to catch this error, you'll need to use the await keyword in front of chan.setParent() (as the .setParent() method is asynchronous):如果你想捕捉这个错误,你需要在chan.setParent()前面使用await关键字(因为.setParent()方法是异步的):

if (command === 'channel') {
  if (args.length === 0) return message.channel.send('Aucun argument trouvé');

  try {
    let chan = await message.guild.channels.create(args[0], {
      type: 'text',
    });

    let data = fs.readFileSync('test.json');
    let parsedata = JSON.parse(data);

    await chan.setParent(parsedata['category']);

    message.channel.send(`channel ${args[0]} crée :)`);
  } catch {
    message.channel.send(
      "il s'emblerais que la commande category est mal été configuré",
    );
    // not sure what channel you want to delete here
    channel.delete();
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 discord.js 嵌入:UnhandledPromiseRejectionWarning:DiscordAPIError:无效的表单主体 embed.fields[0].value:长度必须为 1024 或更少 - discord.js embeds: UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.fields[0].value: Must be 1024 or fewer in length DiscordAPIError:无效的表单正文 - Discord.js 嵌入 - DiscordAPIError: Invalid Form Body - Discord.js Embed Discord.js DiscordAPIError:无效的表单正文问题 - Discord.js DiscordAPIError: Invalid Form Body issue Discord.js DiscordAPIError: Invalid Form Body 0.permissions: 长度必须为 10 或更少 - Discord.js DiscordAPIError: Invalid Form Body 0.permissions: Must be 10 or fewer in length Discord.js - DiscordAPIError: Invalid Form Body embed.fields[0].value: This field is required Error - Discord.js - DiscordAPIError: Invalid Form Body embed.fields[0].value: This field is required Error Discord.js UnhandledPromiseRejectionWarning:DiscordAPIError:无法发送空消息 - Discord.js UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message Discord.JS 命令中的表单主体无效 - Discord.JS Invalid Form Body in command DiscordAPIError:无效的表单正文 - Discord 斜杠命令 - DiscordAPIError: Invalid Form Body - Discord slash commands Discord bot - DiscordAPIError [50035]:无效的表单正文 - Discord bot - DiscordAPIError[50035]: Invalid Form Body UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body content: 长度必须小于等于 4000 - UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body content: Must be 4000 or fewer in length
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM