1
votes

I am creating a discord bot, and I want to create a "warn" command for it, to warn users in a discord server when they are misbehaving. But, when I run the code and try out the command it only gets to about halfway through, where an unhandled promise rejection message pops up, saying that I haven't defined my message.channel.find() function, even though I have fully defined it.

For some background, I am coding this bot in javascript, node.js to be more specific. I am using the discord.js library as well. I've tried restarting my code, re-entering what is inside of my parentheses, and even tried reformatting it a couple of times. But, none of that has worked. I've included my code to help you all better understand my issue.

// Here is where it starts to get shakey
let firstslice = message.content.slice(6);
let membertag = membertobewarned.tag;
let reason = firstslice.slice(membertag);
message.channel.send("Second debugging check, check!");

// Here is where it stops working, and the line below this text is where I 
// am getting unhandled promise rejection errors from.
let warnchannel = message.channel.find(`name`, "logs");
if (message.author.bot) return;
if (!message.author.hasPermission("MANAGE_MESSAGES")) return message.channel.send("Sorry, but you do not have permission to use this command.");
if (!membertobewarned.hasPermission("MANAGE_MESSAGES")) return message.channel.send("Sorry, but you cannot warn this member.");

message.channel.send("Third debugging thing, check!");

let warnembed = new Discord.RichEmbed()
  .setDescription("Warning")
  .setColor("#FFA500")
  .addField("User:", membertobewarned)
  .addField("Warned By:", commanduser)
  .addField("For Reason:", reason)
  .addField("Time:", timewarned);
warnchannel.send(warnembed);

message.channel.send("I'm all done!");

What's supposed to happen is the bot will get the channel the message was sent in, who to warn, who warned that person, their mention, and the reason for the warning.
It will then take that information, and compile it into a discord RichEmbed, and then send it into a #logs channel.
But, what actually happens is when the command is running, it takes all the info, and then right where it checks for the #logs channel, it throws the error:

UnhandledPromiseRejectionWarning TypeError: message.channel.find is not a function. 

I am hoping to be able to figure out why am I getting this error, and how to fix it. Thank you all for reading this, and I hope that you can all help me soon, and have and have an awesome rest of your day.

1

1 Answers

1
votes

That's because message.channel is an object, not an array or Map (in Discord.js it's called a Collection).

I'm not too sure what you want to accomplish, but if you want to select another channel you need to use either

message.guild.channels.find('name', 'logs')

or in newer Discord.js versions

message.guild.channels.find(channel => channel.name === 'logs')