Skip to content

Commit

Permalink
refactor: rewrite lockdown command
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Mar 14, 2024
1 parent e7f4bb4 commit 84a4064
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 129 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"main": "./dist/Skyra.js",
"type": "module",
"imports": {
"#utils/common": "./dist/lib/util/common/index.js",
"#utils/functions": "./dist/lib/util/functions/index.js",
"#utils/resolvers": "./dist/lib/util/resolvers/index.js",
"#utils/*": "./dist/lib/util/*.js",
"#lib/database": "./dist/lib/database/index.js",
"#lib/database/entities": "./dist/lib/database/entities/index.js",
"#lib/database/keys": "./dist/lib/database/keys/index.js",
Expand All @@ -22,9 +26,6 @@
"#lib/i18n/languageKeys": "./dist/lib/i18n/languageKeys/index.js",
"#lib/*": "./dist/lib/*.js",
"#languages": "./dist/languages/index.js",
"#utils/common": "./dist/lib/util/common/index.js",
"#utils/functions": "./dist/lib/util/functions/index.js",
"#utils/*": "./dist/lib/util/*.js",
"#root/*": "./dist/*.js"
},
"scripts": {
Expand Down
34 changes: 3 additions & 31 deletions src/arguments/timespan.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
import { LanguageKeys } from '#lib/i18n/languageKeys';
import { seconds } from '#utils/common';
import { resolveTimeSpan } from '#utils/resolvers';
import { Argument } from '@sapphire/framework';
import { Duration } from '@sapphire/time-utilities';

export class UserArgument extends Argument<number> {
public run(parameter: string, context: Argument.Context) {
const duration = this.parseParameter(parameter);

if (!Number.isSafeInteger(duration)) {
return this.error({ parameter, identifier: LanguageKeys.Arguments.TimeSpan, context });
}

if (typeof context.minimum === 'number' && duration < context.minimum) {
return this.error({ parameter, identifier: LanguageKeys.Arguments.TimeSpanTooSmall, context });
}

if (typeof context.maximum === 'number' && duration > context.maximum) {
return this.error({ parameter, identifier: LanguageKeys.Arguments.TimeSpanTooBig, context });
}

return this.ok(duration);
}

private parseParameter(parameter: string): number {
const number = Number(parameter);
if (!Number.isNaN(number)) return seconds(number);

const duration = new Duration(parameter).offset;
if (!Number.isNaN(duration)) return duration;

const date = Date.parse(parameter);
if (!Number.isNaN(date)) return date - Date.now();

return NaN;
return resolveTimeSpan(parameter, { minimum: context.minimum, maximum: context.maximum }) //
.mapErrInto((identifier) => this.error({ parameter, identifier, context }));
}
}
341 changes: 251 additions & 90 deletions src/commands/Moderation/lockdown.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function parseInternationalizationOptions(): InternationalizationOptions {
load: 'all',
lng: 'en-US',
fallbackLng: {
'es-419': ['es-ES'], // Latin America Spanish falls back to Spain Spanish
'es-419': ['es-ES', 'en-US'], // Latin America Spanish falls back to Spain Spanish
default: ['en-US']
},
defaultNS: 'globals',
Expand Down
16 changes: 16 additions & 0 deletions src/languages/en-US/commands/lockdown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "lockdown",
"description": "Manage the server's lockdown status",
"actionName": "action",
"actionDescription": "The action to perform",
"channelName": "channel",
"channelDescription": "The channel to lock down",
"durationName": "duration",
"durationDescription": "How long the lockdown should last",
"roleName": "role",
"roleDescription": "The role to use for the lockdown",
"globalName": "global",
"globalDescription": "⚠️ Whether or not to apply the lockdown to the entire server",
"actionLock": "Lock",
"actionUnlock": "Unlock"
}
1 change: 1 addition & 0 deletions src/lib/i18n/languageKeys/keys/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * as Admin from '#lib/i18n/languageKeys/keys/commands/Admin';
export * as Fun from '#lib/i18n/languageKeys/keys/commands/Fun';
export * as Games from '#lib/i18n/languageKeys/keys/commands/Games';
export * as General from '#lib/i18n/languageKeys/keys/commands/General';
export * as Lockdown from '#lib/i18n/languageKeys/keys/commands/Lockdown';
export * as Management from '#lib/i18n/languageKeys/keys/commands/Management';
export * as Misc from '#lib/i18n/languageKeys/keys/commands/Misc';
export * as Moderation from '#lib/i18n/languageKeys/keys/commands/Moderation';
Expand Down
39 changes: 39 additions & 0 deletions src/lib/i18n/languageKeys/keys/commands/Lockdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { FT, T } from '#lib/types';
import type { ChannelMention, RoleMention } from 'discord.js';

// Root
export const Name = T('commands/lockdown:name');
export const Description = T('commands/lockdown:description');

// Options
export const Action = 'commands/lockdown:action';
export const Channel = 'commands/lockdown:channel';
export const Duration = 'commands/lockdown:duration';
export const Role = 'commands/lockdown:role';
export const Global = 'commands/lockdown:global';

// Action choices
export const ActionLock = T('commands/lockdown:actionLock');
export const ActionUnlock = T('commands/lockdown:actionUnlock');

export const AuditLogRequestedBy = FT<{ user: string; channel: ChannelMention }>('commands/lockdown:auditLogRequestedBy');

// Guild
export const GuildLocked = FT<{ role: RoleMention }>('commands/lockdown:guildLocked');
export const SuccessGuild = FT<{ role: RoleMention }>('commands/lockdown:successGuild');
export const GuildUnknownRole = FT<{ role: RoleMention }>('commands/lockdown:guildUnknownRole');
export const GuildLockFailed = FT<{ role: RoleMention }>('commands/lockdown:guildLockFailed');

// Thread
export const SuccessThread = FT<{ channel: ChannelMention }>('commands/lockdown:successThread');
export const ThreadLocked = FT<{ channel: ChannelMention }>('commands/lockdown:threadLocked');
export const ThreadUnmanageable = FT<{ channel: ChannelMention }>('commands/lockdown:threadUnmanageable');
export const ThreadUnknownChannel = FT<{ channel: ChannelMention }>('commands/lockdown:threadUnknownChannel');
export const ThreadLockFailed = FT<{ channel: ChannelMention }>('commands/lockdown:threadLockFailed');

// Channel
export const SuccessChannel = FT<{ channel: ChannelMention }>('commands/lockdown:successChannel');
export const ChannelLocked = FT<{ channel: ChannelMention }>('commands/lockdown:channelLocked');
export const ChannelUnmanageable = FT<{ channel: ChannelMention }>('commands/lockdown:channelUnmanageable');
export const ChannelUnknownChannel = FT<{ channel: ChannelMention }>('commands/lockdown:channelUnknownChannel');
export const ChannelLockFailed = FT<{ channel: ChannelMention }>('commands/lockdown:channelLockFailed');
2 changes: 1 addition & 1 deletion src/lib/util/functions/pieces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Piece } from '@sapphire/framework';
import { bgBlue, bgRed } from 'colorette';

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

