aboutsummaryrefslogtreecommitdiff
path: root/src/commands/info/ping.ts
blob: 7f8ab6b58868b3ddf7b624c64de34b2385661e8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { CommandInteraction } from 'discord.js';
import { Message } from 'discord.js';
import { MessageEmbed } from 'discord.js';
import { BotCommand } from '../../lib/extensions/BotCommand';
import { BotMessage } from '../../lib/extensions/BotMessage';

export default class PingCommand extends BotCommand {
	constructor() {
		super('ping', {
			aliases: ['ping'],
			description: {
				content: 'Gets the latency of the bot',
				usage: 'ping',
				examples: ['ping']
			}
		});
	}

	public async exec(message: BotMessage): Promise<void> {
		const sentMessage = await message.util.send('Pong!');
		const timestamp: number = message.editedTimestamp
			? message.editedTimestamp
			: message.createdTimestamp;
		const botLatency = `\`\`\`\n ${Math.floor(
			sentMessage.createdTimestamp - timestamp
		)}ms \`\`\``;
		const apiLatency = `\`\`\`\n ${Math.round(
			message.client.ws.ping
		)}ms \`\`\``;
		const embed = new MessageEmbed()
			.setTitle('Pong!  🏓')
			.addField('Bot Latency', botLatency, true)
			.addField('API Latency', apiLatency, true)
			.setFooter(
				message.author.username,
				message.author.displayAvatarURL({ dynamic: true })
			)
			.setTimestamp();
		await sentMessage.edit({
			content: null,
			embed
		});
	}

	public async execSlash(message: CommandInteraction): Promise<void> {
		const timestamp1 = message.createdTimestamp;
		await message.reply('Pong!');
		const timestamp2 = await message
			.fetchReply()
			.then((m) => (m as Message).createdTimestamp);
		const botLatency = `\`\`\`\n ${Math.floor(
			timestamp2 - timestamp1
		)}ms \`\`\``;
		const apiLatency = `\`\`\`\n ${Math.round(this.client.ws.ping)}ms \`\`\``;
		const embed = new MessageEmbed()
			.setTitle('Pong!  🏓')
			.addField('Bot Latency', botLatency, true)
			.addField('API Latency', apiLatency, true)
			.setFooter(
				message.user.username,
				message.user.displayAvatarURL({ dynamic: true })
			)
			.setTimestamp();
		await message.editReply({
			content: null,
			embeds: [embed]
		});
	}
}