aboutsummaryrefslogtreecommitdiff
path: root/src/commands/info/snowflake.ts
blob: 933b1acbfa9b7752b8b6d8c2639db711e8674398 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { BushCommand, type ArgType, type BushMessage, type BushSlashMessage } from '#lib';
import {
	ApplicationCommandOptionType,
	ChannelType,
	EmbedBuilder,
	PermissionFlagsBits,
	SnowflakeUtil,
	type DeconstructedSnowflake,
	type Guild,
	type Role,
	type Snowflake,
	type User
} from 'discord.js';

export default class SnowflakeCommand extends BushCommand {
	public constructor() {
		super('snowflake', {
			aliases: ['snowflake', 'info', 'sf'],
			category: 'info',
			description: 'Provides information about the specified Snowflake.',
			usage: ['snowflake <snowflake>'],
			examples: ['snowflake 322862723090219008'],
			args: [
				{
					id: 'snowflake',
					description: 'The snowflake you would like to get information about.',
					type: 'snowflake',
					prompt: 'What snowflake would you like to get information about?',
					retry: '{error} Choose a valid snowflake.',
					slashType: ApplicationCommandOptionType.String
				}
			],
			clientPermissions: (m) => util.clientSendAndPermCheck(m, [PermissionFlagsBits.EmbedLinks], true),
			userPermissions: [],
			slash: true
		});
	}

	public override async exec(message: BushMessage | BushSlashMessage, args: { snowflake: ArgType<'snowflake'> }) {
		const snowflake = `${args.snowflake}` as Snowflake;
		const snowflakeEmbed = new EmbedBuilder().setTitle('Unknown :snowflake:').setColor(util.colors.default);

		// Channel
		if (client.channels.cache.has(snowflake)) {
			const channel = client.channels.resolve(snowflake)!;
			const channelInfo = [`**Type:** ${BushChannelType[channel.type] ?? ChannelType[channel.type]}`];
			if (channel.isDM()) {
				channelInfo.push(
					`**Recipient:** ${util.discord.escapeMarkdown(channel.recipient?.tag ?? '¯\\_(ツ)_/¯')} (${
						channel.recipient?.id ?? '¯\\_(ツ)_/¯'
					})`
				);
				snowflakeEmbed.setTitle(
					`:snowflake: DM with ${util.discord.escapeMarkdown(channel.recipient?.tag ?? '¯\\_(ツ)_/¯')} \`[Channel]\``
				);
			} else if (
				channel.isCategory() ||
				channel.isNews() ||
				channel.isText() ||
				channel.isVoice() ||
				channel.isStage() ||
				channel.isThread()
			) {
				channelInfo.push(
					`**Channel Name:** <#${channel.id}> (${util.discord.escapeMarkdown(channel.name)})`,
					`**Channel's Server:** ${util.discord.escapeMarkdown(channel.guild.name)} (${channel.guild.id})`
				);
				snowflakeEmbed.setTitle(`:snowflake: ${util.discord.escapeMarkdown(channel.name)} \`[Channel]\``);
			}
			snowflakeEmbed.addFields([{ name: '» Channel Info', value: channelInfo.join('\n') }]);
		}

		// Guild
		if (client.guilds.cache.has(snowflake)) {
			const guild: Guild = client.guilds.cache.get(snowflake)!;
			const guildInfo = [
				`**Name:** ${util.discord.escapeMarkdown(guild.name)}`,
				`**Owner:** ${util.discord.escapeMarkdown(client.users.cache.get(guild.ownerId)?.tag ?? '¯\\_(ツ)_/¯')} (${
					guild.ownerId
				})`,
				`**Members:** ${guild.memberCount?.toLocaleString()}`
			];
			if (guild.icon) snowflakeEmbed.setThumbnail(guild.iconURL({ size: 2048 })!);
			snowflakeEmbed.addFields([{ name: '» Server Info', value: guildInfo.join('\n') }]);
			snowflakeEmbed.setTitle(`:snowflake: ${util.discord.escapeMarkdown(guild.name)} \`[Server]\``);
		}

		// User
		const fetchedUser = await client.users.fetch(`${snowflake}`).catch(() => undefined);
		if (client.users.cache.has(snowflake) || fetchedUser) {
			const user: User = (client.users.cache.get(snowflake) ?? fetchedUser)!;
			const userInfo = [`**Name:** <@${user.id}> (${util.discord.escapeMarkdown(user.tag)})`];
			if (user.avatar) snowflakeEmbed.setThumbnail(user.avatarURL({ size: 2048 })!);
			snowflakeEmbed.addFields([{ name: '» User Info', value: userInfo.join('\n') }]);
			snowflakeEmbed.setTitle(`:snowflake: ${util.discord.escapeMarkdown(user.tag)} \`[User]\``);
		}

		// Emoji
		if (client.emojis.cache.has(snowflake)) {
			const emoji = client.emojis.cache.get(snowflake)!;
			const emojiInfo = [
				`**Name:** ${util.discord.escapeMarkdown(emoji.name ?? '¯\\_(ツ)_/¯')}`,
				`**Animated:** ${emoji.animated}`
			];
			if (emoji.url) snowflakeEmbed.setThumbnail(emoji.url);
			snowflakeEmbed.addFields([{ name: '» Emoji Info', value: emojiInfo.join('\n') }]);
			snowflakeEmbed.setTitle(`:snowflake: ${util.discord.escapeMarkdown(emoji.name ?? '¯\\_(ツ)_/¯')} \`[Emoji]\``);
		}

		// Role
		if (message.guild && message.guild.roles.cache.has(snowflake)) {
			const role: Role = message.guild.roles.cache.get(snowflake)!;
			const roleInfo = [
				`**Name:** <@&${role.id}> (${util.discord.escapeMarkdown(role.name)})`,
				`**Members:** ${role.members.size}`,
				`**Hoisted:** ${role.hoist}`,
				`**Managed:** ${role.managed}`,
				`**Position:** ${role.position}`,
				`**Hex Color:** ${role.hexColor}`
			];
			if (role.color) snowflakeEmbed.setColor(role.color);
			snowflakeEmbed.addFields([{ name: '» Role Info', value: roleInfo.join('\n') }]);
			snowflakeEmbed.setTitle(`:snowflake: ${util.discord.escapeMarkdown(role.name)} \`[Role]\``);
		}

		// SnowflakeInfo
		const deconstructedSnowflake: DeconstructedSnowflake = SnowflakeUtil.deconstruct(snowflake);
		const snowflakeInfo = [
			`**Timestamp:** ${deconstructedSnowflake.timestamp}`,
			`**Created:** ${util.timestamp(new Date(Number(deconstructedSnowflake.timestamp)))}`,
			`**Worker ID:** ${deconstructedSnowflake.workerId}`,
			`**Process ID:** ${deconstructedSnowflake.processId}`,
			`**Increment:** ${deconstructedSnowflake.increment}`
		];
		snowflakeEmbed.addFields([{ name: '» Snowflake Info', value: snowflakeInfo.join('\n') }]);

		return await message.util.reply({ embeds: [snowflakeEmbed] });
	}
}

enum BushChannelType {
	'Text' = 0,
	'DM' = 1,
	'Voice' = 2,
	'Group DM' = 3,
	'Category' = 4,
	'Announcement' = 5,
	'Announcement Store' = 6,
	'News Thread' = 10,
	'Public Thread' = 11,
	'Private Thread' = 12,
	'Stage' = 13
}