Skip to content

Commit 952fe82

Browse files
committed
Cleanup more of our code
1 parent 6582c1c commit 952fe82

File tree

7 files changed

+44
-125
lines changed

7 files changed

+44
-125
lines changed
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
11
import { Command, CommandClient } from 'detritus-client';
22

3-
import { imageManipulationGrayscale } from '../../../api';
43
import { CommandCategories } from '../../../constants';
5-
import { imageReply } from '../../../utils';
4+
import { Formatter } from '../../../utils';
65

76
import { BaseImageCommand } from '../basecommand';
87

98

10-
export interface CommandArgsBefore {
11-
url?: null | string,
12-
}
13-
14-
export interface CommandArgs {
15-
url: string,
16-
}
17-
189
export const COMMAND_NAME = 'grayscale';
1910

20-
export default class GrayscaleCommand extends BaseImageCommand<CommandArgs> {
11+
export default class GrayscaleCommand extends BaseImageCommand {
2112
constructor(client: CommandClient) {
2213
super(client, {
2314
name: COMMAND_NAME,
2415

2516
aliases: ['greyscale'],
2617
metadata: {
18+
category: CommandCategories.IMAGE,
2719
description: 'Grayscale an Image',
2820
examples: [
2921
COMMAND_NAME,
3022
`${COMMAND_NAME} notsobot`,
3123
],
32-
category: CommandCategories.IMAGE,
24+
id: Formatter.Commands.ImageGrayscale.COMMAND_ID,
3325
usage: '?<emoji,user:id|mention|name,url>',
3426
},
3527
});
3628
}
3729

38-
async run(context: Command.Context, args: CommandArgs) {
39-
const response = await imageManipulationGrayscale(context, args);
40-
return imageReply(context, response);
30+
async run(context: Command.Context, args: Formatter.Commands.ImageGrayscale.CommandArgs) {
31+
return Formatter.Commands.ImageGrayscale.createMessage(context, args);
4132
}
4233
}
Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
import { Command, CommandClient } from 'detritus-client';
22

3-
import { imageToolsRotate } from '../../../api';
43
import { CommandCategories } from '../../../constants';
5-
import { imageReply } from '../../../utils';
4+
import { Formatter } from '../../../utils';
65

76
import { BaseImageCommand } from '../basecommand';
87

98

10-
export interface CommandArgsBefore {
11-
degrees?: number,
12-
crop?: boolean,
13-
url?: null | string,
14-
}
15-
16-
export interface CommandArgs {
17-
degrees?: number,
18-
crop?: boolean,
19-
url: string,
20-
}
21-
229
export const COMMAND_NAME = 'rotate';
2310

24-
export default class RotateCommand extends BaseImageCommand<CommandArgs> {
11+
export default class RotateCommand extends BaseImageCommand {
2512
constructor(client: CommandClient) {
2613
super(client, {
2714
name: COMMAND_NAME,
@@ -31,25 +18,21 @@ export default class RotateCommand extends BaseImageCommand<CommandArgs> {
3118
{name: 'crop', type: Boolean},
3219
],
3320
metadata: {
21+
category: CommandCategories.IMAGE,
3422
description: 'Rotate an Image (default 90 degrees)',
3523
examples: [
3624
COMMAND_NAME,
3725
`${COMMAND_NAME} notsobot`,
3826
`${COMMAND_NAME} notsobot -degrees 90`,
3927
`${COMMAND_NAME} notsobot -degrees 90 -nocrop`,
4028
],
41-
category: CommandCategories.IMAGE,
29+
id: Formatter.Commands.ImageToolsRotate.COMMAND_ID,
4230
usage: '?<emoji,user:id|mention|name,url> (-degrees <number>) (-nocrop)',
4331
},
4432
});
4533
}
4634

47-
async run(context: Command.Context, args: CommandArgs) {
48-
const response = await imageToolsRotate(context, {
49-
crop: args.crop,
50-
degrees: args.degrees,
51-
url: args.url,
52-
});
53-
return imageReply(context, response);
35+
async run(context: Command.Context, args: Formatter.Commands.ImageToolsRotate.CommandArgs) {
36+
return Formatter.Commands.ImageToolsRotate.createMessage(context, args);
5437
}
5538
}
Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,32 @@
11
import { Command, CommandClient } from 'detritus-client';
2-
import { Markup } from 'detritus-client/lib/utils';
32

4-
import { imageInformationExif } from '../../../api';
5-
import { RestResponsesRaw } from '../../../api/types';
6-
import { CommandCategories, EmbedBrands, EmbedColors } from '../../../constants';
7-
import { createUserEmbed, editOrReply, formatMemory, splitArray } from '../../../utils';
3+
import { CommandCategories } from '../../../constants';
4+
import { Formatter } from '../../../utils';
85

96
import { BaseImageCommand } from '../basecommand';
107

118

12-
export interface CommandArgsBefore {
13-
url?: null | string,
14-
}
15-
16-
export interface CommandArgs {
17-
url: string,
18-
}
19-
209
export const COMMAND_NAME = 'exif';
2110

22-
export default class ExifCommand extends BaseImageCommand<CommandArgs> {
11+
export default class ExifCommand extends BaseImageCommand {
2312
constructor(client: CommandClient) {
2413
super(client, {
2514
name: COMMAND_NAME,
2615

2716
metadata: {
17+
category: CommandCategories.TOOLS,
2818
description: 'Get exif information from an image',
2919
examples: [
3020
COMMAND_NAME,
3121
`${COMMAND_NAME} https://i.imgur.com/WwiO7Bx.jpg`,
3222
],
33-
category: CommandCategories.IMAGE,
23+
id: Formatter.Commands.ToolsExif.COMMAND_ID,
3424
usage: '?<emoji,user:id|mention|name,url>',
3525
},
3626
});
3727
}
3828

39-
async run(context: Command.Context, args: CommandArgs) {
40-
const response = await imageInformationExif(context, args);
41-
const isGif = (response.information.mimetype === 'image/gif');
42-
43-
const embed = createUserEmbed(context.user);
44-
embed.setColor(EmbedColors.DEFAULT);
45-
embed.setFooter('Image Exif Information', EmbedBrands.NOTSOBOT);
46-
47-
{
48-
const { information, metadata } = response;
49-
50-
const description: Array<string> = [];
51-
description.push(`**Color Profile**: ${information.interpretation}`);
52-
description.push(`**Dimensions**: ${information.width}x${information.height}`);
53-
if (isGif) {
54-
description.push(`**Frames**: ${information.frames.toLocaleString()}`);
55-
description.push(`**Frame Delay**: ${information.delay.toLocaleString()}`);
56-
description.push(`**Loops**: ${(information.loop) ? 'Yes' : 'No'}`);
57-
}
58-
description.push(`**Mimetype**: ${information.mimetype}`);
59-
description.push(`**Size**: ${formatMemory(information.size)}`);
60-
61-
if (metadata['gif-comment']) {
62-
description.push('');
63-
description.push(`**Comment**`);
64-
description.push(Markup.codeblock(metadata['gif-comment']))
65-
}
66-
67-
embed.setDescription(description.join('\n'));
68-
}
69-
70-
if (response.exif.length) {
71-
const exif = response.exif.map((field) => {
72-
field.name = field.name.split('-').pop() as string;
73-
return field;
74-
}).sort((x, y) => x.name.localeCompare(y.name));
75-
76-
const split = splitArray<any>(exif, 3);
77-
for (let i = 0; i < split.length; i++) {
78-
const fields = split[i];
79-
const description: Array<string> = [];
80-
81-
for (let field of fields) {
82-
let value: string;
83-
if (field.description === field.value) {
84-
value = field.value;
85-
} else {
86-
value = `${field.value} (${field.description})`;
87-
}
88-
if (value) {
89-
description.push(`**${field.name.split('-').pop()}**`);
90-
description.push(`-> ${value}`);
91-
}
92-
}
93-
94-
const title = (!i) ? 'Exif Data' : '\u200b';
95-
embed.addField(title, description.join('\n') || '\u200b', true);
96-
}
97-
if (response.url) {
98-
embed.setImage(response.url);
99-
}
100-
} else {
101-
if (response.url) {
102-
embed.setThumbnail(response.url);
103-
}
104-
}
105-
106-
return editOrReply(context, {embed});
29+
async run(context: Command.Context, args: Formatter.Commands.ToolsExif.CommandArgs) {
30+
return Formatter.Commands.ToolsExif.createMessage(context, args);
10731
}
10832
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Command, Interaction } from 'detritus-client';
2+
3+
import { imageManipulationGrayscale } from '../../../api';
4+
import { imageReply } from '../../../utils';
5+
6+
7+
export const COMMAND_ID = 'image.grayscale';
8+
9+
export interface CommandArgs {
10+
url: string,
11+
}
12+
13+
export async function createMessage(
14+
context: Command.Context | Interaction.InteractionContext,
15+
args: CommandArgs,
16+
) {
17+
const response = await imageManipulationGrayscale(context, args);
18+
return imageReply(context, response);
19+
}

src/utils/formatter/commands/image.tools.rotate.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import { imageReply } from '../../../utils';
77
export const COMMAND_ID = 'image.tools.rotate';
88

99
export interface CommandArgs {
10-
amount?: number,
11-
iterations?: number,
12-
seed?: number,
10+
crop?: boolean,
11+
degrees?: number,
1312
url: string,
1413
}
1514

src/utils/formatter/commands/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as ImageGifSeeSaw from './image.gif.seesaw';
2222
import * as ImageGifSpeed from './image.gif.speed';
2323
import * as ImageGlitch from './image.glitch';
2424
import * as ImageGold from './image.gold';
25+
import * as ImageGrayscale from './image.grayscale';
2526
import * as ImageImplode from './image.implode';
2627
import * as ImageInvert from './image.invert';
2728
import * as ImageLegofy from './image.legofy';
@@ -82,6 +83,7 @@ import * as TagRemove from './tag.remove';
8283
import * as TagShow from './tag.show';
8384

8485
import * as ToolsCode from './tools.code';
86+
import * as ToolsExif from './tools.exif';
8587
import * as ToolsHash from './tools.hash';
8688
import * as ToolsOCR from './tools.ocr';
8789
import * as ToolsOCRTranslate from './tools.ocrtranslate';
@@ -117,6 +119,7 @@ export {
117119
ImageGifSpeed,
118120
ImageGlitch,
119121
ImageGold,
122+
ImageGrayscale,
120123
ImageImplode,
121124
ImageInvert,
122125
ImageLegofy,
@@ -169,6 +172,7 @@ export {
169172
TagRemove,
170173
TagShow,
171174
ToolsCode,
175+
ToolsExif,
172176
ToolsHash,
173177
ToolsOCR,
174178
ToolsOCRTranslate,

src/utils/formatter/commands/tools.exif.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { createUserEmbed, editOrReply, formatMemory, splitArray } from '../../..
1010
export const COMMAND_ID = 'tools.exif';
1111

1212
export interface CommandArgs {
13-
isEphemeral?: boolean,
1413
url: string,
1514
}
1615

0 commit comments

Comments
 (0)