Skip to content

Commit

Permalink
refactor: rewrite lockdown
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Apr 5, 2024
1 parent 3c790b6 commit d0160ac
Show file tree
Hide file tree
Showing 17 changed files with 652 additions and 241 deletions.
417 changes: 332 additions & 85 deletions src/commands/Moderation/lockdown.ts

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions src/languages/en-US/commands/lockdown.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"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",
"auditLogLockRequestedBy": "Channel locked at request of {{user}}",
"auditLogUnlockRequestedBy": "Channel unlocked at request of {{user}}",
"guildLocked": "{{role}} is already locked down in the server.",
"guildUnlocked": "{{role}} is currently not locked down in the server.",
"successGuild": "Successfully updated the lockdown status for {{role}} in the server.",
"guildUnknownRole": "I somehow could not find the role {{role}}, you can try with other roles.",
"guildLockFailed": "The role {{role}} could not be locked down, please try again later.",
"guildUnlockFailed": "The role {{role}} could not be unlocked, please try again later.",
"successThread": "Successfully updated the lockdown status for the thread {{channel}} in the server.",
"threadLocked": "The thread {{channel}} is already locked.",
"threadUnlocked": "The thread {{channel}} is currently not locked.",
"threadUnmanageable": "I cannot manage the thread {{channel}}, please update my permissions and try again.",
"threadUnknownChannel": "I somehow could not find the thread {{channel}}, you can try with other channels.",
"threadLockFailed": "The thread {{channel}} could not be locked, please try again later.",
"threadUnlockFailed": "The thread {{channel}} could not be unlocked, please try again later.",
"successChannel": "Successfully updated the lockdown status for the channel {{channel}} in the server",
"channelLocked": "The channel {{channel}} is already locked.",
"channelUnlocked": "The channel {{channel}} is currently not locked.",
"channelUnmanageable": "I cannot manage the channel {{channel}}, please update my permissions and try again.",
"channelUnknownChannel": "I somehow could not find the channel {{channel}}, you can try with other channels.",
"channelLockFailed": "The channel {{channel}} could not be locked, please try again later."
}
2 changes: 1 addition & 1 deletion src/lib/database/entities/ScheduleEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ScheduleEntity extends BaseEntity {
* The stored metadata to send to the Task
*/
@Column('jsonb')
public data!: Record<string, unknown>;
public data!: object;

/**
* Whether or not the entity is running
Expand Down
18 changes: 17 additions & 1 deletion src/lib/database/settings/structures/Task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PartialResponseValue } from '#lib/database/entities';
import { ResponseType, type PartialResponseValue } from '#lib/database/entities';
import { Piece } from '@sapphire/framework';
import type { Awaitable } from '@sapphire/utilities';

Expand All @@ -8,6 +8,22 @@ export abstract class Task extends Piece {
* @param data The data
*/
public abstract run(data: unknown): Awaitable<PartialResponseValue | null>;

protected ignore() {
return { type: ResponseType.Ignore } as const;
}

protected finish() {
return { type: ResponseType.Finished } as const;
}

protected delay(value: number) {
return { type: ResponseType.Delay, value } as const;
}

protected update(value: Date) {
return { type: ResponseType.Update, value } as const;
}
}

