简体   繁体   中英

channel.send is not a function discord.js

I am making a feedback command and I want to log the feedback in a specific channel, but I get this error: UnhandledPromiseRejectionWarning: TypeError: channel.send is not a function

Code:

const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
    name: "feedback",
    description: "Send your feedback for the bot!",
    async execute(message, args) {
        const feedback = args.slice(0).join(" ");
        const no_feedback = new Discord.MessageEmbed()
        .setColor("#993333")
        .setTitle("Feedback")
        .setDescription("An error has occured.")
        .addField("Error" , "You didn't type your feedback after the command!")
        .addField("What to do?" , "Type `=feedback` and add your feedback right after.")
        .setFooter(message.author.username)
        .setTimestamp();

        const confirm = new Discord.MessageEmbed()
        .setColor("#993333")
        .setTitle("Confirm")
        .setDescription("Trolling can result in a ban!")
        .addField("Feedback" , `Your feedback will be shown as \n **${feedback}**`)
        .setFooter(message.author.username)
        .setTimestamp();

        if(!feedback) return message.reply(no_feedback);

        const confirmation = await message.channel.send(confirm);
        const emojis = ['✅', '❌'];

        emojis.forEach((emoji) => confirmation.react(emoji));

        const filter = (reaction, user) =>
  user.id === message.author.id && emojis.includes(reaction.emoji.name);

const collector = confirmation.createReactionCollector(filter, { max: 1 });

collector.on('collect', (reaction, user) => {
  const [confirm, cancel] = emojis;

  confirmation.delete();

  if (reaction.emoji.name === cancel) {
    const cancelEmbed = new Discord.MessageEmbed()
      .setColor('#993333')
      .setTitle('Cancelled')
      .setDescription(
        `Cancelled! The feedback has not been sent.`,
      )
      .setFooter(message.author.username)
      .setTimestamp();
    return message.channel.send(cancelEmbed);
  }

  if (reaction.emoji.name === confirm) {
    const bannedEmbed = new Discord.MessageEmbed()
      .setColor('#993333')
      .setTitle('Sent')
      .setDescription(
        `Your feedback has been sent!`,
      )
      .setFooter(message.author.username)
      .setTimestamp();
    message.channel.send(bannedEmbed);
    const feedback_admins = new Discord.MessageEmbed()
    .setColor('#993333')
      .setTitle('Feedback')
      .setDescription(
        `We have gotten feedback!`,
      )
      .addFields(
          {name:"From" , value: message.author.username} ,
          {name:"In" , value: message.guild.name} ,
          {name:"Feedback" , value: feedback} ,
          )
      .setFooter(message.author.username)
      .setTimestamp();
      const channel_id = client.channels.cache.find((channel) => {
        return channel.id === "816714319172599828";
     });
     
     channel_id.send(feedback_admins);
  }
});
    }
}

Edit: Posted full code now, I hope that is okay like that, Since I have gotten an answer. the error is not the same anymore: It is now: Type Error. Cannot read property send of undefined, The error message says that the error is in line 83. so channel_id.send(feedback_admins).

Maybe the channel is not cached. Try to use fetch instead:

const channel = await client.channels.fetch('816714319172599828');
channel.send(feedback_admins);

PS: Your first error was TypeError: channel.send is not a function , so it seems that it was not a text channel.

You can use something like this.

// to get the channel
const channel_id = client.channels.cache.find((channel) => {
   return channel.id === "816714319172599828";
});

channel_id.send(feedback_admins);

I have found an answer, it seems I had to use message.guild.channels.cache.get("816714319172599828") !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM