Skip to content

Commit 84a4064

Browse files
committed
refactor: rewrite lockdown command
1 parent e7f4bb4 commit 84a4064

File tree

11 files changed

+361
-129
lines changed

11 files changed

+361
-129
lines changed

Diff for: package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"main": "./dist/Skyra.js",
88
"type": "module",
99
"imports": {
10+
"#utils/common": "./dist/lib/util/common/index.js",
11+
"#utils/functions": "./dist/lib/util/functions/index.js",
12+
"#utils/resolvers": "./dist/lib/util/resolvers/index.js",
13+
"#utils/*": "./dist/lib/util/*.js",
1014
"#lib/database": "./dist/lib/database/index.js",
1115
"#lib/database/entities": "./dist/lib/database/entities/index.js",
1216
"#lib/database/keys": "./dist/lib/database/keys/index.js",
@@ -22,9 +26,6 @@
2226
"#lib/i18n/languageKeys": "./dist/lib/i18n/languageKeys/index.js",
2327
"#lib/*": "./dist/lib/*.js",
2428
"#languages": "./dist/languages/index.js",
25-
"#utils/common": "./dist/lib/util/common/index.js",
26-
"#utils/functions": "./dist/lib/util/functions/index.js",
27-
"#utils/*": "./dist/lib/util/*.js",
2829
"#root/*": "./dist/*.js"
2930
},
3031
"scripts": {

Diff for: src/arguments/timespan.ts

+3-31
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,9 @@
1-
import { LanguageKeys } from '#lib/i18n/languageKeys';
2-
import { seconds } from '#utils/common';
1+
import { resolveTimeSpan } from '#utils/resolvers';
32
import { Argument } from '@sapphire/framework';
4-
import { Duration } from '@sapphire/time-utilities';
53

64
export class UserArgument extends Argument<number> {
75
public run(parameter: string, context: Argument.Context) {
8-
const duration = this.parseParameter(parameter);
9-
10-
if (!Number.isSafeInteger(duration)) {
11-
return this.error({ parameter, identifier: LanguageKeys.Arguments.TimeSpan, context });
12-
}
13-
14-
if (typeof context.minimum === 'number' && duration < context.minimum) {
15-
return this.error({ parameter, identifier: LanguageKeys.Arguments.TimeSpanTooSmall, context });
16-
}
17-
18-
if (typeof context.maximum === 'number' && duration > context.maximum) {
19-
return this.error({ parameter, identifier: LanguageKeys.Arguments.TimeSpanTooBig, context });
20-
}
21-
22-
return this.ok(duration);
23-
}
24-
25-
private parseParameter(parameter: string): number {
26-
const number = Number(parameter);
27-
if (!Number.isNaN(number)) return seconds(number);
28-
29-
const duration = new Duration(parameter).offset;
30-
if (!Number.isNaN(duration)) return duration;
31-
32-
const date = Date.parse(parameter);
33-
if (!Number.isNaN(date)) return date - Date.now();
34-
35-
return NaN;
6+
return resolveTimeSpan(parameter, { minimum: context.minimum, maximum: context.maximum }) //
7+
.mapErrInto((identifier) => this.error({ parameter, identifier, context }));
368
}
379
}

Diff for: src/commands/Moderation/lockdown.ts

+251-90
Large diffs are not rendered by default.

Diff for: src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function parseInternationalizationOptions(): InternationalizationOptions {
194194
load: 'all',
195195
lng: 'en-US',
196196
fallbackLng: {
197-
'es-419': ['es-ES'], // Latin America Spanish falls back to Spain Spanish
197+
'es-419': ['es-ES', 'en-US'], // Latin America Spanish falls back to Spain Spanish
198198
default: ['en-US']
199199
},
200200
defaultNS: 'globals',

Diff for: src/languages/en-US/commands/lockdown.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "lockdown",
3+
"description": "Manage the server's lockdown status",
4+
"actionName": "action",
5+
"actionDescription": "The action to perform",
6+
"channelName": "channel",
7+
"channelDescription": "The channel to lock down",
8+
"durationName": "duration",
9+
"durationDescription": "How long the lockdown should last",
10+
"roleName": "role",
11+
"roleDescription": "The role to use for the lockdown",
12+
"globalName": "global",
13+
"globalDescription": "⚠️ Whether or not to apply the lockdown to the entire server",
14+
"actionLock": "Lock",
15+
"actionUnlock": "Unlock"
16+
}

Diff for: src/lib/i18n/languageKeys/keys/Commands.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * as Admin from '#lib/i18n/languageKeys/keys/commands/Admin';
22
export * as Fun from '#lib/i18n/languageKeys/keys/commands/Fun';
33
export * as Games from '#lib/i18n/languageKeys/keys/commands/Games';
44
export * as General from '#lib/i18n/languageKeys/keys/commands/General';
5+
export * as Lockdown from '#lib/i18n/languageKeys/keys/commands/Lockdown';
56
export * as Management from '#lib/i18n/languageKeys/keys/commands/Management';
67
export * as Misc from '#lib/i18n/languageKeys/keys/commands/Misc';
78
export * as Moderation from '#lib/i18n/languageKeys/keys/commands/Moderation';

Diff for: src/lib/i18n/languageKeys/keys/commands/Lockdown.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { FT, T } from '#lib/types';
2+
import type { ChannelMention, RoleMention } from 'discord.js';
3+
4+
// Root
5+
export const Name = T('commands/lockdown:name');
6+
export const Description = T('commands/lockdown:description');
7+
8+
// Options
9+
export const Action = 'commands/lockdown:action';
10+
export const Channel = 'commands/lockdown:channel';
11+
export const Duration = 'commands/lockdown:duration';
12+
export const Role = 'commands/lockdown:role';
13+
export const Global = 'commands/lockdown:global';
14+
15+
// Action choices
16+
export const ActionLock = T('commands/lockdown:actionLock');
17+
export const ActionUnlock = T('commands/lockdown:actionUnlock');
18+
19+
export const AuditLogRequestedBy = FT<{ user: string; channel: ChannelMention }>('commands/lockdown:auditLogRequestedBy');
20+
21+
// Guild
22+
export const GuildLocked = FT<{ role: RoleMention }>('commands/lockdown:guildLocked');
23+
export const SuccessGuild = FT<{ role: RoleMention }>('commands/lockdown:successGuild');
24+
export const GuildUnknownRole = FT<{ role: RoleMention }>('commands/lockdown:guildUnknownRole');
25+
export const GuildLockFailed = FT<{ role: RoleMention }>('commands/lockdown:guildLockFailed');
26+
27+
// Thread
28+
export const SuccessThread = FT<{ channel: ChannelMention }>('commands/lockdown:successThread');
29+
export const ThreadLocked = FT<{ channel: ChannelMention }>('commands/lockdown:threadLocked');
30+
export const ThreadUnmanageable = FT<{ channel: ChannelMention }>('commands/lockdown:threadUnmanageable');
31+
export const ThreadUnknownChannel = FT<{ channel: ChannelMention }>('commands/lockdown:threadUnknownChannel');
32+
export const ThreadLockFailed = FT<{ channel: ChannelMention }>('commands/lockdown:threadLockFailed');
33+
34+
// Channel
35+
export const SuccessChannel = FT<{ channel: ChannelMention }>('commands/lockdown:successChannel');
36+
export const ChannelLocked = FT<{ channel: ChannelMention }>('commands/lockdown:channelLocked');
37+
export const ChannelUnmanageable = FT<{ channel: ChannelMention }>('commands/lockdown:channelUnmanageable');
38+
export const ChannelUnknownChannel = FT<{ channel: ChannelMention }>('commands/lockdown:channelUnknownChannel');
39+
export const ChannelLockFailed = FT<{ channel: ChannelMention }>('commands/lockdown:channelLockFailed');

Diff for: src/lib/util/functions/pieces.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Piece } from '@sapphire/framework';
22
import { bgBlue, bgRed } from 'colorette';
33

