Shard info

Hi I want to make a command that shows how many servers each shard has and ping etc. But something is not working. Here are some example results: Shard information Shard ID: undefined Number of servers: 120 Ping: 113 ms Running time: 16 seconds Memory usage: 54.81 MB Shard ID: undefined Number of servers: 120 Ping: N/A ms Running time: 8 seconds Memory usage: 40.36 MB I WANT TO EMPHASIZE THAT MY BOT IS ON 120 SERVERS AND BOTH SHARDS RETURN 120 SERVERS
5 Replies
d.js toolkit
d.js toolkit3mo 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!
🗿Patryk🍷
🗿Patryk🍷OP3mo ago
const { SlashCommandBuilder } = require('@discordjs/builders');
const { EmbedBuilder } = require('discord.js');
const { client } = require('../index.js'); // Import klienta

module.exports = {
data: new SlashCommandBuilder()
.setName('shard')
.setDescription('Wyświetla informacje o shardach'),
async execute(interaction) {
if (!interaction.deferred && !interaction.replied) {
await interaction.deferReply();
}

try {
// Fetch the guild counts for each shard
const guildCounts = await client.shard.fetchClientValues('guilds.cache.size');
const totalGuildCount = guildCounts.reduce((acc, count) => acc + count, 0);

// Get stats for each shard
const shardStats = await client.shard.broadcastEval(c => {
return {
shardId: c.shard.id,
guildCount: c.guilds.cache.size,
ping: c.ws.ping >= 0 ? c.ws.ping : 'N/A', // Używamy 'N/A' zamiast -1
uptime: c.uptime,
memoryUsage: process.memoryUsage().heapUsed / 1024 / 1024 // Użycie pamięci w MB
};
});

// Create embed with shard information
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Informacje o shardach')
.addFields({
name: 'Łączna liczba serwerów:',
value: `${totalGuildCount}`,
inline: false
});

shardStats.forEach(stat => {
embed.addFields({
name: `Shard ID: ${stat.shardId}`,
value: `Liczba serwerów: ${stat.guildCount}\nPing: ${stat.ping} ms\nCzas działania: ${Math.floor(stat.uptime / 1000)} sekundy\nUżycie pamięci: ${Math.round(stat.memoryUsage * 100) / 100} MB`,
inline: false
});
});

await interaction.editReply({ embeds: [embed] });
} catch (error) {
console.error(error);
await interaction.editReply('Wystąpił błąd podczas pobierania informacji o shardach.');
}
},
};
const { SlashCommandBuilder } = require('@discordjs/builders');
const { EmbedBuilder } = require('discord.js');
const { client } = require('../index.js'); // Import klienta

module.exports = {
data: new SlashCommandBuilder()
.setName('shard')
.setDescription('Wyświetla informacje o shardach'),
async execute(interaction) {
if (!interaction.deferred && !interaction.replied) {
await interaction.deferReply();
}

try {
// Fetch the guild counts for each shard
const guildCounts = await client.shard.fetchClientValues('guilds.cache.size');
const totalGuildCount = guildCounts.reduce((acc, count) => acc + count, 0);

// Get stats for each shard
const shardStats = await client.shard.broadcastEval(c => {
return {
shardId: c.shard.id,
guildCount: c.guilds.cache.size,
ping: c.ws.ping >= 0 ? c.ws.ping : 'N/A', // Używamy 'N/A' zamiast -1
uptime: c.uptime,
memoryUsage: process.memoryUsage().heapUsed / 1024 / 1024 // Użycie pamięci w MB
};
});

// Create embed with shard information
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Informacje o shardach')
.addFields({
name: 'Łączna liczba serwerów:',
value: `${totalGuildCount}`,
inline: false
});

shardStats.forEach(stat => {
embed.addFields({
name: `Shard ID: ${stat.shardId}`,
value: `Liczba serwerów: ${stat.guildCount}\nPing: ${stat.ping} ms\nCzas działania: ${Math.floor(stat.uptime / 1000)} sekundy\nUżycie pamięci: ${Math.round(stat.memoryUsage * 100) / 100} MB`,
inline: false
});
});

await interaction.editReply({ embeds: [embed] });
} catch (error) {
console.error(error);
await interaction.editReply('Wystąpił błąd podczas pobierania informacji o shardach.');
}
},
};
treble/luna
treble/luna3mo ago
why are you sharding if you only have 120 servers?
🗿Patryk🍷
🗿Patryk🍷OP3mo ago
So far I'm just testing But I already want to know how to do it
Syjalo
Syjalo3mo ago
It's pointless with that amount of servers, but it's your choice client.shard.id is not a thing. Client can handle multiple shards. So there's client.shard.ids, but I recommend flat map client.ws.shards
Want results from more Discord servers?
Add your server