Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --server flag to .saveemote command #488

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Powercord/plugins/pc-emojiUtility/components/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ module.exports = class EmojiUtilitySettings extends React.Component {
Save directory
</TextInput>

<SwitchItem
note='Whether a separate folder should be created when downloading emotes with the --server flag.'
value={this.props.getSetting('createGuildFolders')}
onChange={() => this.props.toggleSetting('createGuildFolders')}
>
Create separate folder when exporting with --server flag
</SwitchItem>

<SwitchItem
note='Whether saving emotes should contain the id of the emote, this prevents overwriting old saved emotes.'
value={this.props.getSetting('includeIdForSavedEmojis')}
Expand Down
45 changes: 33 additions & 12 deletions src/Powercord/plugins/pc-emojiUtility/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ const { inject, uninject } = require('powercord/injector');
const { open: openModal } = require('powercord/modal');

const { writeFile } = require('fs').promises;
const { existsSync } = require('fs');
const { existsSync, mkdirSync } = require('fs');

const { get } = require('powercord/http');
const { extname, resolve } = require('path');
const { extname, resolve, join } = require('path');
const { parse } = require('url');

const { clipboard } = require('electron');
Expand Down Expand Up @@ -225,6 +225,7 @@ module.exports = class EmojiUtility extends Plugin {
/* Default settings */
this.settings.set('useEmbeds', this.settings.get('useEmbeds', false));
this.settings.set('displayLink', this.settings.get('displayLink', true));
this.settings.set('createGuildFolder', this.settings.get('createGuildFolder', true));
this.settings.set('includeIdForSavedEmojis', this.settings.get('includeIdForSavedEmojis', true));
this.settings.set('defaultCloneIdUseCurrent', this.settings.get('defaultCloneIdUseCurrent', false));

Expand Down Expand Up @@ -616,22 +617,42 @@ module.exports = class EmojiUtility extends Plugin {
powercord.api.commands.registerCommand({
command: 'saveemote',
description: 'Save emotes to a specified directory',
usage: '{c} [emote]',
usage: '{c} [--server | emote]',
executor: async (args) => {
if (!this.settings.get('filePath')) {
let filePath = this.settings.get('filePath');
if (!filePath) {
return this.replyError('Please set your save directory in the settings');
}

if (!existsSync(this.settings.get('filePath'))) {
if (!existsSync(filePath)) {
return this.replyError('The specified save directory does no longer exist, please update it in the settings');
}

const object = this.findEmojisForCommand(args);
if (!object) {
return;
}
let foundEmojis, notFoundEmojis;
if (args.includes('--server')) {
const { guild_id } = this.getChannel(this.getChannelId());
if (!guild_id) {
return this.replyError('The --server flag can not be used in dms');
}

const { foundEmojis, notFoundEmojis } = object;
if (this.settings.get('createGuildFolders')) {
const guild = this.getGuildByIdOrName(guild_id);

filePath = join(filePath, guild.name);
if (!existsSync(filePath)) {
mkdirSync(filePath);
}
}

foundEmojis = this.getEmojis(guild_id);
notFoundEmojis = [];
} else {
const object = this.findEmojisForCommand(args);
if (!object) {
return;
}
({ foundEmojis, notFoundEmojis } = object);
}

if (notFoundEmojis.length > 0) {
return this.replyError(`**${notFoundEmojis.length}** of the provided arguments ${notFoundEmojis.length === 1 ? 'is not a custom emote' : 'are not custom emotes'}`);
Expand All @@ -642,7 +663,7 @@ module.exports = class EmojiUtility extends Plugin {
try {
const name = this.settings.get('includeIdForSavedEmojis') ? `${emoji.name} (${emoji.id})` : emoji.name;

await writeFile(resolve(this.settings.get('filePath'), name + extname(parse(emoji.url).pathname)), (await get(emoji.url)).raw);
await writeFile(resolve(filePath, name + extname(parse(emoji.url).pathname)), (await get(emoji.url)).raw);

this.replySuccess(`Downloaded ${this.getFullEmoji(emoji)}`);
} catch (error) {
Expand All @@ -665,7 +686,7 @@ module.exports = class EmojiUtility extends Plugin {
try {
const name = this.settings.get('includeIdForSavedEmojis') ? `${emoji.name} (${emoji.id})` : emoji.name;

await writeFile(resolve(this.settings.get('filePath'), name + extname(parse(emoji.url).pathname)), (await get(emoji.url)).raw);
await writeFile(resolve(filePath, name + extname(parse(emoji.url).pathname)), (await get(emoji.url)).raw);
} catch (error) {
console.error(error);

Expand Down