aboutsummaryrefslogtreecommitdiff
path: root/src/commands/utilities/steal.ts
blob: 820fa89f06984b8c9be1a72c7648cb5f93300406 (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
import { BushCommand, type ArgType, type BushMessage, type BushSlashMessage } from '#lib';
import assert from 'assert';
import { type ArgumentOptions, type ArgumentType, type ArgumentTypeCaster, type Flag } from 'discord-akairo';
import { ApplicationCommandOptionType, PermissionFlagsBits } from 'discord.js';
import _ from 'lodash';
import { URL } from 'url';

assert(_);

export default class StealCommand extends BushCommand {
	public constructor() {
		super('steal', {
			aliases: ['steal', 'copy-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'],
			args: [
				{
					id: 'emoji',
					description: 'The emoji to steal.',
					type: util.arg.union('discordEmoji', 'snowflake', 'url'),
					readableType: 'discordEmoji|snowflake|url',
					prompt: 'What emoji would you like to steal?',
					retry: '{error} Pick a valid emoji, emoji id, or image url.',
					optional: true,
					only: 'slash',
					slashType: ApplicationCommandOptionType.String
				},
				{
					id: 'name',
					description: 'The name to give the new emoji.',
					prompt: 'What would you like to name the emoji?',
					retry: '{error} Choose a valid name fore the emoji.',
					optional: true,
					only: 'slash',
					slashType: ApplicationCommandOptionType.String
				}
			],
			slash: true,
			channel: 'guild',
			clientPermissions: (m) => util.clientSendAndPermCheck(m, [PermissionFlagsBits.ManageEmojisAndStickers]),
			userPermissions: [PermissionFlagsBits.ManageEmojisAndStickers]
		});
	}

	public override *args(message: BushMessage): Generator<ArgumentOptions | Flag> {
		const hasImage = message.attachments.size && message.attachments.first()?.contentType?.includes('image/');

		const emoji = hasImage
			? message.attachments.first()!.url
			: yield {
					id: 'emoji',
					type: util.arg.union('discordEmoji', 'snowflake', 'url') as ArgumentType | ArgumentTypeCaster,
					prompt: {
						start: 'What emoji would you like to steal?',
						retry: '{error} Pick a valid emoji, emoji id, or image url.'
					}
			  };

		const name = yield {
			id: 'name',
			prompt: {
				start: 'What would you like to name the emoji?',
				retry: '{error} Choose a valid name fore the emoji.',
				optional: true
			},
			default:
				hasImage && message.attachments.first()?.name
					? _.camelCase(message.attachments.first()!.name ?? 'stolen_emoji')
					: 'stolen_emoji'
		};

		return { emoji, name };
	}

	public override async exec(
		message: BushMessage | BushSlashMessage,
		args?: { emoji?: ArgType<'discordEmoji'> | ArgType<'snowflake'> | ArgType<'url'> | string; name: string }
	) {
		if (!args || !args.emoji) return await message.util.reply(`${util.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}`
				: client.consts.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(`${util.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 creationSuccess = await message
			.guild!.emojis.create(image, emojiName, {
				reason: `Stolen by ${message.author.tag} (${message.author.id})`
			})
			.catch((e: Error) => e);

		if (!(creationSuccess instanceof Error))
			return await message.util.reply(`${util.emojis.success} You successfully stole ${creationSuccess}.`);
		else {
			return await message.util.reply(
				`${util.emojis.error} The was an error stealing that emoji \`${creationSuccess.message}\`.`
			);
		}
	}
}