Skip to content

refactor: rewrite lockdown command #2593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
417 changes: 332 additions & 85 deletions src/commands/Moderation/lockdown.ts

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/commands/System/Admin/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ export class UserCommand extends SkyraCommand {
},
structures: {
...(await import('#lib/structures')),
data: await import('#lib/structures/data'),
managers: await import('#lib/structures/managers')
data: await import('#lib/structures/data')
}
},
container: this.container,
Expand Down
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."
}
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 @@ -5,6 +5,7 @@ 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 Info from '#lib/i18n/languageKeys/keys/commands/Info';
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');
7 changes: 6 additions & 1 deletion src/lib/moderation/managers/ModerationManagerEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,12 @@ export class ModerationManagerEntry<Type extends TypeVariation = TypeVariation>
}

#isMatchingTask(task: ScheduleEntry) {
return task.data !== null && task.data.caseID === this.id && task.data.guildID === this.guild.id;
return (
task.data !== null && //
'caseID' in task.data &&
task.data.caseID === this.id &&
task.data.guildID === this.guild.id
);
}

#setDuration(duration: bigint | number | null) {
Expand Down
78 changes: 78 additions & 0 deletions src/lib/schedule/manager/ScheduleEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Schedule } from '@prisma/client';
import { container } from '@sapphire/framework';
import { Cron } from '@sapphire/time-utilities';
import { isNullishOrEmpty } from '@sapphire/utilities';
import type { Snowflake } from 'discord-api-types/v10';

export class ScheduleEntry<Type extends ScheduleEntry.TaskId = ScheduleEntry.TaskId> {
public id: number;
Expand Down Expand Up @@ -126,6 +127,7 @@ export namespace ScheduleEntry {
export interface TaskData {
poststats: null;
syncResourceAnalytics: null;
moderationEndLockdown: LockdownData;
moderationEndAddRole: SharedModerationTaskData<TypeVariation.RoleAdd>;
moderationEndBan: SharedModerationTaskData<TypeVariation.Ban>;
moderationEndMute: SharedModerationTaskData<TypeVariation.Mute>;
Expand Down Expand Up @@ -166,3 +168,79 @@ export namespace ScheduleEntry {
data?: TaskData[Type];
}
}

export type LockdownData = LockdownGuildData | LockdownChannelData | LockdownThreadData;

export enum LockdownType {
Guild,
Channel,
Thread
}

export 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;
}
18 changes: 17 additions & 1 deletion src/lib/schedule/structures/Task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PartialResponseValue } from '#lib/schedule/manager/ScheduleEntry';
import { ResponseType, type PartialResponseValue } from '#lib/schedule/manager/ScheduleEntry';
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 satisfies PartialResponseValue;
}

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

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

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

export namespace Task {
Expand Down
1 change: 0 additions & 1 deletion src/lib/structures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ export * from '#lib/structures/InviteStore';
export * from '#lib/structures/SettingsMenu';
export * from '#lib/structures/commands/index';
export * from '#lib/structures/listeners/index';
export * from '#lib/structures/managers';
export * from '#lib/structures/preconditions/index';
73 changes: 0 additions & 73 deletions src/lib/structures/managers/LockdownManager.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/structures/managers/index.ts

This file was deleted.

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.

Loading