Why is my bot returning "an error has occured TypeError: Cannot read property 'send' of undefined"?

Hey guys,
I’ve been coding a function for my discord that sends a message to the modlog channel when a mod uses the lockdown command. Here’s the code:
//Lock Channel
lockchannelaction = function (RanBy, RanIn, message, ModLog){
const ModReportEmbed = new Discord.MessageEmbed()
ModReportEmbed.setColor(’#FF4500’)
ModReportEmbed.setTitle(‘Lockdown’)
ModReportEmbed.setDescription(‘Denys the Send Messages permission for all users.’)
ModReportEmbed.addFields(
{ name: ‘Responsible Moderator’, value: ${RanBy}, inline: false },
{ name: ‘Channel’, value: ${RanIn}, inline: false }
)
ModReportEmbed.setTimestamp()
const modlogchannel = client.channels.cache.get(${ModLog});
modlogchannel.send(ModReportEmbed)
}
and yes, to define ModLog i used const ModLog = db.fetch(ModlogID_${message.guild.id})
Please help me! I temporarily made the project public in case you want to check it: https://glitch.com/edit/#!/orangeecho?path=bot.js%3A616%3A1
Thanks!

I think it might be because you’ve got a lot of syntax errors. Mainly, you’re using backticks instead of quotes, and you’ve got a couple of missing semicolons.

The channel where you want to send it is not defined

1 Like

Hi,
Thanks for your reply.
and yes to define the channel I want to send it to I used

const ModLog = db.fetch(`ModlogID_${message.guild.id})
The channel IDs of the ModLog channel are on an sqlite database and I checked that it contains the correct channel ID.

Do all const statements require semicolons? I’ll try, but I thought it didn’t matter.
And some of the lines I have written are using ` but this platform got a bit confused.
eg ${ModLog} has a backtick in it.

Can you try to console.log the channel thats getting grabbed from the database

It returns undefined : (
I tried to add the const ModLog = db.fetch(`ModlogID_${message.guild.id}) in the same function but it gave another error which was
an error has occured ReferenceError: message is not defined

I’d recomend trying to use jshint.com - copy and paste your code in and it should tell you all the errors it can find. If it still isn’t working after that then it will at least be a bit easier to find errors.
Also, yes I think all const statements need a semicolon, also with the ModLog thing, I think you first need another const eg ModLogID which you replace ModLog with, and then do
Const ModLog = client.guild.get(ModLogID);
That should work. If it doesn’t, let me know.

1 Like

I have applied the fixes recommended by the site, still didn’t work with the same error :confused:
//Lock Channel
lockchannelaction = function (RanBy, RanIn){
const ModReportEmbed = new Discord.MessageEmbed();
ModReportEmbed.setColor(’#FF4500’);
ModReportEmbed.setTitle(‘Lockdown’);
ModReportEmbed.setDescription(Denys the Send Messages permission for all users.);
ModReportEmbed.addFields(
{ name: ‘Responsible Moderator’, value: ${RanBy}, inline: false },
{ name: ‘Channel’, value: ${RanIn}, inline: false }
);
ModReportEmbed.setTimestamp();
const ModLog = db.fetch(ModlogID_${message.guild.id});
const modlogchannel = client.channels.cache.get(${ModLog});
console.log(modlogchannel);
modlogchannel.send(ModReportEmbed);
};

You arent grabbing any channels from the db. It isn’t the fault of syntax

??
20chars20chars20chars

You arent getting anything from the database

and instead of ${modlog} you would just put modlog.

oh for that. I used ` but it was hidden since the forum system was confused

wait a sec, let me check the sqlite

hmmm. the code works for my other commands, but not for this function

Right, I think it should be this:

//Lock Channel
lockchannelaction = function (RanBy, RanIn){
const ModReportEmbed = new Discord.MessageEmbed();
ModReportEmbed.setColor(’#FF4500’);
ModReportEmbed.setTitle(‘Lockdown’);
ModReportEmbed.setDescription( Denys the Send Messages permission for all users. );
ModReportEmbed.addField(name: ‘Responsible Moderator’, value: RanBy, inline: false);
ModRepottEmbed.addField (name: ‘Channel’, value: RanIn, inline: false);
ModReportEmbed.setTimestamp();
const ModLog = db.fetch(message.guild.id);
const modlogchannel = client.guilds.cache.get(ModLog);
console.log(modlogchannel);
modlogchannel.send(ModReportEmbed);
};
But I’m not quite sure what the ModLod const actuslly does. Is it the guild that the message is sent in? Or the channel to be sent to? If you could let us know that would be great.

Edit: sorry I understand the modlog now.

hmmm, didn’t work with the same error too.

All right, what are you actually fetching from the db? Is it the user id of the mod of that guild or something?

I’m fetching the modlog channel IDs for that particular guild because I have a system where all channel IDs and role IDs are stored in a db so that each server can get the bot customised and not needing to have one bot for each server (not only is it cluttering my discord dev applications portal, but is also a pain when updating. Thus I decided to use this method of customisation.