Home > Software design >  How to check for a newly created channel's parentID on discord?
How to check for a newly created channel's parentID on discord?

Time:11-07

I'm trying to send a message to a specific channel (general) when a new channel is created in a specific category.

So far, I've gotten the bot to only respond when I create the channel in the correct category, and not in other ones. However, the bot then immediately crashes.

This is my code so far:

client.on("channelCreate", function(channel){
    if (client.channels.fetch(channel.parentID == '1037195863317020743')){
        const general = client.channels.cache.get('1038311273651240960');
        general.send("There's a new outline! Let's see what it turns out to be...");
    }
});   

The message does appear in the correct channel, but the bot then crashes. This is the error log:

C:\Users\whydo\OneDrive\Desktop\JS Discord Test\node_modules\@discordjs\rest\dist\index.js:659
        throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
              ^

DiscordAPIError[50035]: Invalid Form Body
channel_id[NUMBER_TYPE_COERCE]: Value "false" is not snowflake.
    at SequentialHandler.runRequest (C:\Users\whydo\OneDrive\Desktop\JS Discord Test\node_modules\@discordjs\rest\dist\index.js:659:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async SequentialHandler.queueRequest (C:\Users\whydo\OneDrive\Desktop\JS Discord Test\node_modules\@discordjs\rest\dist\index.js:458:14)
    at async REST.request (C:\Users\whydo\OneDrive\Desktop\JS Discord Test\node_modules\@discordjs\rest\dist\index.js:902:22)
    at async ChannelManager.fetch (C:\Users\whydo\OneDrive\Desktop\JS Discord Test\node_modules\discord.js\src\managers\ChannelManager.js:123:18) {
  requestBody: { files: undefined, json: undefined },
  rawError: {
    code: 50035,
    errors: {
      channel_id: {
        _errors: [
          {
            code: 'NUMBER_TYPE_COERCE',
            message: 'Value "false" is not snowflake.'
          }
        ]
      }
    },
    message: 'Invalid Form Body'
  },
  code: 50035,
  status: 400,
  method: 'GET',
  url: 'https://discord.com/api/v10/channels/false'
}

The only thing I can think of is that it's something wrong with how I'm sending the message, or how I'm checking the parentID. Then again, I am new to discord.js and the answer might be plainly obvious in the error log.

Note: This event triggers when a channel is created inside the server, not through JavaScript.

CodePudding user response:

channels.fetch accepts an ID, not a statement. By using channel.parentID == '1037195863317020743' inside fetch you're calling it with a boolean as it returns either true or false. Neither of those cases is valid as a boolean can't be a snoflake/ID. That's why you receive the error "channel_id[NUMBER_TYPE_COERCE]: Value "false" is not snowflake."

You don't even need to use fetch there. channel in your channelCreate is the newly created channel. You can check its parentId (not parentID) like this:

client.on('channelCreate', function (channel) {
  if (channel.parentId === '1037195863317020743') {
    const general = client.channels.cache.get('1038311273651240960');
    general.send("There's a new outline! Let's see what it turns out to be...");
  }
});
  • Related