Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
- module.exports = {
- data: new SlashCommandBuilder()
- .setName('ping')
- .setDescription('Return latency details and additional server info'),
- run: async (interaction) => {
- try {
- const updateLatencies = () => {
- const webLatency = Date.now() - interaction.createdTimestamp;
- const apiLatency = interaction.client.ws.ping;
- const uptime = interaction.client.uptime;
- return { webLatency, apiLatency, totalLatency: webLatency + apiLatency, uptime };
- };
- const getColor = (latency) => {
- if (latency < 200) return 0x43B581; // Green
- else if (latency < 500) return 0xF3C35D; // Yellow
- return 0xF04747; // Red
- };
- const formatUptime = (ms) => {
- const totalSeconds = Math.floor(ms / 1000);
- const days = Math.floor(totalSeconds / (3600 * 24));
- const hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
- const minutes = Math.floor((totalSeconds % 3600) / 60);
- const seconds = totalSeconds % 60;
- return `${days}d ${hours}h ${minutes}m ${seconds}s`;
- };
- const { webLatency, apiLatency, totalLatency, uptime } = updateLatencies();
- const botStartedAtTimestamp = Math.floor(interaction.client.readyTimestamp / 1000);
- const botStartedAt = `<t:${botStartedAtTimestamp}:F>`;
- // Initial embed with additional information
- const pingEmbed = new EmbedBuilder()
- .setTitle('Latency and Server Information')
- .setColor(getColor(totalLatency))
- .addFields(
- {
- name: '🌐 Websocket Latency:',
- value: `\`\`\`yml\n${webLatency}ms\`\`\``,
- inline: true,
- },
- {
- name: '🚀 API Latency:',
- value: `\`\`\`yml\n${apiLatency}ms\`\`\``,
- inline: true,
- },
- {
- name: '⏰ Bot Started At:',
- value: `${botStartedAt}`,
- inline: false,
- },
- {
- name: '🕒 Server Uptime:',
- value: `\`\`\`yml\n${formatUptime(uptime)}\`\`\``,
- inline: false,
- }
- )
- .setFooter({ text: 'Made by Rivvix Team' })
- .setTimestamp();
- const buttons = new ActionRowBuilder()
- .addComponents(
- new ButtonBuilder()
- .setCustomId('reload_ping')
- .setLabel('Reload Latency')
- .setStyle(ButtonStyle.Primary)
- .setEmoji('🔄'),
- new ButtonBuilder()
- .setLabel('Invite Rivvix')
- .setStyle(ButtonStyle.Link)
- .setEmoji('🔗')
- .setURL(`https://discord.com/oauth2/authorize?client_id=${interaction.client.user.id}&permissions=8&scope=bot%20applications.commands`)
- );
- const reply = await interaction.reply({ embeds: [pingEmbed], components: [buttons], fetchReply: true });
- const filter = i => i.customId === 'reload_ping' && i.user.id === interaction.user.id;
- const collector = reply.createMessageComponentCollector({ filter, time: 30000 });
- collector.on('collect', async i => {
- const { webLatency, apiLatency, totalLatency, uptime } = updateLatencies();
- pingEmbed.setColor(getColor(totalLatency))
- .fields = [
- {
- name: '🌐 Websocket Latency:',
- value: `\`\`\`yml\n${webLatency}ms\`\`\``,
- inline: true,
- },
- {
- name: '🚀 API Latency:',
- value: `\`\`\`yml\n${apiLatency}ms\`\`\``,
- inline: true,
- },
- {
- name: '⏰ Bot Started At:',
- value: `\`\`\`yml\n${botStartedAt}\`\`\``,
- inline: false,
- },
- {
- name: '🕒 Server Uptime:',
- value: `\`\`\`yml\n${formatUptime(uptime)}\`\`\``,
- inline: false,
- }
- ];
- await i.update({ embeds: [pingEmbed], components: [buttons], ephemeral: true });
- });
- collector.on('end', () => {
- buttons.components.forEach(button => button.setDisabled(true));
- interaction.followUp({ content: 'Button interaction ended. Click "Reload" to refresh latency', components: [buttons], ephemeral: true });
- });
- } catch (error) {
- console.error(error);
- await interaction.reply({ content: 'An error occurred while processing your command', ephemeral: true });
- }
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement