#Bot doesn't send a message

29 messages · Page 1 of 1 (latest)

blissful jewel
#

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

kind brookBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • 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!
blissful jewel
#
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.');
#
                }
            } else {
                console.log('Could not find the guild for this voice channel.');
            }
        });
    }
});
hearty parrot
#

i dont see where you call client.login(...)

blissful jewel
hearty parrot
#

thats why you store it in another file

#

does your bot go online?

#

whats your djs version?

#

npm ls discord.js

blissful jewel
#

yup, he goes
djs is v12, i updated it to v14 and the code broke

hearty parrot
#

that code is most definitely not v14

#

and yes your code will break

#

its 2 major versions

blissful jewel
#

yeah, i understand, that's why i launched it on v12

#

it's worked just on level "it's online", that's it

hearty parrot
#

please show the output of npm ls discord.js

hearty parrot
#

you'll want to look at the v13 guide first, then the v14 guide

steep jackalBOT
#

guide Additional Information: Updating from v12 to v13
read more

#

guide Additional Information: Updating from v13 to v14
read more

blissful jewel
#

i should update it step-by-step u mean?

hearty parrot
#

yes

blissful jewel
#

oke, i'll try

hearty parrot
#

unfortunately, we don't support v12 anymore so you will need to update to at least v13, but i suggest going from 13 to 14

hearty parrot
#

updating your version doesn't magically make your code work

#

you need to actually update the code