aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/viewRaw.ts
blob: 1220f0f583a85093b9483280fdf0e4c20b0fbe73 (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
import {
	Arg,
	BotCommand,
	colors,
	emojis,
	inspect,
	type ArgType,
	type CommandMessage,
	type OptArgType,
	type SlashMessage
} from '#lib';
import assert from 'assert/strict';
import { ApplicationCommandOptionType, Constants, EmbedBuilder, Message } from 'discord.js';

export default class ViewRawCommand extends BotCommand {
	public constructor() {
		super('view-raw', {
			aliases: ['view-raw', 'vr'],
			category: 'utilities',
			description: 'Shows raw information about a message.',
			usage: ['viewraw <message id> <channel>'],
			examples: ['viewraw 322862723090219008'],
			args: [
				{
					id: 'message',
					description: 'The message to view the raw content of.',
					type: Arg.union('message', 'messageLink'),
					readableType: 'message|messageLink',
					prompt: 'What message would you like to view?',
					retry: '{error} Choose a valid message.',
					slashType: ApplicationCommandOptionType.String
				},
				{
					id: 'channel',
					description: 'The channel that the message is in.',
					type: 'textBasedChannel',
					prompt: 'What channel is the message in?',
					retry: '{error} Choose a valid channel.',
					optional: true,
					slashType: ApplicationCommandOptionType.Channel,
					channelTypes: Constants.TextBasedChannelTypes
				},
				{
					id: 'json',
					description: 'Whether or not to view the raw JSON message data.',
					match: 'flag',
					flag: '--json',
					prompt: 'Would you like to view the raw JSON message data?',
					slashType: ApplicationCommandOptionType.Boolean,
					optional: true
				},
				{
					id: 'js',
					description: 'Whether or not to view the raw message data.',
					match: 'flag',
					flag: '--js',
					prompt: 'Would you like to view the raw message data?',
					slashType: ApplicationCommandOptionType.Boolean,
					optional: true
				}
			],
			slash: true,
			channel: 'guild',
			clientPermissions: ['EmbedLinks'],
			clientCheckChannel: true,
			userPermissions: []
		});
	}

	public override async exec(
		message: CommandMessage | SlashMessage,
		args: {
			message: ArgType<'message' | 'messageLink'>;
			channel: OptArgType<'textBasedChannel'>;
			json: ArgType<'flag'>;
			js: ArgType<'flag'>;
		}
	) {
		assert(message.inGuild());

		args.channel ??= message.channel;

		const newMessage =
			args.message instanceof Message ? args.message : await args.channel!.messages.fetch(`${args.message}`).catch(() => null);
		if (!newMessage)
			return await message.util.reply(
				`${emojis.error} There was an error fetching that message, make sure that is a valid id and if the message is not in this channel, please provide a channel.`
			);

		const Embed = await getRawData(newMessage, { json: args.json, js: args.js });

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

export async function getRawData(message: Message, options: { json?: boolean; js: boolean }): Promise<EmbedBuilder> {
	const content =
		options.json || options.js
			? options.json
				? JSON.stringify(message.toJSON(), undefined, 2)
				: inspect(message.toJSON()) || '[No Content]'
			: message.content || '[No Content]';
	const lang = options.json ? 'json' : options.js ? 'js' : undefined;
	return new EmbedBuilder()
		.setFooter({ text: message.author.tag, iconURL: message.author.avatarURL() ?? undefined })
		.setTimestamp(message.createdTimestamp)
		.setColor(message.member?.roles?.color?.color ?? colors.default)
		.setTitle('Raw Message Information')
		.setDescription(await message.client.utils.codeblock(content, 2048, lang));
}