export namespace Task {
Expand Down
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 @@ -4,6 +4,7 @@ export * as Case from '#lib/i18n/languageKeys/keys/commands/Case';
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
45 changes: 45 additions & 0 deletions src/lib/i18n/languageKeys/keys/commands/Lockdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 AuditLogLockRequestedBy = FT<{ user: string }>('commands/lockdown:auditLogLockRequestedBy');
export const AuditLogUnlockRequestedBy = FT<{ user: string }>('commands/lockdown:auditLogUnlockRequestedBy');

// Guild
export const GuildLocked = FT<{ role: RoleMention }>('commands/lockdown:guildLocked');
export const GuildUnlocked = FT<{ role: RoleMention }>('commands/lockdown:guildUnlocked');
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');
export const GuildUnlockFailed = FT<{ role: RoleMention }>('commands/lockdown:guildUnlockFailed');

// Thread
export const SuccessThread = FT<{ channel: ChannelMention }>('commands/lockdown:successThread');
export const ThreadLocked = FT<{ channel: ChannelMention }>('commands/lockdown:threadLocked');
export const ThreadUnlocked = FT<{ channel: ChannelMention }>('commands/lockdown:threadUnlocked');
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');
export const ThreadUnlockFailed = FT<{ channel: ChannelMention }>('commands/lockdown:threadUnlockFailed');

// Channel
export const SuccessChannel = FT<{ channel: ChannelMention }>('commands/lockdown:successChannel');
export const ChannelLocked = FT<{ channel: ChannelMention }>('commands/lockdown:channelLocked');
export const ChannelUnlocked = FT<{ channel: ChannelMention }>('commands/lockdown:channelUnlocked');
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');
1 change: 1 addition & 0 deletions src/lib/moderation/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from '#lib/moderation/actions/index';
export * from '#lib/moderation/managers/index';
export * from '#lib/moderation/structures/index';
export * from '#lib/moderation/types';
77 changes: 77 additions & 0 deletions src/lib/moderation/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { Snowflake } from 'discord.js';

export type LockdownData = LockdownGuildData | LockdownChannelData | LockdownThreadData;

export enum LockdownType {
Guild,
Channel,
Thread
}

interface BaseLockdownData<T extends LockdownType> {
/**
* The type of lockdown that was applied.
*/
type: T;

/**
* The ID of the guild where the lockdown was applied.
*/
guildId: Snowflake;

/**
* The ID of the user who initiated the lockdown.
*/
userId: Snowflake;
}

export interface LockdownGuildData extends BaseLockdownData<LockdownType.Guild> {
/**
* The ID of the role that was locked down.
*/
roleId: Snowflake;

/**
* The permissions that were applied to the role, as a bitfield.
*/
permissionsApplied: number;

/**
* The original permissions for the role before the lockdown.
*/
permissionsOriginal: number;
}

export interface LockdownChannelData extends BaseLockdownData<LockdownType.Channel> {
/**
* The ID of the channel where the lockdown was applied.
*/
channelId: Snowflake;

/**
* The ID of the role that was locked down in the channel.
*/
roleId: Snowflake;

/**
* The permissions that were applied to the role, as a bitfield.
*/
permissionsApplied: number | null;

/**
* The original allow overrides for the role before the lockdown.
*/
permissionsOriginalAllow: number;

/**
* The original deny overrides for the role before the lockdown.
*/
permissionsOriginalDeny: number;
}

export interface LockdownThreadData extends BaseLockdownData<LockdownType.Thread> {
/**
* The ID of the thread where the lockdown was applied.
*/
channelId: Snowflake;
}
73 changes: 0 additions & 73 deletions src/lib/structures/managers/LockdownManager.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/structures/managers/ScheduleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export interface ScheduleManagerAddOptions {
/**
* The data to pass to the Task piece when the ScheduledTask is ready for execution.
*/
data?: Record<string, unknown>;
data?: object;
}

export type TimeResolvable = number | Date | string | Cron;
1 change: 0 additions & 1 deletion src/lib/structures/managers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from '#lib/structures/managers/LockdownManager';
export * from '#lib/structures/managers/ScheduleManager';
21 changes: 0 additions & 21 deletions src/lib/util/Security/GuildSecurity.ts

This file was deleted.

34 changes: 0 additions & 34 deletions src/lib/util/Timers.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/lib/util/functions/guild.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { LoggerManager, ModerationManager, StickyRoleManager } from '#lib/moderation/managers';
import { GuildSecurity } from '#utils/Security/GuildSecurity';
import { container } from '@sapphire/framework';
import type { Guild, GuildResolvable } from 'discord.js';

interface GuildUtilities {
readonly logger: LoggerManager;
readonly moderation: ModerationManager;
readonly security: GuildSecurity;
readonly stickyRoles: StickyRoleManager;
}

Expand All @@ -20,7 +18,6 @@ export function getGuildUtilities(resolvable: GuildResolvable): GuildUtilities {
const entry: GuildUtilities = {
logger: new LoggerManager(guild),
moderation: new ModerationManager(guild),
security: new GuildSecurity(guild),
stickyRoles: new StickyRoleManager(guild)
};
cache.set(guild, entry);
Expand All @@ -30,7 +27,6 @@ export function getGuildUtilities(resolvable: GuildResolvable): GuildUtilities {

export const getLogger = getProperty('logger');
export const getModeration = getProperty('moderation');
export const getSecurity = getProperty('security');
export const getStickyRoles = getProperty('stickyRoles');

function getProperty<K extends keyof GuildUtilities>(property: K) {
Expand Down
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
Loading

0 comments on commit d0160ac

Please sign in to comment.