aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/steal.ts
blob: a2089202e04c7167c883a99c7e5f500f9a2f5e0f (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
import { Arg, BotCommand, emojis, format, OptArgType, regex, type CommandMessage, type SlashMessage } from '#lib';
import assert from 'assert/strict';
import { type ArgumentGeneratorReturn, type ArgumentType, type ArgumentTypeCaster } from 'discord-akairo';
import { ApplicationCommandOptionType, Attachment } from 'discord.js';
import _ from 'lodash';
import { Stream } from 'stream';
import { URL } from 'url';

assert(_);

// so I don't have to retype things
const enum lang {
	emojiStart = 'What emoji would you like to steal?',
	emojiRetry = '{error} Pick a valid emoji, emoji id, or image url.',
	emojiDescription = 'The emoji to steal.',
	nameStart = 'What would you like to name the emoji?',
	nameRetry = '{error} Choose a valid name fore the emoji.',
	nameDescription = 'The name to give the new emoji.'
}

export default class StealCommand extends BotCommand {
	public constructor() {
		super('steal', {
			aliases: ['steal', 'copy-emoji', 'emoji'],
			category: 'utilities',
			description: 'Steal an emoji from another server and add it to your own.',
			usage: ['steal <emoji/emojiId/url> [name]'],
			examples: ['steal <:omegaclown:782630946435366942> ironm00n'],
			slashOptions: [
				{ name: 'emoji', description: lang.emojiStart, type: ApplicationCommandOptionType.Attachment, required: true },
				{ name: 'name', description: lang.nameStart, type: ApplicationCommandOptionType.String, required: false }
			],
			helpArgs: [
				{ name: 'emoji', description: lang.emojiDescription, type: 'emoji|emojiId|url', optional: false },
				{ name: 'name', description: lang.nameDescription, type: 'string', optional: true }
			],
			slash: true,
			channel: 'guild',
			clientPermissions: ['ManageEmojisAndStickers'],
			userPermissions: ['ManageEmojisAndStickers']
		});
	}

	public override *args(message: CommandMessage): ArgumentGeneratorReturn {
		const hasImage = message.attachments.size && message.attachments.first()?.contentType?.includes('image/');

		const emoji = hasImage
			? message.attachments.first()!.url
			: yield {
					type: Arg.union('discordEmoji', 'snowflake', 'url') as ArgumentType | ArgumentTypeCaster,
					prompt: { start: lang.emojiStart, retry: lang.emojiRetry }
			  };

		const name = yield {
			prompt: { start: lang.nameStart, retry: lang.nameRetry, optional: true },
			default: hasImage && message.attachments.first()!.name ? _.snakeCase(message.attachments.first()!.name!) : 'unnamed_emoji'
		};

		return { emoji, name };
	}

	public override async exec(
		message: CommandMessage,
		args: { emoji: OptArgType<'discordEmoji' | 'snowflake' | 'url'>; name: OptArgType<'string'> }
	) {
		assert(message.inGuild());

		if (!args.emoji) return await message.util.reply(`${emojis.error} You must provide an emoji to steal.`);

		const image =
			args.emoji instanceof URL
				? args.emoji.href
				: typeof args.emoji === 'object'
				? `https://cdn.discordapp.com/emojis/${args.emoji.id}`
				: regex.snowflake.test(args.emoji ?? '')
				? `https://cdn.discordapp.com/emojis/${args!.emoji}`
				: (args.emoji ?? '').match(/https?:\/\//)
				? args.emoji
				: undefined;

		if (image === undefined) return await message.util.reply(`${emojis.error} You must provide an emoji to steal.`);

		const emojiName =
			args.name ?? args.emoji instanceof URL
				? args.name ?? 'stolen_emoji'
				: typeof args.emoji === 'object'
				? args.name ?? args.emoji.name ?? 'stolen_emoji'
				: 'stolen_emoji';

		const res = await message.guild.emojis
			.create({
				attachment: image,
				name: emojiName,
				reason: `Stolen by ${message.author.tag} (${message.author.id})`
			})
			.catch((e: Error) => e);

		if (!(res instanceof Error)) {
			return await message.util.reply(
				`${emojis.success} You successfully stole ${res} (${format.input(res.name ?? '[emoji has no name]')}).`
			);
		} else {
			return await message.util.reply(`${emojis.error} The was an error stealing that emoji: ${format.input(res.message)}.`);
		}
	}

	public override async execSlash(message: SlashMessage, args: { emoji: Attachment; name: string | null }) {
		assert(message.inGuild());

		const name = args.name ?? args.emoji.name ?? 'stolen_emoji';

		const data =
			args.emoji.attachment instanceof Stream
				? await (new Promise((resolve, reject) => {
						let data = '';
						assert(args.emoji.attachment instanceof Stream);
						args.emoji.attachment.on('data', (chunk) => (data += chunk));
						args.emoji.attachment.on('end', () => resolve(data));
						args.emoji.attachment.on('error', (e) => reject(e));
				  }) as Promise<string>)
				: args.emoji.attachment;

		const res = await message.guild.emojis
			.create({
				attachment: data,
				name: name,
				reason: `Stolen by ${message.author.tag} (${message.author.id})`
			})
			.catch((e: Error) => e);

		if (!(res instanceof Error)) {
			return await message.util.reply(
				`${emojis.success} You successfully stole ${res} (${format.input(res.name ?? '[emoji has no name]')}).`
			);
		} else {
			return await message.util.reply(`${emojis.error} The was an error stealing that emoji: ${format.input(res.message)}.`);
		}
	}
}