-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
361 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '#utils/resolvers/TimeSpan'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters