Can't post message to just created channel

I am trying to make a bot which will create a channel called ticket-#### (#### being 4 random digits), and then make that channel so only the person who used the command and admins in the server can see, which works fine. However, i want text after the command (in this case +create) to be posted in this new channel. I.E: If i use +create I need help with Discord, it would make a private channel between that user and the admins and the bot would send a message in that channel which is I need help with discord. I keep getting the error TypeError: Cannot read property 'send' of undefined. This is my create.js code:

  name: "create",
  description: "Starts the channel",
  execute(message, args, Discord, client) {
    var fourRD = Math.floor(1000 + Math.random() * 9000);

    const Embed = new Discord.MessageEmbed()
      .setColor("#1e2240")
      .setTitle("Ticket Created!")
      .setDescription(
        "Your ticket is #ticket-" +
          fourRD +
          ". You can use this to get in touch with developers who will answer your questions."
      );

    message.channel.send(Embed);

    message.guild.channels.create(`ticket-` + fourRD, {
      type: "text",
      permissionOverwrites: [
        {
          id: message.author.id,
          allow: ["VIEW_CHANNEL"]
        },
        {
          id: message.guild.id,
          deny: ["VIEW_CHANNEL"]
        }
      ]
    });
    const newchannel = client.channels.cache.find(
      channel => channel.name === "ticket" + fourRD
    );
    newchannel.send(args[0]);
  }
};

Tysm <3

Yo, discord.js is asynchronous which means when you create a channel you will get a promise. The promise basically says “ok I’ll do this when I have time”. You will want to make the execute function asynchronous first by putting async before execute and and await before message.guild.channels.create to wait for the channel to be created.

1 Like

This isn’t really working either! I get the error:

at Object.execute (/app/commands/create.js:36:16)
    at processTicksAndRejections (internal/process/task_queues.js:88:5)
(node:4851) 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(). (rejection id: 1)
(node:4851) [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.```

Thanks!

Can I see the entire error, one of the operations must be failing, perhaps the bot failed to create the channel or the message failed to send(most likely the first)

1 Like

My code is: create.js – glitchy-discord

And the error: (node:157) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined at Object.execute (/app/commands/create.js:36:16) at processTicksAndRejections (internal/process/task_queues.js:88:5) (node:157) 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(). (rejection id: 1) (node:157) [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.

ill try to help as much as possible when i arrive home in an hour :slight_smile:

1 Like

When the new channel is created it is not nesscarily in the cache, I think you need to check the docs and see if that’s the behavior or not.
according to the below link, you should do it this way

First of all, you’re creating a channel called ticket-XXXX, but trying to find a channel called ticketXXXX.
Secondly, it would be better if you refactored the code to this:

  name: "create",
  description: "Starts the channel",
  async execute(message, args, Discord, client) {
    var fourRD = Math.floor(1000 + Math.random() * 9000);

    const Embed = new Discord.MessageEmbed()
      .setColor("#1e2240")
      .setTitle("Ticket Created!")
      .setDescription(
        "Your ticket is #ticket-" +
          fourRD +
          ". You can use this to get in touch with developers who will answer your questions."
      );

    message.channel.send(Embed);
    const newchannel = await message.guild.channels.create("ticket-" + fourRD, {
      type: "text",
      permissionOverwrites: [
        {
          id: message.author.id,
          allow: ["VIEW_CHANNEL"]
        },
        {
          id: message.guild.id,
          deny: ["VIEW_CHANNEL"]
        }
      ]
    });
    newchannel.send(args[0]);
  }
};
5 Likes

YES! This code works ABSOLUTLEY fine! Thank you SO MUCH for your help! Have an amazing day!! :grinning:

1 Like