Skip to content

Commit

Permalink
Create ping.js (SudhanPlayz#860)
Browse files Browse the repository at this point in the history
* Create ping.js

simple Ping command inspired by @XstreamSpeed
This version fixes the mistakes from the previous PR, which include but do not limit themselves to: 
Wrong scopes, mismatched parenthesis, undefined methods and variables, redundant information provided by other commands...

* fix hanging login on replit systems

* Update index.js

changed scope
  • Loading branch information
brianferri authored Jun 9, 2022
1 parent d2f505a commit a3fc74d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
57 changes: 57 additions & 0 deletions commands/slash/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { MessageEmbed } = require("discord.js");
const SlashCommand = require("../../lib/SlashCommand");

const command = new SlashCommand()
.setName("ping")
.setDescription("View the bot's latency")
.setRun(async (client, interaction, options) => {
let msg = await interaction.channel.send({
embeds: [new MessageEmbed()
.setDescription("🏓 | Fetching ping...")
.setColor("#6F8FAF")
]
})

let zap = "⚡";
let green = "🟢";
let red = "🔴";
let yellow = "🟡";

var botState = zap;
var apiState = zap;

let apiPing = client.ws.ping;
let botPing = Math.floor(msg.createdAt - interaction.createdAt);

if (apiPing >= 40 && apiPing < 200) {
apiState = green;
} else if (apiPing >= 200 && apiPing < 400) {
apiState = yellow;
} else if (apiPing >= 400) {
apiState = red;
}

if (botPing >= 40 && botPing < 200) {
botState = green;
} else if (botPing >= 200 && botPing < 400) {
botState = yellow;
} else if (botPing >= 400) {
botState = red;
}

msg.delete();
interaction.reply({
embeds: [new MessageEmbed()
.setTitle("🏓 | Pong!")
.addField("API Latency", `\`\`\`yml\n${apiState} | ${apiPing}ms\`\`\``, true)
.addField("Bot Latency", `\`\`\`yml\n${botState} | ${botPing}ms\`\`\``, true)
.setColor(client.config.embedColor)
.setFooter({
text: `Requested by ${interaction.user.tag}`,
iconURL: (interaction.user.avatarURL())
})
]
})
});

module.exports = command;
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
const DiscordMusicBot = require("./lib/DiscordMusicBot");
const { exec } = require("child_process");

if (process.env.REPL_ID) {
console.log("Replit system detected, initiating special `unhandledRejection` event listener | index.js:19")
process.on('unhandledRejection', (reason, promise) => {
promise.catch((err) => {
if (err.status === 429) {
console.log("something went wrong while logging in, resetting...");
exec("kill 1");
}
});
});
}

const client = new DiscordMusicBot();

console.log("Make sure to fill in the config.js before starting the bot.");
Expand Down

0 comments on commit a3fc74d

Please sign in to comment.