sigma
sigma
DIAdiscord.js - Imagine an app
Created by sigma on 7/30/2024 in #djs-questions
Channel not found!
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] });
}
});
};
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!
28 replies