export function getCodeStyle(code: string | number) {
Expand Down
40 changes: 40 additions & 0 deletions src/lib/util/resolvers/TimeSpan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { LanguageKeys } from '#lib/i18n/languageKeys';
import { seconds } from '#utils/common';
import { err, ok } from '@sapphire/framework';
import { Duration } from '@sapphire/time-utilities';

export function resolveTimeSpan(parameter: string, options?: TimeSpanOptions) {
const duration = parse(parameter);

if (!Number.isSafeInteger(duration)) {
return err(LanguageKeys.Arguments.TimeSpan);
}

if (typeof options?.minimum === 'number' && duration < options.minimum) {
return err(LanguageKeys.Arguments.TimeSpanTooSmall);
}

if (typeof options?.maximum === 'number' && duration > options.maximum) {
return err(LanguageKeys.Arguments.TimeSpanTooBig);
}

return ok(duration);
}

function parse(parameter: string) {
const number = Number(parameter);
if (!Number.isNaN(number)) return seconds(number);

const duration = new Duration(parameter).offset;
if (!Number.isNaN(duration)) return duration;

const date = Date.parse(parameter);
if (!Number.isNaN(date)) return date - Date.now();

return NaN;
}

export interface TimeSpanOptions {
minimum?: number;
maximum?: number;
}
1 change: 1 addition & 0 deletions src/lib/util/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '#utils/resolvers/TimeSpan';
7 changes: 4 additions & 3 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"rootDir": ".",
"baseUrl": ".",
"paths": {
"#utils/common": ["lib/util/common/index.js"],
"#utils/functions": ["lib/util/functions/index.js"],
"#utils/resolvers": ["lib/util/functions/resolvers.js"],
"#utils/*": ["lib/util/*"],
"#lib/database": ["lib/database/index.js"],
"#lib/database/entities": ["lib/database/entities/index.js"],
"#lib/database/keys": ["lib/database/keys/index.js"],
Expand All @@ -21,9 +25,6 @@
"#lib/i18n/languageKeys": ["lib/i18n/languageKeys/index.js"],
"#lib/*": ["lib/*"],
"#languages": ["languages/index.js"],
"#utils/common": ["lib/util/common/index.js"],
"#utils/functions": ["lib/util/functions/index.js"],
"#utils/*": ["lib/util/*"],
"#root/*": ["*"]
}
},
Expand Down

0 comments on commit 84a4064

Please sign in to comment.