sending messages code problem

I wrote some codes that sends a image 'sendthis.jpg' to the chat if there's no messages for 45 minutes.
require('dotenv').config();
const { Client, Intents } = require('discord.js');
const fs = require('fs');

const client = new Client({ intents: [Intents.FLAG.GUILD, Intents.FLAGS.GUILD_MESSAGES] });

let lastMessageTimestamp = null;
const channelID = '1019570335818981427';
const imagePath = 'src/sendthis.jpg';
const timeLimit = 45 * 60 * 1000;

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
const channel = client.channels.cache.get(channelID);
if (!channel) {
console.error(`Channel with ID ${channelID} not found`);
return;
}
channel.messages.fetch({ limit: 1 }).then(messages => {
const lastMessage = messages.first();
if (lastMessage) {
lastMessageTimestamp = lastMessage.createdTimestamp;
}
});
});

client.on('messageCreate', message => {
if (message.channel.id === channelID) {
lastMessageTimestamp = message.createdTimestamp;
}
});

setInterval(() => {
if (lastMessageTimestamp && Date.now() - lastMessageTimestamp > timeLimit) {
const channel = client.channels.cache.get(channelID);
if (channel) {
const file = new Discord.MessageAttachment(imagePath);
channel.send({ files: [file] });
lastMessageTimestamp = null;
}
}
}, 1000);

client.login(process.env.TOKEN);
require('dotenv').config();
const { Client, Intents } = require('discord.js');
const fs = require('fs');

const client = new Client({ intents: [Intents.FLAG.GUILD, Intents.FLAGS.GUILD_MESSAGES] });

let lastMessageTimestamp = null;
const channelID = '1019570335818981427';
const imagePath = 'src/sendthis.jpg';
const timeLimit = 45 * 60 * 1000;

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
const channel = client.channels.cache.get(channelID);
if (!channel) {
console.error(`Channel with ID ${channelID} not found`);
return;
}
channel.messages.fetch({ limit: 1 }).then(messages => {
const lastMessage = messages.first();
if (lastMessage) {
lastMessageTimestamp = lastMessage.createdTimestamp;
}
});
});

client.on('messageCreate', message => {
if (message.channel.id === channelID) {
lastMessageTimestamp = message.createdTimestamp;
}
});

setInterval(() => {
if (lastMessageTimestamp && Date.now() - lastMessageTimestamp > timeLimit) {
const channel = client.channels.cache.get(channelID);
if (channel) {
const file = new Discord.MessageAttachment(imagePath);
channel.send({ files: [file] });
lastMessageTimestamp = null;
}
}
}, 1000);

client.login(process.env.TOKEN);
And when I run this, terminal says 'TypeError: Cannot read properties of undefined (reading 'FLAG') how can I fix it?
5 Replies
d.js toolkit
d.js toolkit2y ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
세련된칡덩굴
im using v14 ok thakns
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs2y ago
guide Additional Information: Updating from v13 to v14 read more
Want results from more Discord servers?
Add your server