#Channel not found!

27 messages · Page 1 of 1 (latest)

paper nova
#

Dear everyone, i'm learning the basics and for learning purpose i wanted to create a fivem server status script. I got stucked at querying the channel id of the target "status" channel, it always says channel not found. All the intents, permission are set for the bot.

code:

const { EmbedBuilder, Client, Intents } = require('discord.js');
const axios = require('axios');

const config = {
    statusChannelId: mychannelid,
    updateInterval: 60000 // 1 minute
};

async function getServerStatus() {
    try {
        const response = await axios.get(`myendpoint/players.json`);
        const players = response.data.length;

        return {
            status: 'Online',
            players: players
        };
    } catch (error) {
        console.error('Error fetching server status:', error);
        return {
            status: 'Offline',
            players: 0
        };
    }
}

async function updateStatusChannel(client) {
    const status = await getServerStatus();
    const embed = new EmbedBuilder()
        .setColor('#0099ff')
        .setTitle('FiveM Server Status')
        .addFields(
            { name: 'Status', value: status.status, inline: true },
            { name: 'Players', value: status.players.toString(), inline: true }
        )
        .setTimestamp();

    try {
        const channel = await client.channels.cache.get(config.statusChannelId);
        if (channel) {
            const messages = await channel.messages.fetch({ limit: 1 });
            const lastMessage = messages.first();
            
            if (lastMessage && lastMessage.embeds.length > 0) {
                await lastMessage.edit({ embeds: [embed] });
            } else {
                await channel.send({ embeds: [embed] });
            }
        } else {
            console.error('Channel not found!');
        }
    } catch (error) {
        console.error('Error fetching channel:', error);
    }
}

module.exports = async (client) => {
    updateStatusChannel(client);
    setInterval(() => updateStatusChannel(client), config.updateInterval);

    client.on('messageCreate', async (message) => {
        if (message.content === '!status') {
            const status = await getServerStatus();

            const embed = new EmbedBuilder()
                .setColor('#0099ff')
                .setTitle('FiveM Server Status')
                .addFields(
                    { name: 'Status', value: status.status, inline: true },
                    { name: 'Players', value: status.players.toString(), inline: true }
                )
                .setTimestamp();

            message.channel.send({ embeds: [embed] });
        }
    });
};

What could be the problem? Thank you for your help in advance. Have a great day!

near terraceBOT
#
  • 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!
  • Marked as resolved by OP
paper nova
azure plinth
#

How is config.statusChannelId defined

#

And show your client constructor

paper nova
# azure plinth How is config.statusChannelId defined
const config = {
    statusChannelId: mychannelid,
    updateInterval: 60000 // 1 minute
};

client:

const client = new Discord.Client({
    allowedMentions: {
        parse: [
            'users',
            'roles'
        ],
        repliedUser: true
    },
    autoReconnect: true,
    disabledEvents: [
        "TYPING_START"
    ],
    partials: [
        Discord.Partials.Channel,
        Discord.Partials.GuildMember,
        Discord.Partials.Message,
        Discord.Partials.Reaction,
        Discord.Partials.User,
        Discord.Partials.GuildScheduledEvent
    ],
    intents: [
        Discord.GatewayIntentBits.Guilds,
        Discord.GatewayIntentBits.GuildMembers,
        Discord.GatewayIntentBits.GuildBans,
        Discord.GatewayIntentBits.GuildEmojisAndStickers,
        Discord.GatewayIntentBits.GuildIntegrations,
        Discord.GatewayIntentBits.GuildWebhooks,
        Discord.GatewayIntentBits.GuildInvites,
        Discord.GatewayIntentBits.GuildVoiceStates,
        Discord.GatewayIntentBits.GuildMessages,
        Discord.GatewayIntentBits.GuildMessageReactions,
        Discord.GatewayIntentBits.GuildMessageTyping,
        Discord.GatewayIntentBits.DirectMessages,
        Discord.GatewayIntentBits.DirectMessageReactions,
        Discord.GatewayIntentBits.DirectMessageTyping,
        Discord.GatewayIntentBits.GuildScheduledEvents,
        Discord.GatewayIntentBits.MessageContent
    ],
    restTimeOffset: 0
});
azure plinth
#

this still doesnt tell me how the id actually is defined

#

and do you really need all those intents

coral sandalBOT
#

We highly recommend only specifying the intents you actually need.

  • Note, that 98303, 32767 or whatever other magic number you read that represents "all intents", gets outdated as soon as new intents are introduced.
  • The number will always represent the same set of intents, and will not include new ones. There is no magic "all intents" bit.
paper nova
paper nova
azure plinth
#

ok how is mychannelid defined then

azure plinth
paper nova
azure plinth
#

Not to mention this causes unnecessary resource usage

azure plinth
#

They should be strings

paper nova
#

i have tried with string but the result was the same.

#

now i'm giving the channelid in string, but still "channel not found".
is it possible to query all the channels my bot see? 😄

azure plinth
#

Log the id right before you get from cache

azure plinth
paper nova
#

i understand 😄

paper nova
azure plinth
#

Where do you call that function

paper nova
# azure plinth Where do you call that function

the bot runs this when it starts up, and then every minute using setInterval, all within the same file. however, it reports that it can't find the channel on the initial start, but it works during the minute-by-minute updates, and the data in the channel is refreshed correctly.

I assume that after the bot has loaded, it caches the channels and then I can manage them?

azure plinth
#

No

#

The client just isnt ready the first time