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.
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.
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):
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();
}
}