44
export function getLogPrefix(piece: Piece | string) {
5-
return bgBlue(piece instanceof Piece ? `[ ${piece.store.name} => ${piece.name} ]` : `[ ${piece} ]`);
5+
return bgBlue(piece instanceof Piece ? `[ ${piece.name} ]` : `[ ${piece} ]`);
66
}
77

88
export function getCodeStyle(code: string | number) {

Diff for: src/lib/util/resolvers/TimeSpan.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { LanguageKeys } from '#lib/i18n/languageKeys';
2+
import { seconds } from '#utils/common';
3+
import { err, ok } from '@sapphire/framework';
4+
import { Duration } from '@sapphire/time-utilities';
5+
6+
export function resolveTimeSpan(parameter: string, options?: TimeSpanOptions) {
7+
const duration = parse(parameter);
8+
9+
if (!Number.isSafeInteger(duration)) {
10+
return err(LanguageKeys.Arguments.TimeSpan);
11+
}
12+
13+
if (typeof options?.minimum === 'number' && duration < options.minimum) {
14+
return err(LanguageKeys.Arguments.TimeSpanTooSmall);
15+
}
16+
17+
if (typeof options?.maximum === 'number' && duration > options.maximum) {
18+
return err(LanguageKeys.Arguments.TimeSpanTooBig);
19+
}
20+
21+
return ok(duration);
22+
}
23+
24+
function parse(parameter: string) {
25+
const number = Number(parameter);
26+
if (!Number.isNaN(number)) return seconds(number);
27+
28+
const duration = new Duration(parameter).offset;
29+
if (!Number.isNaN(duration)) return duration;
30+
31+
const date = Date.parse(parameter);
32+
if (!Number.isNaN(date)) return date - Date.now();
33+
34+
return NaN;
35+
}
36+
37+
export interface TimeSpanOptions {
38+
minimum?: number;
39+
maximum?: number;
40+
}

Diff for: src/lib/util/resolvers/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '#utils/resolvers/TimeSpan';

Diff for: src/tsconfig.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
"rootDir": ".",
77
"baseUrl": ".",
88
"paths": {
9+
"#utils/common": ["lib/util/common/index.js"],
10+
"#utils/functions": ["lib/util/functions/index.js"],
11+
"#utils/resolvers": ["lib/util/functions/resolvers.js"],
12+
"#utils/*": ["lib/util/*"],
913
"#lib/database": ["lib/database/index.js"],
1014
"#lib/database/entities": ["lib/database/entities/index.js"],
1115
"#lib/database/keys": ["lib/database/keys/index.js"],
@@ -21,9 +25,6 @@
2125
"#lib/i18n/languageKeys": ["lib/i18n/languageKeys/index.js"],
2226
"#lib/*": ["lib/*"],
2327
"#languages": ["languages/index.js"],
24-
"#utils/common": ["lib/util/common/index.js"],
25-
"#utils/functions": ["lib/util/functions/index.js"],
26-
"#utils/*": ["lib/util/*"],
2728
"#root/*": ["*"]
2829
}
2930
},

0 commit comments

Comments
 (0)