Bot doesn't send a message

i need to make a bot that will count an usertime in voice channel and send a message with this time in the text-channel after user left it, the problem is that bot doesn't send messages, and there's no info about that in log
19 Replies
d.js toolkit
d.js toolkit11mo 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!
daryl.
daryl.OP11mo ago
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_MESSAGES] });

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});

client.on('voiceStateUpdate', (oldState, newState) => {
console.log('voiceStateUpdate event triggered');
if (oldState.channelId && newState.channelId && oldState.channelId === newState.channelId) {
// User changed their voice settings (e.g., muted/unmuted, deafened/undeafened)
return;
}
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_MESSAGES] });

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});

client.on('voiceStateUpdate', (oldState, newState) => {
console.log('voiceStateUpdate event triggered');
if (oldState.channelId && newState.channelId && oldState.channelId === newState.channelId) {
// User changed their voice settings (e.g., muted/unmuted, deafened/undeafened)
return;
}
// Check if the user has just joined a voice channel
if (!oldState.channelId && newState.channelId) {
// User has joined a voice channel
voiceStates.set(newState.id, {channelId: newState.channelId, joinTime: Date.now()});
} else if (oldState.channelId && !newState.channelId) {
// User has left a voice channel
const joinTime = voiceStates.get(oldState.id).joinTime;
const leaveTime = Date.now();
const timeDifference = leaveTime - joinTime;
const hours = Math.floor(timeDifference / (1000 * 60 * 60));
const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
const timeSpent = `${hours} hours and ${minutes} minutes`;

voiceStates.delete(oldState.id);

// Get the text channel associated with the voice channel where the user has just left
client.guilds.fetch(oldState.guild.id)
.then(guild => {
if (guild) {
const textChannel = guild.channels.cache.find(channel => channel.type === 'text');
if (textChannel) {
// Check if the bot can send messages in the text channel
const permissions = textChannel.permissionsFor(client.user);
if (permissions.has('SEND_MESSAGES')) {
// The bot can send messages in the channel, so proceed with sending the message
console.log(`User ${oldState.member.user.tag} spent ${timeSpent} in the voice channel.`)
textChannel.send(`User ${oldState.member.user.tag} spent ${timeSpent} in the voice channel.`);
} else {
console.log('The bot does not have permission to send messages in this text channel.');
}
} else {
console.log('No text channel found for this guild.');
// Check if the user has just joined a voice channel
if (!oldState.channelId && newState.channelId) {
// User has joined a voice channel
voiceStates.set(newState.id, {channelId: newState.channelId, joinTime: Date.now()});
} else if (oldState.channelId && !newState.channelId) {
// User has left a voice channel
const joinTime = voiceStates.get(oldState.id).joinTime;
const leaveTime = Date.now();
const timeDifference = leaveTime - joinTime;
const hours = Math.floor(timeDifference / (1000 * 60 * 60));
const minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
const timeSpent = `${hours} hours and ${minutes} minutes`;

voiceStates.delete(oldState.id);

// Get the text channel associated with the voice channel where the user has just left
client.guilds.fetch(oldState.guild.id)
.then(guild => {
if (guild) {
const textChannel = guild.channels.cache.find(channel => channel.type === 'text');
if (textChannel) {
// Check if the bot can send messages in the text channel
const permissions = textChannel.permissionsFor(client.user);
if (permissions.has('SEND_MESSAGES')) {
// The bot can send messages in the channel, so proceed with sending the message
console.log(`User ${oldState.member.user.tag} spent ${timeSpent} in the voice channel.`)
textChannel.send(`User ${oldState.member.user.tag} spent ${timeSpent} in the voice channel.`);
} else {
console.log('The bot does not have permission to send messages in this text channel.');
}
} else {
console.log('No text channel found for this guild.');
}
} else {
console.log('Could not find the guild for this voice channel.');
}
});
}
});
}
} else {
console.log('Could not find the guild for this voice channel.');
}
});
}
});
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
daryl.
daryl.OP11mo ago
it's the last line i just deleted it, i think people don't need to see the token :))
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
daryl.
daryl.OP11mo ago
yup, he goes djs is v12, i updated it to v14 and the code broke
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
daryl.
daryl.OP11mo ago
yeah, i understand, that's why i launched it on v12 it's worked just on level "it's online", that's it
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
daryl.
daryl.OP11mo ago
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs11mo ago
guide Additional Information: Updating from v12 to v13 read moreguide Additional Information: Updating from v13 to v14 read more
daryl.
daryl.OP11mo ago
i should update it step-by-step u mean?
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
daryl.
daryl.OP11mo ago
oke, i'll try
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
daryl.
daryl.OP11mo ago
well, i did it. correct?
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
daryl.
daryl.OP11mo ago
of course)
Want results from more Discord servers?
Add your server